r3830: *** empty log message ***
authorKevin M. Rosenberg <kevin@rosenberg.net>
Tue, 21 Jan 2003 07:37:13 +0000 (07:37 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Tue, 21 Jan 2003 07:37:13 +0000 (07:37 +0000)
Makefile
wdq2wav.cpp
wdq2wav.h

index 7b647d2575dfa2f4070335261981044ea2d7c02e..f0bf2d64385eb371851e1f23b47c82b917131243 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,9 @@ clean:
 
 compile: wdq2wav
 
+debug:
+       g++ -g -I. $(base).cpp -o $(base)
+
 wdq2wav: wdq2wav.cpp wdq2wav.h
        g++ -O2 -I. $(base).cpp -o $(base)
 
index 7a57e9181ddad913ac3674501b30fc4194e59d5e..3fbe18f75723b0a175ba35c467db5d4b118c5a36 100644 (file)
@@ -8,15 +8,7 @@
 **
 **  Copyright (c) 2003 Kevin Rosenberg
 **
-**  NOTES
-**   Quick & Dirty hack, but efficient and good error checking
-**   Allocates memory equal to the size of channel (2 * number of samples)
-**
-**  BUGS
-**   Need command line options for verbose and debug output
-**   Should comment the reading and writing of files to document the file formats
-**
-**  $Id: wdq2wav.cpp,v 1.7 2003/01/21 04:19:44 kevin Exp $
+**  $Id: wdq2wav.cpp,v 1.8 2003/01/21 07:37:13 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 
 #include <wdq2wav.h>
 
-static bool g_verbose = true;
+enum { O_VERBOSE, O_QUIET, O_DEBUG, O_HELP, O_VERSION };
+
+static struct option my_options[] = 
+{
+  {"verbose", 0, 0, O_VERBOSE},
+  {"quiet", 0, 0, O_QUIET},
+  {"debug", 0, 0, O_DEBUG},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+static const char* g_szIdStr = "$Id: wdq2wav.cpp,v 1.8 2003/01/21 07:37:13 kevin Exp $";
+
+static bool g_quiet = false;
+static bool g_verbose = false;
 static bool g_debug = false;
 
+#define MAX_INPUT_STR 256
+
 void
 error_msg (const char *msg)
 {
-  fprintf (stderr, "%s\n",msg);
+  std::cerr << msg << "\n";
 }
 
 void
 info_msg (const char* msg)
 {
-  fprintf (stdout, "%s\n", msg);
+  std::cout << msg << "\n";
 }
 
 void
 info_msg_sans_newline (const char* msg)
 {
-  fprintf (stdout, "%s", msg);
+  std::cout << msg;
 }
 
+const char*
+fileBasename (const char* filename)
+{
+  const char* p = strrchr (filename, '/');
+  return (p ? p + 1 : filename);
+}
+
+char *
+str_rm_tail (char *str, const char* const charlist)
+{
+  int i;
+
+  for (i = strlen(str) - 1; i >= 0; i--)
+    if (strchr (charlist, str[i]) != NULL)
+      str[i] = 0;
+    else
+      break;            /* found non-specified char, all done */
+
+  return (str);
+}
+
+char *
+str_wrm_tail (char *str)
+{
+  return (str_rm_tail(str, "\b\t\n\r"));
+}
+
+
 void
-usage ()
+usage (const char* progname)
 {
-  error_msg ("usage: wdq2wav <wdq-file> <channel-number> <wav-file>");
+  std::cout << "usage: " << fileBasename (progname) << " <wdq-file> <channel-number> <wav-file>\n";
+  std::cout << "     --quiet     Supress all messages\n";
+  std::cout << "     --verbose   Verbose mode\n";
+  std::cout << "     --debug     Debug mode\n";
+  std::cout << "     --version   Print version\n";
+  std::cout << "     --help      Print this help message\n";
 }
 
 bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname);
@@ -66,28 +108,78 @@ bool wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname);
 int
 main (int argc, char *argv[])
 {
-  if (argc != 4) {
-    usage();
-    exit(1);
-  }
-
-  char *wdq_fname = argv[1];
-  char *channel_endptr;
-  int channel = (int) strtol (argv[2], &channel_endptr, 10);
-  char *wav_fname = argv[3];
+    while (1) {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      if (c == -1)
+        break;
+      
+      switch (c) {
+      case O_VERSION:
+        std::cout << "Version " << g_szIdStr << std::endl;
+        break;
+      case O_QUIET:
+        g_quiet = true;
+        break;
+      case O_VERBOSE:
+        g_verbose = true;
+        break;
+      case O_DEBUG:
+        g_debug = true;
+        break;
+      case O_HELP:
+      case '?':
+        usage(argv[0]);
+        return (0);
+      default:
+        usage(argv[0]);
+        return (1);
+      }
+    }
+    
+    if (optind + 3 < argc) {
+      std::cerr << "Too many parameters\n";
+      usage (argv[0]);
+      return (1);
+    }
 
-  if (*channel_endptr != 0) {
-    std::ostringstream os;
-    os << "Error: Channel " << argv[2] << " is not an integer";
-    error_msg (os.str().c_str());
-    usage();
-    return(1);
-  }
+    char wdq_fname[MAX_INPUT_STR];
+    if (optind < argc)
+      strncpy (wdq_fname, argv [optind], MAX_INPUT_STR);
+    else {
+      std::cout << "Enter input WinDAQ filename: ";
+      std::cin.getline (wdq_fname, MAX_INPUT_STR);
+    }
 
-  if (! wdq2wav (wdq_fname, channel, wav_fname))
-    return 1;
+    char channel_buf [MAX_INPUT_STR];
+    if (optind + 1 < argc)
+      strncpy (channel_buf, argv[optind+1], MAX_INPUT_STR);
+    else {
+      std::cout << "Enter channel number: ";
+      std::cin.getline (channel_buf, MAX_INPUT_STR);
+    }
+    
+    char *channel_endptr;
+    int channel = static_cast<int>(strtol (channel_buf, &channel_endptr, 10));
+    if (*channel_endptr != 0) {
+      std::ostringstream os;
+      os << "Error: Channel " << channel_buf << " is not an integer";
+      error_msg (os.str().c_str());
+      usage (argv[0]);
+      return (1);
+    }
 
-  return 0;
+    char wav_fname[MAX_INPUT_STR];
+    if (optind + 2 < argc)
+      strncpy (wav_fname, argv[optind+2], MAX_INPUT_STR);
+    else {
+      std::cout << "Enter output wav filename: ";
+      std::cin.getline (wav_fname, MAX_INPUT_STR);
+    }
+      
+    if (! wdq2wav (wdq_fname, channel, wav_fname))
+      return 1;
+    
+    return 0;
 }
 
 bool
