xref: /aosp_15_r20/external/webp/examples/example_util.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li //  Utility functions used by the example programs.
11*b2055c35SXin Li //
12*b2055c35SXin Li 
13*b2055c35SXin Li #ifndef WEBP_EXAMPLES_EXAMPLE_UTIL_H_
14*b2055c35SXin Li #define WEBP_EXAMPLES_EXAMPLE_UTIL_H_
15*b2055c35SXin Li 
16*b2055c35SXin Li #include "webp/types.h"
17*b2055c35SXin Li #include "webp/mux_types.h"
18*b2055c35SXin Li 
19*b2055c35SXin Li #ifdef __cplusplus
20*b2055c35SXin Li extern "C" {
21*b2055c35SXin Li #endif
22*b2055c35SXin Li 
23*b2055c35SXin Li //------------------------------------------------------------------------------
24*b2055c35SXin Li // String parsing
25*b2055c35SXin Li 
26*b2055c35SXin Li // Parses 'v' using strto(ul|l|d)(). If error is non-NULL, '*error' is set to
27*b2055c35SXin Li // true on failure while on success it is left unmodified to allow chaining of
28*b2055c35SXin Li // calls. An error is only printed on the first occurrence.
29*b2055c35SXin Li uint32_t ExUtilGetUInt(const char* const v, int base, int* const error);
30*b2055c35SXin Li int ExUtilGetInt(const char* const v, int base, int* const error);
31*b2055c35SXin Li float ExUtilGetFloat(const char* const v, int* const error);
32*b2055c35SXin Li 
33*b2055c35SXin Li // This variant of ExUtilGetInt() will parse multiple integers from a
34*b2055c35SXin Li // comma-separated list. Up to 'max_output' integers are parsed.
35*b2055c35SXin Li // The result is placed in the output[] array, and the number of integers
36*b2055c35SXin Li // actually parsed is returned, or -1 if an error occurred.
37*b2055c35SXin Li int ExUtilGetInts(const char* v, int base, int max_output, int output[]);
38*b2055c35SXin Li 
39*b2055c35SXin Li // Reads a file named 'filename' into a WebPData structure. The content of
40*b2055c35SXin Li // webp_data is overwritten. Returns false in case of error.
41*b2055c35SXin Li int ExUtilReadFileToWebPData(const char* const filename,
42*b2055c35SXin Li                              WebPData* const webp_data);
43*b2055c35SXin Li 
44*b2055c35SXin Li //------------------------------------------------------------------------------
45*b2055c35SXin Li // Command-line arguments
46*b2055c35SXin Li 
47*b2055c35SXin Li typedef struct {
48*b2055c35SXin Li   int argc_;
49*b2055c35SXin Li   const char** argv_;
50*b2055c35SXin Li   WebPData argv_data_;
51*b2055c35SXin Li   int own_argv_;
52*b2055c35SXin Li } CommandLineArguments;
53*b2055c35SXin Li 
54*b2055c35SXin Li // Initializes the structure from the command-line parameters. If there is
55*b2055c35SXin Li // only one parameter and it does not start with a '-', then it is assumed to
56*b2055c35SXin Li // be a file name. This file will be read and tokenized into command-line
57*b2055c35SXin Li // arguments. The content of 'args' is overwritten.
58*b2055c35SXin Li // Returns false in case of error (memory allocation failure, non
59*b2055c35SXin Li // existing file, too many arguments, ...).
60*b2055c35SXin Li int ExUtilInitCommandLineArguments(int argc, const char* argv[],
61*b2055c35SXin Li                                    CommandLineArguments* const args);
62*b2055c35SXin Li 
63*b2055c35SXin Li // Deallocate all memory and reset 'args'.
64*b2055c35SXin Li void ExUtilDeleteCommandLineArguments(CommandLineArguments* const args);
65*b2055c35SXin Li 
66*b2055c35SXin Li #ifdef __cplusplus
67*b2055c35SXin Li }    // extern "C"
68*b2055c35SXin Li #endif
69*b2055c35SXin Li 
70*b2055c35SXin Li #endif  // WEBP_EXAMPLES_EXAMPLE_UTIL_H_
71