Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/home/vlg/develop/gwavmerger/src/NameUtils.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                              NameUtils.cpp
00004 //------------------------------------------------------------------------------
00005 // $Id: NameUtils.cpp,v 1.6 2003/02/07 05:15:10 vlg Exp $
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2002 by Vladislav Grinchenko
00008 //
00009 //  This program is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU General Public License
00011 //  as published by the Free Software Foundation; either version
00012 //  2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 // 06/28/2002 VLG  Created
00015 //------------------------------------------------------------------------------
00016 
00017 #include <stdio.h>
00018 #include <fstream>
00019 #include <sstream>
00020 using std::ostringstream;
00021 using std::ifstream;
00022 using std::ofstream;
00023 using std::ios;
00024 
00025 #include "gWavMerger-main.h"
00026 #include "NameUtils.h"
00027 
00028 /*******************************************************************************
00029  Local defines
00030 *******************************************************************************/
00031 
00032 /*******************************************************************************
00033  Parse format from fully qualified sequence of names. A fully quialified
00034  name is of the format:
00035 
00036     <prefix><number>.wav
00037 
00038  The sequence might have "gap" entires which are ignored.
00039  If <number> is not in uniform format, 00..0x, then the most generic
00040  "%d" is returned.
00041 
00042  For example, ru_034.wav is a fully qualified name with
00043  
00044      <prefix>="ru_"
00045      <number>="034" 
00046 
00047  The printf format of this name is "%03d".
00048 
00049  Return: On success, printf()-like format is returned.
00050 
00051          If failed, an empty string is returned. An empty string might result
00052          in case if not all names in sequence have the same prefix.
00053 *******************************************************************************/
00054 string
00055 
00056 nameutils::parse_format (const string& prefix_, vector<string>& vs_)
00057 {
00058     static const char self[]="nameutils::parse_format";
00059     trace(self);
00060 
00061     string ret;
00062     string name;
00063     int i;
00064     int len;
00065     bool is_varlen = false;
00066     bool is_numeric = true;
00067     i = len = 0;
00068 
00069     vector<string>::const_iterator cit = vs_.begin ();
00070 
00071     while (cit != vs_.end ()) {
00072         DL((TRACE,"---------------------\n"));
00073         DL((TRACE,"Parsing \"%s\"\n", (*cit).c_str ()));
00074 
00075         if (*cit != "gap") {
00076             //          name = parse_seq_number (*cit, prefix_);
00077             name = *cit;
00078             // DL((TRACE,"parse_seq_number () = \"%s\"\n", name.c_str ()));
00079             if (name.length () == 0) {
00080                 return ret;
00081             }
00082             i = strtol (name.c_str (), (char **) NULL, 10);
00083             DL((TRACE,"Parsed number %d < %d < %d\n",LONG_MIN,i,LONG_MAX));
00084             if (i == LONG_MIN || i == LONG_MAX) {
00085                 DL((TRACE,"Set is_numeric = false\n"));
00086                 is_numeric = false;
00087                 break;
00088             }
00089             if (len == 0) {
00090                 len = name.length ();
00091                 DL((TRACE,"Set len = %d\n", len));
00092             }
00093             if (name.length () != len) {
00094                 DL((TRACE,"Set is_varlen = true (%d != %d)\n", 
00095                     name.length (), len));
00096                 is_varlen = true;
00097                 len = max2 (len, name.length ());
00098                 DL((TRACE,"Set len = %d\n", len));
00099             }
00100         }
00101         cit++;
00102     }
00103     DL((TRACE,"is_numeric = %s, is_varlen = %s\n", is_numeric?"true":"false",
00104         is_varlen?"true":"false"));
00105 
00106     if (is_numeric == true) {
00107         if (is_varlen == false) {
00108             ostringstream os;
00109             os << "%0" << len << "d";
00110             ret = os.str ();
00111         }
00112         else {
00113             ret = "%d";
00114         }
00115     }
00116     DL((TRACE,"Parsed format: \"%s\"\n", ret.c_str ()));
00117     return ret;
00118 }
00119 
00120 /*******************************************************************************
00121  Extract the sequence number from a file name. A file name is of the format:
00122 
00123     <prefix><seq_number>.wav
00124 
00125  For example, ru_034.wav is a fully qualified name with
00126  
00127      <prefix>="ru_"
00128      <seq_number>="034" 
00129 
00130  Return: parsed <seq_number> if success; 
00131          empty string if failed
00132 *******************************************************************************/
00133 string
00134 
00135 nameutils::parse_seq_number (const string& name_, const string& prefix_)
00136 {
00137     static const char self[]="nameutils::parse_seq_number";
00138     trace(self);
00139 
00140     string s (name_);
00141     string::size_type idx = string::npos;
00142 
00143     DL((TRACE,"Parsing(0) \"%s\", prefix_ = \"%s\"\n", s.c_str (),
00144         prefix_.c_str ()));
00145 
00146     if ((idx = s.find (prefix_)) == 0) {
00147         s.erase (0, prefix_.length ());
00148         DL((TRACE,"Parsing(1) \"%s\"\n", s.c_str ()));
00149         idx = s.find ('.');
00150         if (idx != string::npos) {
00151             if (s.substr (idx) == ".wav") {
00152                 s.replace (idx, name_.size (), "");
00153                 DL((TRACE,"Parsing(2) \"%s\"\n", s.c_str ()));
00154                 return s;
00155             }
00156         }
00157     }
00158     return ("");
00159 }
00160 
00161 
00162 /*******************************************************************************
00163  Format sequence number according to the supplied format.
00164  For example, if seqnum = 34 and format "%03d" the result will be 034.
00165 
00166  Return: formatted sequence number.
00167 *******************************************************************************/
00168 string
00169 
00170 nameutils::format_seqnum (int seqnum_, const string& fmt_)
00171 {
00172     char buf [PATH_MAX];
00173 
00174     snprintf (buf, PATH_MAX, fmt_.c_str (), seqnum_);
00175     return string (buf);
00176 }
00177 
00178 /*******************************************************************************
00179  Take fully qualified file name in form <prefix><seq_number>.wav and make a 
00180  fully qualified name of the file with offset
00181     <path>/<prefix><seq_number+offset>.wav
00182 
00183  Return: fully qualified name or an empty string if parsing sequence number
00184          failed.
00185 *******************************************************************************/
00186 string
00187 
00188 nameutils::make_name_with_offset (const string& name_, const string& fmt_,
00189                        const string& prefix_, const string& dirpath_, 
00190                        int offset_)
00191 {
00192     static const char self[]="nameutils::make_name_with_offset";
00193     trace(self);
00194 
00195     string s (name_);
00196     string::size_type idx;
00197 
00198     DL((TRACE,"\n\tname_=\"%s\"\n\tfmt_=\"%s\"\n\tprefix_=\"%s\"\n",
00199         name_.c_str (), fmt_.c_str(), prefix_.c_str ()));
00200     
00201     if ((idx = s.find (dirpath_)) == 0) {
00202         s.erase (0, dirpath_.length ()+1);
00203     }
00204     s = parse_seq_number (s, prefix_);
00205 
00206     if (s.length () == 0) {
00207         return s;
00208     }
00209     return (dirpath_ + "/" + prefix_ 
00210             + format_seqnum (atoi (s.c_str ()) + offset_, fmt_) 
00211             + string (".wav"));
00212 }
00213 
00214 /*******************************************************************************
00215  Take file name in form <path>/<prefix><seq_number>.wav and make a fully 
00216  qualified name of the file that follows: <path>/<prefix><seq_number-1>.wav
00217 
00218  Return: fully qualified name or an empty string if parsing sequence number
00219          failed.
00220 *******************************************************************************/
00221 string
00222 
00223 nameutils::make_prev_name (const string& name_, const string& fmt_,
00224                 const string& prefix_, const string& dirpath_)
00225 {
00226     return make_name_with_offset (name_, fmt_, prefix_, dirpath_, -1);
00227 }
00228 
00229 /*******************************************************************************
00230  Take file name in form <path>/<prefix><seq_number>.wav and make a fully 
00231  qualified name of the file that follows: <path>/<prefix><seq_number+1>.wav
00232 
00233  Return: fully qualified name or an empty string if parsing sequence number
00234          failed.
00235 *******************************************************************************/
00236 string
00237 
00238 nameutils::make_next_name (const string& name_, const string& fmt_,
00239                 const string& prefix_, const string& dirpath_)
00240 {
00241     return make_name_with_offset (name_, fmt_, prefix_, dirpath_, +1);
00242 }
00243 
00244 
00245 /*******************************************************************************
00246  Fast file copy.
00247 
00248  RETURN: 0 on success; -1 if either source or destination cannot be opened.
00249 *******************************************************************************/
00250 int
00251 
00252 nameutils::fast_copy (const string& src_, const string& dest_)
00253 {
00254     ifstream source (src_.c_str (), std::ios::in);
00255     ofstream destination (dest_.c_str (), std::ios::out);
00256 
00257     if (!(source.good () || destination.good ())) {
00258         return -1;
00259     }
00260 
00261     destination << source.rdbuf();
00262     source.close ();
00263     destination.close ();
00264     return 0;
00265 }
00266     

Generated on Tue Feb 11 23:05:19 2003 for gwavmerger by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002