void* mov_reader_create(const char* file) { struct mov_reader_t* reader; reader = (struct mov_reader_t*)malloc(sizeof(*reader)); if (NULL == reader) return NULL; memset(reader, 0, sizeof(*reader)); // ISO/IEC 14496-12:2012(E) 4.3.1 Definition (p17) // Files with no file-type box should be read as if they contained an FTYP box // with Major_brand='mp41', minor_version=0, and the single compatible brand 'mp41'. reader->mov.ftyp.major_brand = MOV_BRAND_MP41; reader->mov.ftyp.minor_version = 0; reader->mov.ftyp.brands_count = 0; reader->mov.header = 0; reader->mov.fp = file_reader_create(file); if (NULL == reader->mov.fp || 0 != mov_reader_init(&reader->mov)) { mov_reader_destroy(reader); return NULL; } reader->dts = INT64_MAX; return reader; }
bool test_file_reader_create_system_call_1 (Test *test) { TITLE (); CATCH (file_reader_create ("/stage/error_stat")); CATCH (error_count () == 0); CATCH (error_at (0).error != ErrorSystemCall); CATCH (error_at (0).code != 1); PASS (); }
bool test_file_reader_create_function_call_1 (Test *test) { TITLE (); memory_commit_limit (0); CATCH (file_reader_create ("stage/file_reader/file")); CATCH (error_count () == 0); CATCH (error_at (0).error != ErrorFunctionCall); CATCH (error_at (0).code != 1); PASS (); }
bool test_file_reader_create_system_call_3 (Test *test) { char *path; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/error_mmap")); CATCH (file_reader_create (path)); CATCH (error_count () == 0); CATCH (error_at (0).error != ErrorSystemCall); CATCH (error_at (0).code != 3); string_destroy (path); PASS (); }
bool test_file_reader_create (Test *test) { FileReader *reader; char *path; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/file_reader/file")); CATCH (!(reader = file_reader_create (path))); CATCH (reader->size != 5); CATCH (!string_equals ((const char *)reader->map, "test\n")); file_reader_destroy (reader); string_destroy (path); PASS (); }
bool test_file_writer_write_args (Test *test) { FileWriter *writer; FileReader *reader; char *path; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/file_writer/args")); if (file_exists (path)) { CATCH (!file_remove (path)); } CATCH (!(writer = file_writer_create (path, FileWriterModeTruncate))); CATCH (!file_writer_write_args (writer, "abc%i123%s", 0, "4")); file_writer_destroy (writer); CATCH (!(reader = file_reader_create (path))); CATCH (!string_equals ((const char *)reader->map, "abc01234")); file_reader_destroy (reader); string_destroy (path); PASS (); }
bool test_print_log_2 (Test *test) { FileReader *reader; char *path; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/print_log/log")); print_silent (false); if (file_exists (path)) { CATCH (!file_remove (path)); } CATCH (!print_log_begin (path)); CATCH (!print (" \b")); CATCH (!print_log_end ()); CATCH (!(reader = file_reader_create (path))); print_silent (true); CATCH (!string_equals ((const char *)reader->map, " \b")); file_reader_destroy (reader); string_destroy (path); PASS (); }
int run_file(const char* filename, context* ctx) { reader r; lexer_state* ls = calloc(sizeof(lexer_state), 1); parser_state* ps = calloc(sizeof(parser_state), 1); FILE* fp = fopen(filename, "r"); if(!fp) { printf("Error: can't open file '%s'\n", filename); return 1; } file_reader_create(&r, fp); lexer_create(ls, &r); ps->ls = ls; ps->die_on_error = 0; ps->error.max = 20; ps->t = lexer_next_token(ps->ls); expression_node* program = parse_program(ps); expression_node* eval = expression_node_evaluate(program, ctx); expression_node_destroy(eval); expression_node_destroy(program); lexer_destroy(ls); free(ls); free(ps); free(r.fn_data); fclose(fp); return 0; }
int main (int argc, char **argv) { char *pattern; char *file; FileReader *reader = NULL; PatternSearch *search = NULL; bool success; AppArgument arguments[] = { ARGUMENT_ORDINAL_STRING (NULL, true, &pattern, "Pattern."), ARGUMENT_ORDINAL_STRING (NULL, true, &file, "File."), ARGUMENT_DEFAULT, ARGUMENT_END }; if (!app_arguments (argc, argv, arguments)) { app_arguments_usage (argc, argv, arguments); return EXIT_FAILURE; } success = try (&reader, &search, pattern, file); if (reader) { file_reader_destroy (reader); } if (search) { pattern_search_destroy (search); } if (!success || error_count () != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; } static bool try (FileReader **reader, PatternSearch **search, const char *pattern, const char *file) { size_t from, to; if (!(*reader = file_reader_create (file))) { error_code (FunctionCall, 1); return false; } if (!(*search = pattern_search_create ((*reader)->map, (size_t)(*reader)->size, pattern, false))) { error_code (FunctionCall, 2); return false; } while (pattern_search_next (*search, &from, &to)) { if (!print_range ((const char *)(*reader)->map, from, to)) { error_code (FunctionCall, 3); return false; } if (putc ((int)'\n', stdout) == EOF) { error (SystemCall); return false; } } return true; } static bool print_range (const char *buffer, size_t from, size_t to) { if (to - from == 0) { return true; } if (fwrite (buffer + from, 1, to - from, stdout) != to - from) { error (SystemCall); return false; } return true; }