cWebshotsMisc.cpp

Go to the documentation of this file.
00001 /*
00002     cWebshots - Webshots files handling classes
00003     Copyright (C) 2005  Hervé "Setaou" BRY <uwc at apinc dot org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 
00026 #include "cWebshots.h"
00027 
00028 #include <cstdio>
00029 #include <sstream>
00030 
00031 using namespace std;
00032 
00033 namespace cWebshots
00034 {
00035 
00044         string GetFileName(string path)
00045         {
00046                 size_t start = path.rfind("\\") + 1;
00047                 return path.substr(start,path.length() - start);
00048         }
00049 
00058         string GetDirectory(string path)
00059         {
00060                 return path.substr(0,path.rfind("\\") + 1);
00061         }
00062 
00070         Webshots_Formats IdentifyFile(string File)
00071         {
00072                 ifstream in_file;
00073 
00074                 // Open the file
00075                 in_file.open(File.c_str(), ios_base::in | ios_base::binary);
00076 
00077                 if (in_file.is_open())
00078                 {
00079                         return IdentifyFile(in_file);
00080                 }
00081                 else
00082                 {
00083                         Exceptions::FileSystemError Err("Unable to open the file \"" + File + "\" for reading.");
00084                         throw Err;
00085                 }
00086         }
00087 
00095         Webshots_Formats IdentifyFile(ifstream &in_file)
00096         {
00097                 long File_Type;
00098                 Webshots_Formats Type;
00099 
00100                 // Read the first 4 bytes of the file
00101                 in_file.seekg(0);
00102                 in_file.read((char *)&File_Type, 4);
00103 
00104                 switch (File_Type)
00105                 {
00106                 // WBC file
00107                 case WBC_File_Marker:
00108                         Type = WBC;
00109                         break;
00110 
00111                 // WBZ file
00112                 case WBZ_File_Marker:
00113                         Type = WBZ;
00114                         break;
00115 
00116                 // JPG File / WB1 File
00117                 case WB1_File_Marker:
00118                 case JFIF_File_Marker: 
00119                 case EXIF_File_Marker:
00120                         Type = WB1;
00121                         break;
00122 
00123                 // Unknown file format
00124                 default:
00125                         Type = Unknown;
00126                 }
00127 
00128                 return Type;
00129         }
00130 
00131 
00139         void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ")
00140         {
00141                 // Skip delimiters at beginning.
00142                 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00143                 // Find first "non-delimiter".
00144                 string::size_type pos = str.find_first_of(delimiters, lastPos);
00145 
00146                 while (string::npos != pos || string::npos != lastPos)
00147                 {
00148                         // Found a token, add it to the vector.
00149                         tokens.push_back(str.substr(lastPos, pos - lastPos));
00150                         // Skip delimiters.  Note the "not_of"
00151                         lastPos = str.find_first_not_of(delimiters, pos);
00152                         // Find next "non-delimiter"
00153                         pos = str.find_first_of(delimiters, lastPos);
00154                 }
00155         }
00156 
00168         string GetINI(const string &Data, const string &Section, const string &Field)
00169         {
00170                 vector<string> Lines;
00171 
00172                 // Fill the vector Lines with each line of Data
00173                 if (Data.find("\r\n") != string::npos)     // \r\n = Windows line break
00174                         Tokenize(Data, Lines, "\r\n");
00175                 else if (Data.find("\n") == string::npos)  // \r = Mac line break
00176                         Tokenize(Data, Lines, "\r");
00177                 else                                       // \n = UNIX line break
00178                         Tokenize(Data, Lines, "\n");
00179 
00180                 int l;
00181                 bool header_found = false;
00182 
00183                 // For each line
00184                 for (l = 0; l < Lines.size(); l++)
00185                 {
00186                         // Found the header of the section
00187                         if (Lines[l] == "[" + Section + "]")
00188                                 header_found = true;
00189                         // Found the beginning of the next section
00190                         else if (header_found && (Lines[l].find("[") == 0))
00191                         {
00192                                 Exceptions::CorruptedData Err("The field \"" + Field + "\" was not foud in the section \"" + Section + "\".");
00193                                 throw Err;
00194                         }
00195                         // Found the field
00196                         else if (header_found && (Lines[l].find(Field + "=") != string::npos))
00197                         {
00198                                 return Lines[l].substr(Field.size() + 1);
00199                         }
00200                 }
00201 
00202                 Exceptions::CorruptedData Err("The section \"" + Section + "\" was not found.");
00203                 throw Err;
00204         }
00205 
00213         string IntToStr(const int &i)
00214         {
00215                 stringstream str;
00216                 str << i;
00217                 return str.str();
00218         }
00219 
00227         long StrToLong(const string &s)
00228         {
00229                 stringstream str(s);
00230                 long l = 0;
00231 
00232                 str >> l;
00233 
00234                 return l;
00235         }
00236 
00237 }

Generated on Tue Oct 25 14:36:29 2005 for cWebshots by  doxygen 1.4.5