example_util.c (4213B)
1 // Copyright 2012 Google Inc. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the COPYING file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 // ----------------------------------------------------------------------------- 9 // 10 // Utility functions used by the example programs. 11 // 12 13 #include "./example_util.h" 14 15 #include <assert.h> 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 20 #include "../imageio/imageio_util.h" 21 #include "webp/mux_types.h" 22 #include "webp/types.h" 23 24 //------------------------------------------------------------------------------ 25 // String parsing 26 27 uint32_t ExUtilGetUInt(const char* const v, int base, int* const error) { 28 char* end = NULL; 29 const uint32_t n = (v != NULL) ? (uint32_t)strtoul(v, &end, base) : 0u; 30 if (end == v && error != NULL && !*error) { 31 *error = 1; 32 fprintf(stderr, "Error! '%s' is not an integer.\n", 33 (v != NULL) ? v : "(null)"); 34 } 35 return n; 36 } 37 38 int ExUtilGetInt(const char* const v, int base, int* const error) { 39 return (int)ExUtilGetUInt(v, base, error); 40 } 41 42 int ExUtilGetInts(const char* v, int base, int max_output, int output[]) { 43 int n, error = 0; 44 for (n = 0; v != NULL && n < max_output; ++n) { 45 const int value = ExUtilGetInt(v, base, &error); 46 if (error) return -1; 47 output[n] = value; 48 v = strchr(v, ','); 49 if (v != NULL) ++v; // skip over the trailing ',' 50 } 51 return n; 52 } 53 54 float ExUtilGetFloat(const char* const v, int* const error) { 55 char* end = NULL; 56 const float f = (v != NULL) ? (float)strtod(v, &end) : 0.f; 57 if (end == v && error != NULL && !*error) { 58 *error = 1; 59 fprintf(stderr, "Error! '%s' is not a floating point number.\n", 60 (v != NULL) ? v : "(null)"); 61 } 62 return f; 63 } 64 65 //------------------------------------------------------------------------------ 66 67 static void ResetCommandLineArguments(int argc, const char* argv[], 68 CommandLineArguments* const args) { 69 assert(args != NULL); 70 args->argc = argc; 71 args->argv = argv; 72 args->own_argv = 0; 73 WebPDataInit(&args->argv_data); 74 } 75 76 void ExUtilDeleteCommandLineArguments(CommandLineArguments* const args) { 77 if (args != NULL) { 78 if (args->own_argv) { 79 WebPFree((void*)args->argv); 80 WebPDataClear(&args->argv_data); 81 } 82 ResetCommandLineArguments(0, NULL, args); 83 } 84 } 85 86 #define MAX_ARGC 16384 87 int ExUtilInitCommandLineArguments(int argc, const char* argv[], 88 CommandLineArguments* const args) { 89 if (args == NULL || argv == NULL) return 0; 90 ResetCommandLineArguments(argc, argv, args); 91 if (argc == 1 && argv[0][0] != '-') { 92 char* cur; 93 const char sep[] = " \t\r\n\f\v"; 94 95 #if defined(_WIN32) && defined(_UNICODE) 96 fprintf(stderr, 97 "Error: Reading arguments from a file is a feature unavailable " 98 "with Unicode binaries.\n"); 99 return 0; 100 #endif 101 102 if (!ExUtilReadFileToWebPData(argv[0], &args->argv_data)) { 103 return 0; 104 } 105 args->own_argv = 1; 106 args->argv = (const char**)WebPMalloc(MAX_ARGC * sizeof(*args->argv)); 107 if (args->argv == NULL) { 108 ExUtilDeleteCommandLineArguments(args); 109 return 0; 110 } 111 112 argc = 0; 113 for (cur = strtok((char*)args->argv_data.bytes, sep); 114 cur != NULL; 115 cur = strtok(NULL, sep)) { 116 if (argc == MAX_ARGC) { 117 fprintf(stderr, "ERROR: Arguments limit %d reached\n", MAX_ARGC); 118 ExUtilDeleteCommandLineArguments(args); 119 return 0; 120 } 121 assert(strlen(cur) != 0); 122 args->argv[argc++] = cur; 123 } 124 args->argc = argc; 125 } 126 return 1; 127 } 128 129 //------------------------------------------------------------------------------ 130 131 int ExUtilReadFileToWebPData(const char* const filename, 132 WebPData* const webp_data) { 133 const uint8_t* data; 134 size_t size; 135 if (webp_data == NULL) return 0; 136 if (!ImgIoUtilReadFile(filename, &data, &size)) return 0; 137 webp_data->bytes = data; 138 webp_data->size = size; 139 return 1; 140 }