webshots.cpp

Go to the documentation of this file.
00001 /*
00002     cWebshots CLI Test Program
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 
00025 #include "cWebshots.h"
00026 
00027 #include <iostream>
00028 
00029 using namespace std;
00030 using namespace cWebshots;
00031 
00032 // Command line options
00033 enum CLArgs
00034 {
00035         claUndefined,
00036         claInput,
00037         claOutputFormat,
00038         claOutputFileName,
00039         claOutputDirectory,
00040         claIdentify,
00041         claDefaultCategory,
00042         claDefaultAlbum,
00043         claDefaultAlbumID,
00044         claDefaultCredits,
00045         claDefaultSource
00046 };
00047 
00048 int main(int argc, char* argv[])
00049 {
00050         CLArgs mode = claUndefined;
00051         int i;
00052         string commande;
00053 
00054         // Input pictures
00055         cWebshotsCollection Entree;
00056 
00057         // Input temporary collection
00058         cWebshotsCollection EntreeColl;
00059 
00060         // Output format
00061         Webshots_Formats Format = Unknown;
00062 
00063         // Output folder
00064         string DossierSortie;
00065 
00066         // Output file
00067         string FichierSortie;
00068 
00069         // Rename files or not
00070         bool Rename = false;
00071 
00072         // Default values
00073         string DefaultCategory = "cWebshots";
00074         string DefaultAlbum = "Converted files";
00075         string DefaultAlbumID = "0";
00076         string DefaultCredits = "cWebshots";
00077         string DefaultSource = "u";
00078 
00079         cout << "UWC - Ultimate Webshots Converter" << endl
00080                 << "Version 2.0 beta 6 - Console" << endl
00081                 << "THIS IS A BETA VERSION, USE IT WITH CARE !" << endl
00082                 << endl
00083                 << "Use '--help' command for help." << endl
00084                 << endl;
00085 
00086         Entree.Title = "cWebshots - Converted Pictures";
00087         Entree.Topic = "cWebshots";
00088 
00089         try
00090         {
00091                 // Read command line arguments
00092                 for (i = 0 ; i < argc; i++)
00093                 {
00094                         commande.assign(argv[i]);
00095 
00096                         // Commands
00097                         if ((commande.compare("-i") == 0) || (commande.compare("--input") == 0))
00098                                 mode = claInput;
00099                         else if ((commande.compare("-of") == 0) || (commande.compare("--output-format") == 0))
00100                                 mode = claOutputFormat;
00101                         else if ((commande.compare("-ofn") == 0) || (commande.compare("--output-file-name") == 0))
00102                                 mode = claOutputFileName;
00103                         else if ((commande.compare("-od") == 0) || (commande.compare("--output-directory") == 0))
00104                                 mode = claOutputDirectory;
00105                         else if ((commande.compare("-r") == 0) || (commande.compare("--rename") == 0))
00106                                 Rename = true;
00107                         else if ((commande.compare("-h") == 0) || (commande.compare("--help") == 0))
00108                         {
00109                                 cout << "Usage :" << endl;
00110                                 cout << endl
00111                                 << "-i or --input (\"file\" | \"folder\") [(\"file\" | \"folder\") ...]" << endl
00112                                 << "-of or --output-format (WBC | JPG | WBZ | WB1)" << endl
00113                                 << "-ofn or --output-file-name \"file\"" << endl
00114                                 << "-od or --output-directory \"folder\"" << endl
00115                                 << "-r or --rename" << endl
00116                                 << "-id or --identify (file) *must be used alone*" << endl
00117                                 << "-dc or --default-category (category)" << endl
00118                                 << "-da or --default-album (album)" << endl
00119                                 << "-dai or --default-album-id (albumid)" << endl
00120                                 << "-dcr or --default-credits (credits)" << endl
00121                                 << "-ds or --default-source ( w | c | u )" << endl
00122                                 << endl
00123                                 << "If the output format is WBC or WBZ, the input pictures will be merged into one "
00124                                 << "collection, and saved in the file defined by --output-file-name, "
00125                                 << "in the directory defined with --output-directory." << endl
00126                                 << endl
00127                                 << "For the other formats, if there is only one input picture, it will be saved into the file defined "
00128                                 << "by --output-file-name in the directory defined with --output-directory." << endl
00129                                 << endl
00130                                 << "If there is more than one picture or if the --rename option is set, the name of the output file will be "
00131                                 << "the caption of the picture, and if not available, the original filename "
00132                                 << "of the picture will be used instead." << endl;
00133 
00134                                 return 1;
00135                         }
00136                         else if ((commande.compare("-id") == 0) || (commande.compare("--identify") == 0))
00137                                 mode = claIdentify;
00138                         else if ((commande.compare("-dc") == 0) || (commande.compare("--default-category") == 0))
00139                                 mode = claDefaultCategory;
00140                         else if ((commande.compare("-da") == 0) || (commande.compare("--default-album") == 0))
00141                                 mode = claDefaultAlbum;
00142                         else if ((commande.compare("-dai") == 0) || (commande.compare("--default-album-id") == 0))
00143                                 mode = claDefaultAlbumID;
00144                         else if ((commande.compare("-dcr") == 0) || (commande.compare("--default-credits") == 0))
00145                                 mode = claDefaultCredits;
00146                         else if ((commande.compare("-ds") == 0) || (commande.compare("--default-source") == 0))
00147                                 mode = claDefaultSource;
00148                         // Values
00149                         else
00150                         {
00151                                 // Input files
00152                                 if (mode == claInput)
00153                                 {
00154                                         cout << "Loading " << commande << " ... ";
00155                                         try
00156                                         {
00157                                                 EntreeColl.LoadFromFile(commande);
00158                                         }
00159                                         catch (Exceptions::FileSystemError)
00160                                         {
00161                                                 cout << "Error !" << endl
00162                                                         << "Warning : This cannot be opened as a file, trying to open it as a folder ... ";
00163                                                 EntreeColl.LoadFromFolder(commande);
00164                                         }
00165                                         cout << "Ok : " << EntreeColl.PictureCount() << " pictures loaded." << endl;
00166 
00167                                         Entree += EntreeColl;
00168                                         EntreeColl.Clear();
00169                                 }
00170                                 // Output format
00171                                 else if (mode == claOutputFormat)
00172                                 {
00173                                         if (commande == "JPG")
00174                                                 Format = JPG;
00175                                         else if (commande == "WB1")
00176                                                 Format = WB1;
00177                                         else if (commande == "WBZ")
00178                                                 Format = WBZ;
00179                                         else if (commande == "WBC")
00180                                                 Format = WBC;
00181                                         else if (commande == "FOLDER")
00182                                                 Format = Folder;
00183                                         else
00184                                         {
00185                                                 Exceptions::Exception error("Unknown output format : " + commande);
00186                                                 throw error;
00187                                         }
00188                                 }
00189                                 // Output directory
00190                                 else if (mode == claOutputDirectory)
00191                                         DossierSortie = commande;
00192                                 // Output file
00193                                 else if (mode == claOutputFileName)
00194                                         FichierSortie = commande;
00195                                 // Identify
00196                                 else if (mode == claIdentify)
00197                                 {
00198                                         cout << "The file \"" << commande << "\" seems to be a ";
00199 
00200                                         switch (IdentifyFile(commande))
00201                                         {
00202                                         case WB1:
00203                                                 cout << "WB1 / JPG";
00204                                                 break;
00205                                         case WBZ:
00206                                                 cout << "WBZ";
00207                                                 break;
00208                                         case WBC:
00209                                                 cout << "WBC";
00210                                                 break;
00211                                         case Unknown:
00212                                                 cout << "unknown";
00213                                         }
00214 
00215                                         cout << " file." << endl;
00216 
00217                                         return 1;
00218                                 }
00219                                 // Default values
00220                                 else if (mode == claDefaultAlbum)
00221                                         DefaultAlbum = commande;
00222                                 else if (mode == claDefaultAlbumID)
00223                                         DefaultAlbumID = commande;
00224                                 else if (mode == claDefaultCategory)
00225                                         DefaultCategory = commande;
00226                                 else if (mode == claDefaultCredits)
00227                                         DefaultCredits = commande;
00228                                 else if (mode == claDefaultSource)
00229                                         DefaultSource = commande;
00230                         }
00231                 }
00232 
00233                 // ---- Start conversion ----
00234 
00235                 // If there is no picture to convert
00236                 if (Entree.PictureCount() == 0)
00237                 {
00238                         Exceptions::Exception error("No input files, or no pictures in the input files");
00239                         throw error;
00240                 }
00241 
00242                 cout << endl;
00243 
00244                 // Check if there is the need to use default values
00245                 for (i = 0; i < Entree.PictureCount(); i++)
00246                 {
00247                         if (Entree[i].Title.length() == 0)
00248                         {
00249                                 if (Entree[i].Original_Filename.length() > 0)
00250                                 {
00251                                         Entree[i].Title = Entree[i].Original_Filename;
00252                                         cout << "Warning : Picture " << i << " has no title. Filename will be used instead." << endl;
00253                                 }
00254                                 else
00255                                 {
00256                                         Entree[i].Title = "No title";
00257                                         cout << "Warning : Picture " << i << " has no title. Using default title." << endl;
00258                                 }
00259                         }
00260 
00261                         if ((Entree[i].Category.length() == 0) && (Entree[i].Album.length() == 0))
00262                         {
00263                                 Entree[i].Category = DefaultCategory;
00264                                 Entree[i].Album = DefaultAlbum;
00265                                 Entree[i].AlbumID = DefaultAlbumID;
00266                                 cout << "Warning : Picture " << i << " has no album and category. Using defaults." << endl;
00267                         }
00268 
00269                         if (Entree[i].Credits.length() == 0)
00270                         {
00271                                 Entree[i].Credits = DefaultCredits;
00272                                 cout << "Warning : Picture " << i << " has no credit. Using default." << endl;
00273                         }
00274 
00275                         if (Entree[i].Source.length() == 0)
00276                         {
00277                                 Entree[i].Source = DefaultSource;
00278                                 cout << "Warning : Picture " << i << " has no source. Using default." << endl;
00279                         }
00280                 }
00281 
00282                 // List the pictures in the collection
00283                 cout << endl << "List of the input pictures :" << endl
00284                         << endl;
00285                 for (i = 0; i < Entree.PictureCount(); i++)
00286                 {
00287                         cout << "[" << i << "] Title: " << Entree[i].Title << endl
00288                                 << "    ID: " << Entree[i].ID << endl
00289                                 << "    Caption: " << Entree[i].Caption << endl
00290                                 << "    Original Filename: " << Entree[i].Original_Filename << endl
00291                                 << endl;
00292                 }
00293 
00294 
00295                 // If the output format is a single picture format
00296                 if ((Format == JPG) || (Format == WB1))
00297                 {
00298                         string tmpFileName;
00299                         string tmpExt;
00300 
00301                         switch (Format)
00302                         {
00303                         case JPG:
00304                                 tmpExt = ".jpg";
00305                                 break;
00306                         case WB1:
00307                                 tmpExt = ".wb1";
00308                         }
00309 
00310                         // If there is more than one picture to save, then they must obviously be saved each in one file,
00311                         // so the rename option is turned on
00312                         if (Entree.PictureCount() > 1)
00313                         {
00314                                 cout << "Warning: There is more than one picture to save, and the format you selected contains only one picture per file. "
00315                                         << "Turning on the --rename option." << endl
00316                                         << endl;
00317 
00318                                 Rename = true;
00319                         }
00320 
00321                         // For each picture in the input collection
00322                         for (i = 0; i < Entree.PictureCount(); i++)
00323                         {
00324                                 if (Rename)
00325                                 {
00326                                         // Use the caption as the output filename
00327                                         if (Entree[i].Title.length() != 0)
00328                                                 tmpFileName = Entree[i].Title;
00329                                         // if unavailable, use the original filename
00330                                         else if (Entree[i].Original_Filename.length() != 0)
00331                                                 tmpFileName = Entree[i].Original_Filename;
00332                                 }
00333                                 else
00334                                 {
00335                                         tmpFileName = FichierSortie;
00336                                         tmpExt = "";
00337                                 }
00338 
00339                                 // Save the file
00340                                 cout << "Saving " << DossierSortie << "\\" << tmpFileName << tmpExt << " ... ";
00341                                 Entree[i].SaveToFile(DossierSortie + "\\" + tmpFileName + tmpExt, Format);
00342                                 cout << "Ok." << endl;
00343                         }
00344                 }
00345                 // If the output format is a collection
00346                 else if ((Format == WBC) || (Format == WBZ))
00347                 {
00348                         cout << "Saving "  << DossierSortie << "\\" << FichierSortie << " ... ";
00349 
00350                         // Save the collection
00351                         switch (Format)
00352                         {
00353                         case WBC:
00354                                 Entree.SaveToFile(DossierSortie + "\\" + FichierSortie, WBC);
00355                                 break;
00356 
00357                         case WBZ:
00358                                 Entree.SaveToFile(DossierSortie + "\\" + FichierSortie, WBZ);
00359                         }
00360 
00361                         cout << "Ok." << endl;
00362                 }
00363                 else if (Format == Folder)
00364                 {
00365                         cout << "Saving the collection in "  << DossierSortie << endl;
00366 
00367                         // Save the collection
00368                         Entree.SaveToFolder(DossierSortie);
00369                 }
00370 
00371 
00372         }
00373         catch (Exceptions::Exception &Err)
00374         {
00375                 cout << endl << "Error : " << Err.Description() << endl;
00376         }
00377 
00378         return 1;
00379 }

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