00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00101 in_file.seekg(0);
00102 in_file.read((char *)&File_Type, 4);
00103
00104 switch (File_Type)
00105 {
00106
00107 case WBC_File_Marker:
00108 Type = WBC;
00109 break;
00110
00111
00112 case WBZ_File_Marker:
00113 Type = WBZ;
00114 break;
00115
00116
00117 case WB1_File_Marker:
00118 case JFIF_File_Marker:
00119 case EXIF_File_Marker:
00120 Type = WB1;
00121 break;
00122
00123
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
00142 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00143
00144 string::size_type pos = str.find_first_of(delimiters, lastPos);
00145
00146 while (string::npos != pos || string::npos != lastPos)
00147 {
00148
00149 tokens.push_back(str.substr(lastPos, pos - lastPos));
00150
00151 lastPos = str.find_first_not_of(delimiters, pos);
00152
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
00173 if (Data.find("\r\n") != string::npos)
00174 Tokenize(Data, Lines, "\r\n");
00175 else if (Data.find("\n") == string::npos)
00176 Tokenize(Data, Lines, "\r");
00177 else
00178 Tokenize(Data, Lines, "\n");
00179
00180 int l;
00181 bool header_found = false;
00182
00183
00184 for (l = 0; l < Lines.size(); l++)
00185 {
00186
00187 if (Lines[l] == "[" + Section + "]")
00188 header_found = true;
00189
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
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 }