- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/06_jpeg_decode/jpeg_decode_csvparser.cpp

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
    00003  *
    00004  * Redistribution and use in source and binary forms, with or without
    00005  * modification, are permitted provided that the following conditions
    00006  * are met:
    00007  *  * Redistributions of source code must retain the above copyright
    00008  *    notice, this list of conditions and the following disclaimer.
    00009  *  * Redistributions in binary form must reproduce the above copyright
    00010  *    notice, this list of conditions and the following disclaimer in the
    00011  *    documentation and/or other materials provided with the distribution.
    00012  *  * Neither the name of NVIDIA CORPORATION nor the names of its
    00013  *    contributors may be used to endorse or promote products derived
    00014  *    from this software without specific prior written permission.
    00015  *
    00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
    00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    00019  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00020  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00022  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00023  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    00024  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00026  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    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 }