00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 #include <cstdlib>
00031 #include <cstring>
00032
00033 #include "jpeg_decode.h"
00034
00035 #define CHECK_OPTION_VALUE(argp) if(!*argp || (*argp)[0] == '-') \
00036 { \
00037 cerr << "Error: value not specified for option " << arg << endl; \
00038 goto error; \
00039 }
00040
00041 #define CSV_PARSE_CHECK_ERROR(condition, str) \
00042 if (condition) {\
00043 cerr << "Error: " << str << endl; \
00044 goto error; \
00045 }
00046
00047 using namespace std;
00048
00049 static void
00050 print_help(void)
00051 {
00052 cerr <<
00053 "\njpeg-decode <in-file> <out-file> [OPTIONS]\n\n"
00054 "OPTIONS:\n"
00055 "\t-h,--help Prints this text\n"
00056 "\t--dbg-level <level> Sets the debug level [Values 0-3]\n\n"
00057 "\t--decode-fd Uses FD as output of decoder [DEFAULT]\n"
00058 "\t--decode-buffer Uses buffer as output of decoder\n\n";
00059 }
00060
00061 static int32_t
00062 get_dbg_level(char *arg)
00063 {
00064 int32_t log_level = atoi(arg);
00065 if (log_level < 0)
00066 {
00067 cout<<"log level too small, set to 0"<<endl;
00068 return 0;
00069 }
00070
00071 if (log_level > 3)
00072 {
00073 cout<<"log level too high, set to 3"<<endl;
00074 return 3;
00075 }
00076
00077 return log_level;
00078 }
00079
00080 int
00081 parse_csv_args(context_t * ctx, int argc, char *argv[])
00082 {
00083 char **argp = argv;
00084 char *arg = *(++argp);
00085
00086 if (argc == 1 || (arg && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))))
00087 {
00088 print_help();
00089 exit(EXIT_SUCCESS);
00090 }
00091
00092 CSV_PARSE_CHECK_ERROR(argc < 3, "Insufficient arguments");
00093
00094 ctx->in_file_path = strdup(*argp);
00095 CSV_PARSE_CHECK_ERROR(!ctx->in_file_path, "Input file not specified");
00096
00097 ctx->out_file_path = strdup(*(++argp));
00098 CSV_PARSE_CHECK_ERROR(!ctx->out_file_path, "Output file not specified");
00099
00100 while ((arg = *(++argp)))
00101 {
00102 if (!strcmp(arg, "--dbg-level"))
00103 {
00104 argp++;
00105 CHECK_OPTION_VALUE(argp);
00106 log_level = get_dbg_level(*argp);
00107 }
00108 else if (!strcmp(arg, "--decode-fd"))
00109 {
00110 ctx->use_fd = true;
00111 }
00112 else if (!strcmp(arg, "--decode-buffer"))
00113 {
00114 ctx->use_fd = false;
00115 }
00116 else if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
00117 {
00118 print_help();
00119 exit(EXIT_SUCCESS);
00120 }
00121 else
00122 {
00123 CSV_PARSE_CHECK_ERROR(ctx->out_file_path, "Unknown option " << arg);
00124 }
00125 }
00126
00127 return 0;
00128
00129 error:
00130 print_help();
00131 return -1;
00132 }