@@ -96,9 +188,18 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
   WindaqFile wdq (wdq_fname);
 
   if (! wdq.ReadHeader()) {
+    if (wdq.m_error.size()) {
+      std::ostringstream os;
+      os << "Error reading file " << wdq_fname << ": " << wdq.m_error.c_str();
+      error_msg (os.str().c_str());
+    } else {
+      std::ostringstream os;
+      os << "Error reading file " << wdq_fname;
+      error_msg (os.str().c_str());
+    }
     return false;
   }
-  if (g_verbose) {
+  if (! g_quiet || g_verbose || g_debug) {
     std::ostringstream os1;
     os1 << "File " << wdq_fname;
     info_msg (os1.str().c_str());
@@ -125,7 +226,7 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
     return false;
   }
 
-  if (g_verbose) {
+  if (! g_quiet || g_verbose || g_debug) {
     std::ostringstream os1;
     os1 << "Channel " << channel;
     info_msg (os1.str().c_str());
@@ -136,6 +237,8 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname)
     os3 << "  Raw minimum: " << wdq_channel.m_min_raw_data <<
       ", maximum: " << wdq_channel.m_max_raw_data;
     info_msg (os3.str().c_str());
+  }
+  if (g_debug) {
     std::ostringstream os4;
     os4 << "  Scaled minimum: " << wdq_channel.m_min_scaled_data <<
       ", maximum: " << wdq_channel.m_max_scaled_data;
@@ -181,67 +284,61 @@ WindaqFile::ReadHeader ()
   unsigned int tmp4;
 
   m_valid = false;
-  if ((m_fd = open (m_strFile.c_str(), O_RDONLY)) == 0)
+  if ((m_fd = open (m_strFile.c_str(), O_RDONLY)) == 0) {
+    m_error = "Unable to open file";
     return false;
+  }
 
   lseek (0, 0, SEEK_SET);
   if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
-    error_msg ("Error reading file");
+    m_error = "Unable to read beginning of file";
     return false;
   }
   m_nChannels = tmp2 & 0x1f;
   m_sr_denom = (tmp2 & 0x7fff) >> 5;
   m_sr_numer = (tmp2 & 0x8000) << 1;
   
-  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2))
     return false;
