00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
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
00077 name = *cit;
00078
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
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
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
00164
00165
00166
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
00180
00181
00182
00183
00184
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
00216
00217
00218
00219
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
00231
00232
00233
00234
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
00247
00248
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