TSPLIB
src/TransformTSPLIB.cpp
Go to the documentation of this file.
00001 
00057 #include <iostream>
00058 #include <stdexcept>
00059 #include <string>
00060 #include <algorithm>
00061 #include <fstream>
00062 #include <sstream>
00063 
00064 #include <xercesc/util/XMLException.hpp>
00065 #include <xercesc/dom/DOMException.hpp>
00066 #include <xercesc/util/OutOfMemoryException.hpp>
00067 
00068 #include "TransformConstantsClassesAndFunctions.hpp"
00069 
00070 using namespace std;
00071 using namespace xercesc;
00072 
00073 XERCES_CPP_NAMESPACE_USE
00074 
00075 
00081 const vector<vector<double> >::size_type N_THRESHOLD = 4000;
00082 
00083 
00095 int main(int argc, char* argv[]) {
00096         /*
00097          * Initialling.
00098          */
00099         cout << "TransformTSPLIB.cpp:" << endl;
00100         cout << "----------------------------------------------------------------" << endl;
00101 
00102         string inputFileName = "";
00103         string outputFileName = "";
00104         switch (argc) {
00105         case 1:
00106         {
00107                 try {
00108                         cout <<
00109                                         "The name of input file (the filename extension has to be \"tsp\" or \"atsp\"): " <<
00110                                         flush;
00111                         getline(cin, inputFileName);
00112 
00113                         cout << "The name of output file (the filename extension has to be \"xml\"): " << flush;
00114                         getline(cin, outputFileName);
00115                 }
00116                 catch (ios::failure &e) {
00117                         cerr << "The command-line arguments are invalid!" << endl;
00118                     return (1);
00119             }
00120         }
00121         break;
00122         case 3:
00123         {
00124                 inputFileName = argv[1];
00125 
00126                 outputFileName = argv[2];
00127         }
00128         break;
00129         default:
00130         {
00131                 cerr << "The command-line arguments are invalid!" << endl;
00132                 return (1);
00133         }
00134         }
00135 
00136         //Checking of validity of the inputFileName and the ouptuFileName
00137         string inputFileFilenameExtensionTSP;
00138         if (inputFileName.size() >= INPUT_FILE_FILENAME_EXTENSION_TSP.size() + 1) {
00139                 inputFileFilenameExtensionTSP =
00140                                 inputFileName.substr(
00141                                                 inputFileName.size() - INPUT_FILE_FILENAME_EXTENSION_TSP.size(),
00142                                                 INPUT_FILE_FILENAME_EXTENSION_TSP.size());
00143         }
00144         else {
00145                 inputFileFilenameExtensionTSP = "";
00146         }
00147 
00148         string inputFileFilenameExtensionATSP;
00149         if (inputFileName.size() >= INPUT_FILE_FILENAME_EXTENSION_ATSP.size() + 1) {
00150                 inputFileFilenameExtensionATSP =
00151                                 inputFileName.substr(
00152                                                 inputFileName.size() - INPUT_FILE_FILENAME_EXTENSION_ATSP.size(),
00153                                                 INPUT_FILE_FILENAME_EXTENSION_ATSP.size());
00154         }
00155         else {
00156                 inputFileFilenameExtensionATSP = "";
00157         }
00158 
00159         string outputFileFilenameExtension;
00160         if (outputFileName.size() >= OUTPUT_FILE_FILENAME_EXTENSION.size() + 1) {
00161                 outputFileFilenameExtension =
00162                                 outputFileName.substr(
00163                                                 outputFileName.size() - OUTPUT_FILE_FILENAME_EXTENSION.size(),
00164                                                 OUTPUT_FILE_FILENAME_EXTENSION.size());
00165         }
00166         else {
00167                 cerr << "The filename extension of the output file has to be  \"xml\"!" << endl;
00168                 return (1);
00169         }
00170 
00171         transform(
00172                         inputFileFilenameExtensionTSP.begin(),
00173                         inputFileFilenameExtensionTSP.end(),
00174                         inputFileFilenameExtensionTSP.begin(), ::tolower);
00175         transform(
00176                         inputFileFilenameExtensionATSP.begin(),
00177                         inputFileFilenameExtensionATSP.end(),
00178                         inputFileFilenameExtensionATSP.begin(), ::tolower);
00179         transform(
00180                         outputFileFilenameExtension.begin(),
00181                         outputFileFilenameExtension.end(),
00182                         outputFileFilenameExtension.begin(), ::tolower);
00183 
00184         if (
00185                         (inputFileFilenameExtensionTSP != INPUT_FILE_FILENAME_EXTENSION_TSP) &&
00186                         (inputFileFilenameExtensionATSP != INPUT_FILE_FILENAME_EXTENSION_ATSP)) {
00187                 cerr << "The filename extension of the input file has to be  \"tsp\" or \"atsp\"!" << endl;
00188                 return (1);
00189         }
00190         if (outputFileFilenameExtension != OUTPUT_FILE_FILENAME_EXTENSION) {
00191                 cerr << "The filename extension of the output file has to be  \"xml\"!" << endl;
00192                 return (1);
00193         }
00194 
00195         //Writing the used filenames on the standard output
00196         cout << "Input file: " << inputFileName << endl;
00197         cout << "Output file: " << outputFileName << endl;
00198 
00199         cout << endl;
00200 
00201 
00202         /*
00203          * Reading of the input file.
00204          */
00205         cout << "Reading the file \"" << inputFileName << "\" ... " << flush;
00206 
00207         TransformInstance *transformInstance = 0;
00208         try {
00209                 transformInstance = readInputFileTSPLIB(inputFileName);
00210         }
00211         catch (InputFileFormatNotSupported &e) {
00212             cerr << "the file format of the file \"" << inputFileName << "\" is not supported!" << endl;
00213             return (1);
00214         }
00215         catch (ifstream::failure &e) {
00216             cerr << "the file \"" << inputFileName << "\" does not exists or is not valid" <<
00217                         " or is damaged!" << endl;
00218             return (1);
00219     }
00220         catch (bad_alloc &e) {
00221                 cerr << "the input graph is too large!" << endl;
00222             return (1);
00223         }
00224         catch (range_error &e) {
00225                 cerr << "the range error occurs!" << endl;
00226             return (1);
00227         }
00228         catch ( ... ) {
00229                 cerr << "an exception occurs!" << endl;
00230             return (1);
00231         }
00232 
00233         cout << "OK" << endl;
00234 
00235 
00236         /*
00237          * Writing of the input file.
00238          */
00239         cout << "Writing to the file \"" << outputFileName << "\" ... " << flush;
00240 
00241         try {
00242                 //If the graph is small enough, use the safe method.
00243                 if (transformInstance->getN() <= N_THRESHOLD) {
00244                         writeOutputFile(outputFileName, transformInstance);
00245                 }  //If the graph is too big, create the XML file directly.
00246                 else {
00247                         writeOutputFileWithoutUsingAParser(outputFileName, transformInstance);
00248                 }
00249         }
00250         catch (bad_alloc &e) {
00251                 cerr << "the input graph is too large!" << endl;
00252             return (1);
00253         }
00254     catch (const OutOfMemoryException &e) {
00255                 cerr << "out of memory problem!" << endl;
00256                 delete transformInstance;
00257                 return (1);
00258     }
00259     catch (const DOMException &e) {
00260                 cerr << "Xerces failed!" << endl;
00261                 delete transformInstance;
00262                 return (1);
00263     }
00264         catch (const XMLException &e) {
00265                 cerr << "Xerces failed!" << endl;
00266                 delete transformInstance;
00267                 return (1);
00268         }
00269         catch ( ... ) {
00270                 cerr << "an exception occurs!" << endl;
00271                 delete transformInstance;
00272                 return (1);
00273         }
00274 
00275         cout << "OK" << endl;
00276 
00277 
00278         /*
00279          * Releasing of the memory.
00280          */
00281         delete transformInstance;
00282 
00283         return (0);
00284 }
 All Classes Files Functions Variables Friends Defines