cWebshotsTXT.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 
00030 using namespace std;
00031 
00032 namespace cWebshots
00033 {
00037         cWebshotsTXT::cWebshotsTXT(void)
00038         {
00039                 Count = 0;
00040         }
00041 
00047         cWebshotsTXT::cWebshotsTXT(string directory)
00048         {
00049                 Count = 0;
00050                 Load(directory);
00051         }
00052 
00056         cWebshotsTXT::~cWebshotsTXT(void)
00057         {
00058                 if (Count > 0)
00059                         delete[] Pictures;
00060         }
00061 
00062 
00068         void cWebshotsTXT::Load(string directory)
00069         {
00070                 // File name and pointer
00071                 ifstream in_file;
00072                 string file;
00073 
00074                 // Buffer
00075                 char buffer[500];
00076                 string line;
00077 
00078                 // Field name & value, and index
00079                 string field, value;
00080                 int index;
00081 
00082                 // Position of the separator
00083                 size_t separator;
00084 
00085 
00086                 // Clear previous data
00087                 if (Count > 0)
00088                         delete[] Pictures;
00089 
00090 
00091                 // First, "album.txt" file
00092                 file = directory + "album.txt";
00093 
00094                 in_file.open(file.c_str(), ios_base::in);
00095 
00096                 // If a "album.txt" file is available
00097                 if (in_file.is_open())
00098                 {
00099                         // If the file is not empty
00100                         if (!in_file.eof())
00101                         {
00102                                 // Get a line
00103                                 in_file.getline(buffer,500,'\n');
00104                                 line = buffer;
00105 
00106                                 // If the line is not empty and contains a field and its value
00107                                 if ((line.length() > 0) && (separator = line.find("||")))
00108                                 {
00109                                         // Get the field name and the value from the line
00110                                         field = line.substr(0,separator);
00111                                         value = line.substr(separator + 2);
00112 
00113                                         if (field == "count")
00114                                                 Count = atoi(value.c_str());
00115                                         else
00116                                                 Count = 0;
00117                                 }
00118                                 else
00119                                         Count = 0;
00120                         }
00121                         else
00122                                 Count = 0;
00123 
00124 
00125                         // If the files contains just 1 record (an album *must* contain olny one record)
00126                         if (Count == 1)
00127                         {
00128                                 // Until the end of the file
00129                                 while (!in_file.eof())
00130                                 {
00131                                         // Get a line
00132                                         in_file.getline(buffer,500,'\n');
00133                                         line = buffer;
00134 
00135                                         // If the line is not empty and contains a field and its value
00136                                         if ((line.length() > 0) && (separator = line.find("||")))
00137                                         {
00138                                                 // Get the field name and the value from the line
00139                                                 field = line.substr(0,separator);
00140                                                 value = line.substr(separator + 2);
00141 
00142                                                 // Remove the index from the field name
00143                                                 field = field.substr(0,field.rfind("_"));
00144 
00145                                                 if (field == "ID")
00146                                                         Album.id = value;
00147                                                 else if (field == "TITLE")
00148                                                         Album.title = value;
00149                                                 else if (field == "IN_USE")
00150                                                         Album.in_use = (atoi(value.c_str()) == 1);
00151                                                 else if (field == "DESCRIPTION")
00152                                                         Album.description = value;
00153                                                 else if (field == "TOPIC_NAME")
00154                                                         Album.topic_name = value;
00155                                                 else if (field == "CREATE_DATE")
00156                                                         Album.create_date = atol(value.c_str());
00157                                                 else if (field == "EDIT_DATE")
00158                                                         Album.edit_date = atol(value.c_str());
00159                                                 else if (field == "PHOTO_COUNT")
00160                                                         Album.photo_count = atoi(value.c_str());
00161                                         }
00162                                 }
00163                         }
00164 
00165                         in_file.close();
00166                 }
00167 
00168 
00169                 // Then, "photos.txt" file
00170                 file = directory + "photos.txt";
00171 
00172                 in_file.clear();
00173                 in_file.open(file.c_str(), ios_base::in);
00174 
00175                 // If a "photos.txt" file is available
00176                 if (in_file.is_open())
00177                 {
00178                         // If the file is not empty
00179                         if (!in_file.eof())
00180                         {
00181                                 // Get a line
00182                                 in_file.getline(buffer,500,'\n');
00183                                 line = buffer;
00184 
00185                                 // If the line is not empty and contains a field and its value
00186                                 if ((line.length() > 0) && (separator = line.find("||")))
00187                                 {
00188                                         // Get the field name and the value from the line
00189                                         field = line.substr(0,separator);
00190                                         value = line.substr(separator + 2);
00191 
00192                                         if (field == "count")
00193                                                 Count = atoi(value.c_str());
00194                                         else
00195                                                 Count = 0;
00196                                 }
00197                                 else
00198                                         Count = 0;
00199                         }
00200                         else
00201                                 Count = 0;
00202 
00203 
00204                         // If the files contains at least 1 record
00205                         if (Count > 0)
00206                         {
00207                                 Pictures = new sPhotosTXT[Count];
00208 
00209                                 // Until the end of the file
00210                                 while (!in_file.eof())
00211                                 {
00212                                         // Get a line
00213                                         in_file.getline(buffer,500,'\n');
00214                                         line = buffer;
00215 
00216                                         // If the line is not empty and contains a field and its value
00217                                         if ((line.length() > 0) && (separator = line.find("||")))
00218                                         {
00219                                                 // Get the field name and the value from the line
00220                                                 field = line.substr(0,separator);
00221                                                 value = line.substr(separator + 2);
00222 
00223                                                 // Get the index of the record
00224                                                 index = atoi(field.substr(field.rfind("_") + 1).c_str());
00225 
00226                                                 // Remove the index from the field name
00227                                                 field = field.substr(0,field.rfind("_"));
00228 
00229                                                 if (field == "ID")
00230                                                         Pictures[index].id = atol(value.c_str());
00231                                                 else if (field == "POSITION")
00232                                                         Pictures[index].position = value;
00233                                                 else if (field == "TITLE")
00234                                                         Pictures[index].title = value;
00235                                                 else if (field == "CAPTION")
00236                                                         Pictures[index].caption = value;
00237                                                 else if (field == "CREDIT")
00238                                                         Pictures[index].credit = value;
00239                                                 else if (field == "URL_PHOTO")
00240                                                         Pictures[index].url_photo = value;
00241                                                 else if (field == "URL_THUMB")
00242                                                         Pictures[index].url_thumb = value;
00243                                                 else if (field == "SOURCE")
00244                                                         Pictures[index].source = value;
00245                                         }
00246                                 }
00247                         }
00248 
00249                         in_file.close();
00250                 }
00251         }
00252 
00253 
00261         sPhotosTXT& cWebshotsTXT::Picture(int index)
00262         {
00263                 return Pictures[index];
00264         }
00265 
00271         int cWebshotsTXT::PictureCount()
00272         {
00273                 return Count;
00274         }
00275 
00286         int cWebshotsTXT::IndexFromFileName(string filename, bool case_sensitive)
00287         {
00288                 int i;
00289 
00290                 for (i = 0; i < Count; i++)
00291                 {
00292                         if (case_sensitive)
00293                         {
00294                                 if (Pictures[i].url_photo == filename)
00295                                         return i;
00296                         }
00297                         else
00298                         {
00299                                 // This comparation should be case insensitive in order to work with some strange files
00300                                 if (Pictures[i].url_photo == filename)
00301                                         return i;
00302                         }
00303                 }
00304 
00305                 return IndexNotFound;
00306         }
00307 
00308 }

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