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

/home/vlg/develop/gwavmerger/tests/test.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                          str_parse_test.cpp
00004 //------------------------------------------------------------------------------
00005 // $Id: test.cpp,v 1.1 2003/02/07 05:15:10 vlg Exp $
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2001,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 
00015 /*******************************************************************************
00016  Parse format from a fully qualified sequence of names. A fully quialified
00017  name is of the format:
00018 
00019     <prefix><number>.wav
00020 
00021  The sequence might have "gap" entires which are ignored.
00022 
00023  For example, ru_034.wav is a fully qualified name with
00024  
00025      <prefix>="ru_"
00026      <number>="034" 
00027 
00028  The printf format of this name is "%03d".
00029 
00030  Return: format if successful; If format is not uniform, but otherwise
00031          valid, "%d" is returned.
00032 
00033          empty string if failed. An empty string is resulted in
00034          case if not all names in sequence have the same prefix.
00035 
00036 *******************************************************************************/
00037 string
00038 parse_format (const string& prefix_, vector<string>& vs_)
00039 {
00040     string ret;
00041     int i = 0;
00042     bool is_varlen = false;
00043     int len = 0;
00044     bool is_numeric = true;
00045     string name;
00046 
00047     vector<string>::const_iterator cit = vs_.begin ();
00048     name = parse_seq_number (*cit, prefix_);
00049 
00050     if ((len = name.length ()) == 0) {
00051         return ret;
00052     }
00053 
00054     while (cit != vs_.end ()) {
00055         if (*cit != "gap") {
00056             name = parse_seq_number (*cit, prefix_);
00057             if (name.length () == 0) {
00058                 return ret;
00059             }
00060             i = strtol (name.c_str (), (char **) NULL, 10);
00061             if (i == LONG_MIN || i == LONG_MAX) {
00062                 is_numeric = false;
00063                 break;
00064             }
00065             if (name.length () != len) {
00066                 is_varlen = true;
00067             }
00068         }
00069         cit++;
00070     }
00071     if (is_numeric) {
00072         if (!is_varlen) {
00073             ostringstream os;
00074             os << "%0" << len << "d";
00075             ret = os.str ();
00076         }
00077         else {
00078             ret = "%d";
00079         }
00080     }
00081     return ret;
00082 }
00083 
00084 /*******************************************************************************
00085  Extract the sequence number from a file name. A file name is of the format:
00086 
00087     <prefix><seq_number>.wav
00088 
00089  For example, ru_034.wav is a fully qualified name with
00090  
00091      <prefix>="ru_"
00092      <seq_number>="034" 
00093 
00094  Return: parsed <seq_number> if success; 
00095          empty string if failed
00096 *******************************************************************************/
00097 string
00098 parse_seq_number (const string& name_, const string& prefix_)
00099 {
00100     string s (name_);
00101     string::size_type idx = string::npos;
00102 
00103     if ((idx = s.find (prefix_)) == 0) {
00104         s.erase (0, prefix_.length ());
00105         idx = s.find ('.');
00106         if (idx != string::npos) {
00107             if (s.substr (idx) == ".wav") {
00108                 s.replace (idx, name_.size (), "");
00109                 return s;
00110             }
00111         }
00112     }
00113     return ("");
00114 }
00115 
00116 
00117 /*******************************************************************************
00118  Format sequence number according to the supplied format.
00119  For example, if seqnum = 34 and format "%03d" the result will be 034.
00120 
00121  Return: formatted sequence number.
00122 *******************************************************************************/
00123 string
00124 format_seqnum (int seqnum_, const string& fmt_)
00125 {
00126     char buf [PATH_MAX];
00127 
00128     snprintf (buf, PATH_MAX, fmt_.c_str (), seqnum_);
00129     return string (buf);
00130 }
00131 
00132 /*******************************************************************************
00133  Take fully qualified file name in form <prefix><seq_number>.wav and make a 
00134  fully qualified name of the file with offset
00135     <path>/<prefix><seq_number+offset>.wav
00136 
00137  Return: fully qualified name or an empty string if parsing sequence number
00138          failed.
00139 *******************************************************************************/
00140 string
00141 make_name_with_offset (const string& name_, const string& fmt_,
00142                        const string& prefix_, const string& dirpath_, 
00143                        int offset_)
00144 {
00145     string s (name_);
00146     string::size_type idx;
00147 
00148     cout << '\n';
00149     s = parse_seq_number (s, prefix_);
00150     cout << "\tparsed sequence number: " << s << '\n';
00151     if (s.length () == 0) {
00152         return s;
00153     }
00154     return (dirpath_ + prefix_ 
00155             + format_seqnum (atoi (s.c_str ()) + offset_, fmt_) 
00156             + string (".wav"));
00157 }
00158 
00159 /*******************************************************************************
00160  Take file name in form <path>/<prefix><seq_number>.wav and make a fully 
00161  qualified name of the file that follows: <path>/<prefix><seq_number-1>.wav
00162 
00163  Return: fully qualified name or an empty string if parsing sequence number
00164          failed.
00165 *******************************************************************************/
00166 string
00167 make_prev_name (const string& name_, const string& fmt_,
00168                 const string& prefix_, const string& dirpath_)
00169 {
00170     return make_name_with_offset (name_, fmt_, prefix_, dirpath_, -1);
00171 }
00172 
00173 /*******************************************************************************
00174  Take file name in form <path>/<prefix><seq_number>.wav and make a fully 
00175  qualified name of the file that follows: <path>/<prefix><seq_number+1>.wav
00176 
00177  Return: fully qualified name or an empty string if parsing sequence number
00178          failed.
00179 *******************************************************************************/
00180 string
00181 make_next_name (const string& name_, const string& fmt_,
00182                 const string& prefix_, const string& dirpath_)
00183 {
00184     return make_name_with_offset (name_, fmt_, prefix_, dirpath_, +1);
00185 }
00186     
00187 /******************************************************************************/
00188 
00189 static const char* list[]={ 
00190     "ru_001.wav", "ru_002.wav", "ru_003.wav", 
00191     "ru_004.wav", "ru_005.wav", NULL 
00192 };
00193 
00194 static const char* list_non_uniform_format[]={ 
00195     "ru_001.wav", "ru_2.wav", "ru_003.wav", 
00196     "ru_004.wav", "ru_005.wav", NULL 
00197 };
00198 
00199 static const char* list_with_gaps []={ 
00200     "ru_001.wav", "gap", "ru_003.wav", 
00201     "ru_004.wav", "gap", "ru_006.wav", NULL 
00202 };
00203 
00204 static const char* list_bad_format []={ 
00205     "ru_001.wav", "en_002.wav", "ru_003.wav", 
00206     "ru_004.wav", "", "ru_006.wav", NULL 
00207 };
00208 
00209 /******************************************************************************/
00210 
00211 int 
00212 main()
00213 {
00214     int i;
00215     string format;
00216     vector<string> v;
00217 
00218     cout << '\n' << "Testing parse_format() ...";
00219 
00220     i = 0;
00221     while (list [i]) { v.push_back (list [i++]); }
00222 
00223     format = parse_format ("ru_", v);
00224     if (format != "%03d") {
00225         cout << "failed valid list\n";
00226         cout << "(expected \"%03\", returned \"" << format << "\"\n\n";
00227         return 1;
00228     }
00229     
00230     v.clear ();
00231     i = 0;
00232     while (list_with_gaps [i]) { v.push_back (list_with_gaps [i++]); }
00233 
00234     format = parse_format ("ru_", v);
00235     if (format != "%03d") {
00236         cout << "failed list with gaps\n";
00237         return 1;
00238     }
00239 
00240     v.clear ();
00241     i = 0;
00242     while (list_non_uniform_format [i]) { 
00243         v.push_back (list_non_uniform_format [i++]); 
00244     }
00245 
00246     format = parse_format ("ru_", v);
00247     if (format != "%d") {
00248         cout << "failed list with non-uniform format\n";
00249         cout << "(expected \"\", returned \"" << format << "\"\n\n";
00250         return 1;
00251     }
00252     v.clear ();
00253 
00254     i = 0;
00255     while (list_bad_format [i]) { 
00256         v.push_back (list_bad_format [i++]); 
00257     }
00258 
00259     format = parse_format ("ru_", v);
00260     if (format != "") {
00261         cout << "failed list with bad format\n";
00262         cout << "(expected \"\", returned \"" << format << "\"\n\n";
00263         return 1;
00264     }
00265     cout << "ok\n";
00266 
00267     cout << "\nTesting parse_seq_num() ";
00268     string res;
00269     string name;
00270     name = "ru_00034.wav";
00271     res = parse_seq_number (name, "ru_");
00272     if (res != "00034") {
00273         cout << "failed (expected \"00034\", returned \"" << res << "\"\n\n";
00274         return 1;
00275     }
00276     res = parse_seq_number (name, "en_");
00277     if (res != "") {
00278         cout << "failed (expected \"\", returned \"" << res << "\"\n\n";
00279         return 1;
00280     }
00281     name = "ru_0999";
00282     res = parse_seq_number (name, "ru_");
00283     if (res != "") {
00284         cout << "failed (expected \"\", returned \"" << res << "\"\n\n";
00285         return 1;
00286     }
00287     name = "ru_0999.wav";
00288     res = parse_seq_number (name, "ru_");
00289     if (res != "0999") {
00290         cout << "failed (expected \"0999\", returned \"" << res << "\"\n\n";
00291         return 1;
00292     }
00293     cout << "ok\n";
00294 
00295     cout << "\nTesting format_seqnum () ";
00296     res = format_seqnum (54, "%03d");
00297     if (res != "054") {
00298         cout << "failed (expected \"054\", returned \"" << res << "\"\n\n";
00299         return 1;
00300     }
00301 
00302     res = format_seqnum (54, "%d");
00303     if (res != "54") {
00304         cout << "failed (expected \"54\", returned \"" << res << "\"\n\n";
00305         return 1;
00306     }
00307     cout << "ok\n";
00308 
00309     cout << "\nTesting make_name_with_offset () ";
00310     string dirpath ("/English/Test/");
00311     format = "%04d";
00312     name = "ru_0999.wav";
00313     res = make_name_with_offset (name, format, "ru_", dirpath, 0);
00314 
00315     if (res != dirpath + "ru_0999.wav") {
00316         cout << "failed\n\t(expected \"/English/Test/ru_0999.wav\""
00317              << ", returned \"" << res << "\")\n\n";
00318         return 1;
00319     }
00320     cout << "ok\n";
00321 
00322     cout << "\nTesting make_prev_name () ";
00323     res = make_prev_name (name, format, "ru_", dirpath);
00324     if (res != dirpath + "ru_0998.wav") {
00325         cout << "failed\n\t(expected \"/English/Test/ru_0998.wav\""
00326              << ", returned \"" << res << "\")\n\n";
00327         return 1;
00328     }
00329     res.erase (0, dirpath.length ());
00330     name = res;
00331     res = make_prev_name (name, format, "ru_", dirpath);
00332     if (res != dirpath + "ru_0997.wav") {
00333         cout << "failed\n\t(expected \"/English/Test/ru_0997.wav\""
00334              << ", returned \"" << res << "\")\n\n";
00335         return 1;
00336     }
00337     cout << "ok\n";
00338 
00339     cout << "\n";
00340 }

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