d57154beb9572e052715183c9f8c6c5b926b5ddb
[libdcp.git] / asdcplib / src / kmuuidgen.cpp
1 /*
2 Copyright (c) 2005-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27   /*! \file    kmuuidgen.cpp
28     \version $Id: kmuuidgen.cpp,v 1.7 2009/03/04 17:52:45 jhurst Exp $
29     \brief   UUID generation utility
30   */
31
32 #include "AS_DCP.h"
33 #include <KM_util.h>
34 #include <ctype.h>
35
36
37 const char* PROGRAM_NAME = "kmuuidgen";
38
39 // Increment the iterator, test for an additional non-option command line argument.
40 // Causes the caller to return if there are no remaining arguments or if the next
41 // argument begins with '-'.
42 #define TEST_EXTRA_ARG(i,c)    if ( ++i >= argc || argv[(i)][0] == '-' ) \
43                                  { \
44                                    fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
45                                    return; \
46                                  }
47
48 //
49 void
50 banner(FILE* stream = stdout)
51 {
52   fprintf(stream, "\n\
53 %s (asdcplib %s)\n\n\
54 Copyright (c) 2003-2009 John Hurst\n\n\
55 %s is part of the asdcp DCP tools package.\n\
56 asdcplib may be copied only under the terms of the license found at\n\
57 the top of every file in the asdcplib distribution kit.\n\n\
58 Specify the -h (help) option for further information about %s\n\n",
59           PROGRAM_NAME, Kumu::Version(), PROGRAM_NAME, PROGRAM_NAME);
60 }
61
62 //
63 void
64 usage(FILE* stream = stdout)
65 {
66   fprintf(stream, "\
67 USAGE: %s [-c] [-n] [-v]\n\
68 \n\
69        %s [-h|-help] [-V]\n\
70 \n\
71   -c          - Output a C-language struct containing the value\n\
72   -h | -help  - Show help\n\
73   -n          - Suppress the newline\n\
74   -v          - Verbose. Prints informative messages to stderr\n\
75   -V          - Show version information\n\
76 \n\
77   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
78          o All option arguments must be separated from the option by whitespace.\n\
79 \n", PROGRAM_NAME, PROGRAM_NAME);
80 }
81
82 //
83 class CommandOptions
84 {
85   CommandOptions();
86
87 public:
88   bool   error_flag;      // true if the given options are in error or not complete
89   bool   no_newline_flag; //
90   bool   c_array_flag;    //
91   bool   version_flag;    // true if the version display option was selected
92   bool   help_flag;       // true if the help display option was selected
93   bool   verbose_flag;    // true if the verbose flag was selected
94
95  //
96   CommandOptions(int argc, const char** argv) :
97     error_flag(true), no_newline_flag(false), c_array_flag(false), version_flag(false),
98     help_flag(false), verbose_flag(false)
99   {
100     for ( int i = 1; i < argc; i++ )
101       {
102
103          if ( (strcmp( argv[i], "-help") == 0) )
104            {
105              help_flag = true;
106              continue;
107            }
108      
109         if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
110           {
111             switch ( argv[i][1] )
112               {
113               case 'c': c_array_flag = true; break;
114               case 'n': no_newline_flag = true; break;
115               case 'h': help_flag = true; break;
116               case 'v': verbose_flag = true; break;
117               case 'V': version_flag = true; break;
118
119               default:
120                 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
121                 return;
122               }
123           }
124         else
125           {
126             fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
127             return;
128           }
129       }
130
131     if ( help_flag || version_flag )
132       return;
133     
134     error_flag = false;
135   }
136 };
137
138
139
140 //
141 int
142 main(int argc, const char** argv)
143 {
144   CommandOptions Options(argc, argv);
145
146    if ( Options.version_flag )
147     banner();
148
149   if ( Options.help_flag )
150     usage();
151
152   if ( Options.version_flag || Options.help_flag )
153     return 0;
154
155   if ( Options.error_flag )
156     {
157       fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
158       return 3;
159     }
160
161   Kumu::UUID UUID;
162   Kumu::GenRandomValue(UUID);
163   char uuid_buf[40];
164
165   if ( Options.c_array_flag )
166     {
167       const byte_t* p = UUID.Value();
168
169       printf("\
170 byte_t uuid_buf[] = {\n\
171   // %s\n ",
172              UUID.EncodeHex(uuid_buf, 40));
173           
174       for ( ui32_t i = 0; i < 16; i++ )
175         printf(" 0x%02x,", p[i]);
176
177       printf("\n");
178       printf("};\n");
179       return 0;
180     }
181   else
182     {
183       fputs(UUID.EncodeHex(uuid_buf, 40), stdout);
184     }
185
186   if ( Options.no_newline_flag == 0 )
187     printf("\n");
188
189   return 0;
190 }
191
192
193 //
194 // end kmuuidgen.cpp
195 //