-  }
+
   m_sr_numer |= tmp2;
   m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels);
 
-  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2))
     return false;
-  }
+
   m_channel_offset = tmp2 & 0xFF;
   m_nBytes_channel_header = tmp2 >> 8;
   
-  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2))
     return false;
-  }
+
   m_nHeader_bytes = tmp2;
   
-  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4))
     return false;
-  }
+
   m_nData_bytes = tmp4;
   m_nSamples = (m_nData_bytes / m_nChannels) / 2;
 
   lseek (m_fd, 36, SEEK_SET);
-  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4))
     return false;
-  }
+
   m_time_acq_start = tmp4;
-  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp4, sizeof(tmp4)) != sizeof(tmp4))
     return false;
-  }
+
   m_time_acq_stop = tmp4;
   
   // Verify Windaq signature
   lseek (m_fd, m_nHeader_bytes - 2, SEEK_SET);
-  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2)) {
-    error_msg ("Error reading file");
+  if (read (m_fd, &tmp2, sizeof(tmp2)) != sizeof(tmp2))
     return false;
-  }
+
   if (tmp2 != 0x8001) {
     std::ostringstream os;
-    os << m_strFile << " is not a WinDAQ file";
-    error_msg (os.str().c_str());
+    m_error = "File is not a valid WinDAQ file";
     return false;
   }
 
@@ -365,8 +462,11 @@ WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname)
       data_scale = 65535. / (wdq_channel.m_max_scaled_data -
                             wdq_channel.m_min_scaled_data);
     
-    if (g_debug)
-      printf ("data_scale: %f, data_offset: %f\n", data_scale, data_offset);
+    if (g_debug) {
+      std::ostringstream os;
+      os << "  Wav data_scale: " << data_scale << ", data_offset: " << data_offset;
+      info_msg (os.str().c_str());
+    }
     
     signed short int* input = wdq_channel.m_data;
     double slope = wdq_channel.m_slope;
index fb8d0e00809c98f8741c67de225407de2c347a5b..e4d261ff51124798d1472fe657104bf056073e65 100644 (file)
--- a/wdq2wav.h
+++ b/wdq2wav.h
@@ -1,11 +1,38 @@
-#include <stdio.h>
+/*****************************************************************************
+** FILE IDENTIFICATION
+**
+**   Name:          wdq2wav.h
+**   Purpose:       Header file for wdq2wav.cpp
+**   Programmer:    Kevin Rosenberg <kevin@rosenberg.net>
+**   Date Started:  Jan 2003
+**
+**  Copyright (c) 2003 Kevin Rosenberg
+**
+**  $Id: wdq2wav.h,v 1.5 2003/01/21 07:37:13 kevin Exp $
+**
+**  This program is free software; you can redistribute it and/or modify
+**  it under the terms of the GNU General Public License (version 2) as
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+******************************************************************************/
+
+#include <iostream>
+#include <cstdio>
+#include <cstring>
 #include <string>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <sstream>
+#include <ctime>
 #include <fcntl.h>
 #include <unistd.h>
-#include <sstream>
-#include <time.h>
+#include <getopt.h>
 
 class WindaqFile
 {
@@ -16,6 +43,7 @@ public:
 
   std::string m_strFile;
   bool m_valid;
+  std::string m_error;
   int m_fd;
   unsigned int m_nChannels;
   unsigned int m_nSamples;