Reverting const accessor for class optional_property
[asdcplib.git] / src / kmfilegen.cpp
index bc1243d9da8e7106801b6508d7bdea0687004a1a..2ce033251fa84b23086ba788030fe13a642ea857 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2009, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -40,7 +40,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 using namespace Kumu;
 
 // constants
-static const char* PACKAGE = "kmfilegen";  // program name for messages
+static const char* PROGRAM_NAME = "kmfilegen";  // program name for messages
 const ui32_t RNG_KEY_SIZE = 16;
 const ui32_t RNG_KEY_SIZE_BITS = 128;
 const ui32_t RNG_BLOCK_SIZE = 16;
@@ -69,12 +69,12 @@ banner(FILE* stream = stdout)
 {
   fprintf(stream, "\n\
 %s (asdcplib %s)\n\n\
-Copyright (c) 2005-2006 John Hurst\n\
+Copyright (c) 2005-2009 John Hurst\n\
 %s is part of the asdcplib DCP tools package.\n\
 asdcplib may be copied only under the terms of the license found at\n\
 the top of every file in the asdcplib distribution kit.\n\n\
 Specify the -h (help) option for further information about %s\n\n",
-         PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
+         PROGRAM_NAME, Kumu::Version(), PROGRAM_NAME, PROGRAM_NAME);
 }
 
 
