File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1818
1919#include <stdint.h>
2020#include <stddef.h>
21- #include <stdio.h>
2221#include <assert.h>
22+ #include <stdio.h>
23+ #include <string.h>
2324
2425#include "include/file.h"
2526#include "include/args.h"
2627#include "include/byte_array.h"
2728#include "include/util.h"
2829
30+ #define STDIN_FILENAME "-"
31+ #define STDOUT_FILENAME "-"
32+
33+ FILE * file_open (const char * path , enum EFileOpenMode mode ) {
34+ switch (mode ) {
35+ case FILE_MODE_READ :
36+ if (strcmp (path , STDIN_FILENAME ) == 0 )
37+ return stdin ;
38+ else
39+ return fopen (path , "rb" );
40+
41+ case FILE_MODE_WRITE :
42+ if (strcmp (path , STDOUT_FILENAME ) == 0 )
43+ return stdout ;
44+ else
45+ return fopen (path , "wb" );
46+
47+ default :
48+ DIE ("Invalid mode enumerator. Aborting." );
49+ }
50+ }
51+
2952bool file_read (ByteArray * dst ,
3053 FILE * fp ,
3154 size_t offset_start ,
Original file line number Diff line number Diff line change 2525
2626#include "byte_array.h"
2727
28+ /*
29+ * Enumeration with the available modes for opening files.
30+ */
31+ enum EFileOpenMode {
32+ FILE_MODE_READ ,
33+ FILE_MODE_WRITE ,
34+ };
35+
36+ /*----------------------------------------------------------------------------*/
37+
38+ /*
39+ * Open the file at the specified path with the specified mode. This function
40+ * supports special strings for specifying 'stdin' and 'stdout', defined in
41+ * 'file.c'.
42+ *
43+ * The caller is responsible for checking if this function returns NULL in case
44+ * of error.
45+ */
46+ FILE * file_open (const char * path , enum EFileOpenMode mode );
47+
2848/*
2949 * Read the bytes of a file in a linear way from the starting offset to the end
3050 * offset. This function returns true on success, or false otherwise.
Original file line number Diff line number Diff line change @@ -34,11 +34,8 @@ int main(int argc, char** argv) {
3434 args_init (& args );
3535 args_parse (& args , argc , argv );
3636
37- /*
38- * Open the input for reading.
39- * TODO: Support "-" as a filename for standard input.
40- */
41- FILE * input_fp = fopen (args .input_filename , "rb" );
37+ /* Open the input for reading */
38+ FILE * input_fp = file_open (args .input_filename , FILE_MODE_READ );
4239 if (input_fp == NULL )
4340 DIE ("Can't open file '%s': %s" , args .input_filename , strerror (errno ));
4441
@@ -67,9 +64,7 @@ int main(int argc, char** argv) {
6764 image_transform_squares (image , args .transform_squares_side );
6865
6966 /* Open the output file for writing */
70- FILE * output_fp = (strcmp (args .output_filename , "-" ) == 0 )
71- ? stdout
72- : fopen (args .output_filename , "wb" );
67+ FILE * output_fp = file_open (args .output_filename , FILE_MODE_WRITE );
7368 if (output_fp == NULL )
7469 DIE ("Can't open file '%s': %s" , args .output_filename , strerror (errno ));
7570
You can’t perform that action at this time.
0 commit comments