Commits

Anonymous committed 1643192

Demo: option added to specify buffer size when reading

  • Participants
  • Parent commits 2ef8c91

Comments (0)

Files changed (1)

 	jsmn_parser parser;
 	char *js = NULL;
 	jsontok_t *tokens;
+	int block_size = 1024;
 	int num_tokens = 100;
 
-	while ((c = getopt(argc, argv, "ht:")) != -1) {
+	while ((c = getopt(argc, argv, "ht:b:")) != -1) {
 		switch (c) {
 			case 'h':
 				usage();
 					exit(EXIT_FAILURE);
 				}
 				break;
+			case 'b':
+				block_size = atoi(optarg);
+				if (errno || block_size < 0) {
+					fprintf(stderr, "Invalid block size: %s!\n", optarg);
+					exit(EXIT_FAILURE);
+				}
 		}
 	}
 
 
 	jsmn_init_parser(&parser, js, tokens, num_tokens);
 
+	char *buf = malloc(block_size);
 	while (1) {
-		char buf[BUFSIZ];
-		r = fread(buf, 1, 1, f);
+		r = fread(buf, 1, block_size, f);
 		if (r <= 0) {
 			break;
 		}
 	}
 
 	fclose(f);
+	free(buf);
 	free(tokens);
 	free(js);