@@ -83,23 +83,26 @@ void
 usage(FILE* stream = stdout)
 {
   fprintf(stream, "\
-USAGE: %s [-c <file-size>][-o <fwd|rev|rand>][-w <filename>][-v]\n\
-       <filename>\n\
+USAGE: %s [-c <file-size>] [-v] <output-file>\n\
+\n\
+       %s [-o <fwd|rev|rand>] [-v] <input-file>\n\
+\n\
+       %s [-w <output-file>] [-v] <input-file>\n\
 \n\
        %s [-h|-help] [-V]\n\
 \n\
-  -c <file-size>  - Create a test file containing <file-size> megabytes of data\n\
-  -h | -help      - Show help\n\
-  -o <order>      - Specify order used when validating a file.\n\
-                    One of fwd|rev|rand, default is rand\n\
-  -v              - Verbose. Prints informative messages to stderr\n\
-  -V              - Show version information\n\
-  -w              - Read-Validate-Write - file is written to <filename>\n\
-                    (sequential read only)\n\
+  -c <file-size>     - Create test file containing <file-size> megabytes of data\n\
+  -h | -help         - Show help\n\
+  -o <fwd|rev|rand>  - Specify order used when validating a file.\n\
+                       One of fwd|rev|rand, default is rand\n\
+  -v                 - Verbose. Prints informative messages to stderr\n\
+  -V                 - Show version information\n\
+  -w <output-file>   - Read-Validate-Write - file is written to <output-file>\n\
+                       (sequential read only)\n\
 \n\
   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
          o All option arguments must be separated from the option by whitespace.\n\
-\n", PACKAGE, PACKAGE);
+\n", PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME);
 }
 
 enum MajorMode_t {
@@ -120,26 +123,27 @@ public:
   bool   verbose_flag;   // true if the verbose option was selected
   bool   version_flag;   // true if the version display option was selected
   bool   help_flag;      // true if the help display option was selected
-  const char* filename;  // filename to be processed
-  const char* write_filename;  // filename to write with val_write_flag
+  std::string filename;  // filename to be processed
+  std::string write_filename;  // filename to write with val_write_flag
   ui32_t chunk_count;
   MajorMode_t mode;      // MajorMode selector
 
   //
   CommandOptions(int argc, const char** argv) :
-    error_flag(true), order(0), verbose_flag(false), version_flag(false), help_flag(false),
-    filename(""), write_filename(""), chunk_count(0), mode(MMT_VALIDATE)
+    error_flag(true), order(""), verbose_flag(false),
+    version_flag(false), help_flag(false),
+    chunk_count(0), mode(MMT_VALIDATE)
   {
-    order = "rand";
+    //    order = "rand";
 
     for ( int i = 1; i < argc; i++ )
       {
 
-        if ( (strcmp( argv[i], "-help") == 0) )
-          {
-            help_flag = true;
-            continue;
-          }
+       if ( (strcmp( argv[i], "-help") == 0) )
+         {
+           help_flag = true;
+           continue;
+         }
      
        if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
          {
@@ -148,7 +152,7 @@ public:
              case 'c':
                mode = MMT_CREATE;
                TEST_EXTRA_ARG(i, 'c');
-               chunk_count = atoi(argv[i]);
+               chunk_count = Kumu::xabs(strtol(argv[i], 0, 10));
                break;
                
              case 'V': version_flag = true; break;
@@ -184,7 +188,7 @@ public:
          {
            if (argv[i][0] != '-' )
              {
-               if ( filename != "" )
+               if ( ! filename.empty() )
                  {
                    fprintf(stderr, "Extra filename found: %s\n", argv[i]);
                    return;
@@ -200,22 +204,31 @@ public:
          }
       }
     
-     if ( help_flag || version_flag )
-       return;
-  
-     if ( strlen ( filename ) == 0 )
+    if ( help_flag || version_flag )
+      return;
+    
+    if ( filename.empty() )
       {
        fprintf(stderr, "Filename required.\n");
        return;
       }
+    
+    if ( mode != MMT_VALIDATE && strcmp(order, "") != 0 )
+      {
+       fprintf(stderr, "-o option not valid with -c or -w options.\n");
+       return;
+      }
+    else
+      if ( strcmp(order, "") == 0 )
+       order = "rand";
 
-   if ( strcmp ( filename, write_filename ) == 0 )
-     {
-       fprintf(stderr, "Output and input files must be different.\n");
-       return;
-     }
-
-   error_flag = false;
+    if ( filename == write_filename )
+      {
+       fprintf(stderr, "Output and input files must be different.\n");
+       return;
+      }
+    
+    error_flag = false;
   }
 };
 
@@ -287,7 +300,7 @@ CreateLargeFile(CommandOptions& Options)
   FB.Capacity(Megabyte);
   assert(FB.Capacity() == Megabyte);
 
-  fprintf(stderr, "Writing %lu chunks:\n", Options.chunk_count);
+  fprintf(stderr, "Writing %u chunks:\n", Options.chunk_count);
   s_Nonce = Options.chunk_count;
   Result_t result = Writer.OpenWrite(Options.filename);
 
@@ -300,7 +313,7 @@ CreateLargeFile(CommandOptions& Options)
          CTR.FillRandom(FB.Data() + CTR.WriteSize(), Megabyte - CTR.WriteSize());
          result = Writer.Write(FB.RoData(), Megabyte, &write_count);
          assert(write_count == Megabyte);
-         fprintf(stderr, "\r%8lu ", ++write_total);
+         fprintf(stderr, "\r%8u ", ++write_total);
        }
     }
   
@@ -370,7 +383,7 @@ randomize_list(read_list_t* read_list, ui32_t check_total)
 Result_t
 ReadValidateWriteLargeFile(CommandOptions& Options)
 {
-  assert(Options.write_filename);
+  assert(!Options.write_filename.empty());
   ui32_t  check_total = 0;
   ui32_t  write_total = 0;
   ui32_t  read_count = 0;
@@ -399,7 +412,7 @@ ReadValidateWriteLargeFile(CommandOptions& Options)
        {
          if ( read_count < Megabyte )
            {
-             fprintf(stderr, "Read() returned short buffer: %lu\n", read_count);
+             fprintf(stderr, "Read() returned short buffer: %u\n", read_count);
              result = RESULT_FAIL;
            }
 
@@ -409,7 +422,7 @@ ReadValidateWriteLargeFile(CommandOptions& Options)
            {
              result = Writer.Write(FB.RoData(), Megabyte, &write_count);
              assert(write_count == Megabyte);
-             fprintf(stderr, "\r%8lu ", ++write_total);
+             fprintf(stderr, "\r%8u ", ++write_total);
            }
        }
       else if ( result == RESULT_ENDOFFILE )
@@ -449,7 +462,7 @@ ValidateLargeFile(CommandOptions& Options)
 
       if ( read_count < Megabyte )
        {
-         fprintf(stderr, "Read() returned short buffer: %lu\n", read_count);
+         fprintf(stderr, "Read() returned short buffer: %u\n", read_count);
          result = RESULT_FAIL;
        }
       else if ( KM_SUCCESS(result) )
@@ -457,7 +470,7 @@ ValidateLargeFile(CommandOptions& Options)
 
       if ( KM_SUCCESS(result) )
        {
-         fprintf(stderr, "Validating %lu chunk%s in %s order:\n",
+         fprintf(stderr, "Validating %u chunk%s in %s order:\n",
                  check_total, (check_total == 1 ? "" : "s"), Options.order);
          assert(read_list == 0);
          read_list = (read_list_t*)malloc(check_total * sizeof(read_list_t));
@@ -498,7 +511,7 @@ ValidateLargeFile(CommandOptions& Options)
            read_list_i < check_total && KM_SUCCESS(result);
            read_list_i++ )
        {
-         fprintf(stderr, "\r%8lu [%8lu] ", read_list_i+1, read_list[read_list_i]);
+         fprintf(stderr, "\r%8u [%8u] ", read_list_i+1, read_list[read_list_i].nonce);
          result = Reader.Seek(read_list[read_list_i].position);
 
          if ( KM_SUCCESS(result) )
@@ -509,7 +522,7 @@ ValidateLargeFile(CommandOptions& Options)
 
          else if ( read_count < Megabyte )
            {
-             fprintf(stderr, "Read() returned short buffer: %lu\n", read_count);
+             fprintf(stderr, "Read() returned short buffer: %u\n", read_count);
              result = RESULT_FAIL;
            }
          else if ( KM_SUCCESS(result) )
@@ -518,7 +531,7 @@ ValidateLargeFile(CommandOptions& Options)
              
              if ( nonce != read_list[read_list_i].nonce )
                {
-                 fprintf(stderr, "Nonce mismatch: expecting %lu, got %lu\n",
+                 fprintf(stderr, "Nonce mismatch: expecting %u, got %u\n",
                          nonce, read_list[read_list_i].nonce);
 
                  return RESULT_FAIL;
@@ -535,7 +548,7 @@ ValidateLargeFile(CommandOptions& Options)
        result = RESULT_OK;
       else
        {
-         fprintf(stderr, "Unexpected chunk count, got %lu, wanted %lu\n",
+         fprintf(stderr, "Unexpected chunk count, got %u, wanted %u\n",
                  read_list_i, check_total);
          result = RESULT_FAIL;
        }
@@ -562,7 +575,7 @@ main(int argc, const char **argv)
 
   if ( Options.error_flag )
     {
-      fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
+      fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
       return 3;
     }