• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

csv2yapet/main.cc

Go to the documentation of this file.
00001 // $Id: main.cc 3344 2010-09-17 19:26:56Z java $
00002 //
00003 // Copyright (C) 2009-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 // Additional permission under GNU GPL version 3 section 7
00021 //
00022 // If you modify this program, or any covered work, by linking or combining it
00023 // with the OpenSSL project's OpenSSL library (or a modified version of that
00024 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00025 // licenses, Rafael Ostertag grants you additional permission to convey the
00026 // resulting work.  Corresponding Source for a non-source form of such a
00027 // combination shall include the source code for the parts of OpenSSL used as
00028 // well as that of the covered work.
00029 //
00030 
00031 #ifdef HAVE_CONFIG_H
00032 # include <config.h>
00033 #endif
00034 
00035 #ifdef HAVE_ERRNO_H
00036 # include <errno.h>
00037 #endif
00038 
00039 #ifdef HAVE_STDIO_H
00040 # include <stdio.h>
00041 #endif
00042 
00043 #ifdef HAVE_GETOPT_H
00044 # include <getopt.h>
00045 #endif
00046 
00047 #ifdef HAVE_UNISTD_H
00048 # include <unistd.h>
00049 #endif
00050 
00051 #ifdef HAVE_STRING_H
00052 # include <string.h>
00053 #endif
00054 
00055 #ifdef HAVE_TERMIOS_H
00056 # include <termios.h>
00057 #endif
00058 
00059 #ifdef HAVE_STRING
00060 # include <string>
00061 #endif
00062 
00063 #ifdef HAVE_IOSTREAM
00064 # include <iostream>
00065 #endif
00066 
00067 #ifdef HAVE_STDEXCEPT
00068 # include <stdexcept>
00069 #endif
00070 
00071 #include <consts.h>
00072 #include "csvimport.h"
00073 
00074 #if defined(HAVE_TERMIOS_H) && defined (HAVE_TCSETATTR) && defined (HAVE_TCGETATTR)
00075 #define CAN_DISABLE_ECHO 1
00076 #endif
00077 
00078 enum {
00082     ERR_CMDLINE = 1,
00086     ERR_PASSWDMISMATCH = 2,
00090     ERR_FILEEXISTS = 3,
00094     ERR_FATAL = 4,
00098     MAX_PASSWD = 1024,
00102     MAX_FILEPATH = 1024
00103 };
00104 
00105 const char COPYRIGHT[] = "\nCopyright (C) 2009-2010  Rafael Ostertag\n"  \
00106                          "\n"                                \
00107                          "csv2yapet is part of YAPET.\n"                 \
00108                          "\n"                                \
00109                          "This program is free software: you can redistribute it and/or modify\n" \
00110                          "it under the terms of the GNU General Public License as published by\n" \
00111                          "the Free Software Foundation, either version 3 of the License, or\n" \
00112                          "(at your option) any later version.\n"             \
00113                          "\n"                                \
00114                          "This program is distributed in the hope that it will be useful,\n" \
00115                          "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"  \
00116                          "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"   \
00117                          "GNU General Public License for more details.\n"            \
00118                          "\n"                                \
00119                          "You should have received a copy of the GNU General Public License\n" \
00120                          "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n";
00121 
00126 void disable_echo() {
00127 #ifdef CAN_DISABLE_ECHO
00128     struct termios ctios;
00129     int err = tcgetattr (STDIN_FILENO, &ctios);
00130 
00131     if (err < 0)
00132         throw std::runtime_error (strerror (errno) );
00133 
00134     ctios.c_lflag &= ~ECHO;
00135     err = tcsetattr (STDIN_FILENO, TCSANOW, &ctios);
00136 
00137     if (err < 0)
00138         throw std::runtime_error (strerror (errno) );
00139 
00140 #endif
00141 }
00142 
00146 void enable_echo() {
00147 #ifdef CAN_DISABLE_ECHO
00148     struct termios ctios;
00149     int err = tcgetattr (STDIN_FILENO, &ctios);
00150 
00151     if (err < 0)
00152         throw std::runtime_error (strerror (errno) );
00153 
00154     ctios.c_lflag |= ECHO;
00155     err = tcsetattr (STDIN_FILENO, TCSANOW, &ctios);
00156 
00157     if (err < 0)
00158         throw std::runtime_error (strerror (errno) );
00159 
00160 #endif
00161 }
00162 
00163 void show_version() {
00164     std::cout << std::endl;
00165     std::cout << "csv2yapet is part of ";
00166     std::cout << PACKAGE_STRING << std::endl;
00167 }
00168 
00169 void show_copyright() {
00170     std::cout << COPYRIGHT << std::endl;
00171 }
00172 
00173 void show_help (char* prgname) {
00174     std::cout << std::endl;
00175     std::cout << prgname
00176               << " [-h] [-p <password>] [-q] [-s <char>] [-V] <src> <dst>"
00177               << std::endl
00178               << std::endl;
00179     std::cout << "-c, --copyright\tshow copyright information"
00180               << std::endl
00181               << std::endl;
00182     std::cout << "-h, --help\tshow this help text"
00183               << std::endl
00184               << std::endl;
00185     std::cout << "-p, --password\tuse <password> as the password for the file created"
00186               << std::endl
00187               << "\t\tby the convert."
00188               << std::endl
00189               << "\t\tThe use of this option is discouraged."
00190               << std::endl
00191               << std::endl;
00192     std::cout << "-q, --quiet\toperate quietly"
00193               << std::endl
00194               << std::endl;
00195     std::cout << "-s, --separator\tuse <char> as field separator."
00196               << std::endl
00197               << "\t\tDefault: ,"
00198               << std::endl
00199               << std::endl;
00200     std::cout << "-V, --version\tshow the version of csv2yapet"
00201               << std::endl
00202               << std::endl;
00203     std::cout << "<src>\t\tthe source csv file"
00204               << std::endl
00205               << std::endl;
00206     std::cout << "<dst>\t\tthe output file"
00207               << std::endl
00208               << std::endl;
00209     std::cout << "csv2yapet converts csv text files to files readable by YAPET."
00210               << std::endl
00211               << std::endl;
00212 }
00213 
00214 int main (int argc, char** argv) {
00215     bool quiet = false;
00216     bool cmdline_pw = false;
00217     char passwd[MAX_PASSWD];
00218     char separator = ',';
00219     std::string srcfile;
00220     std::string dstfile;
00221     int c;
00222 #ifdef HAVE_GETOPT_LONG
00223     struct option long_options[] = {
00224         { (char*) "copyright", no_argument, NULL, 'c'},
00225         { (char*) "help", no_argument, NULL, 'h'},
00226         { (char*) "password", required_argument, NULL, 'p'},
00227         { (char*) "quiet", no_argument, NULL, 'q'},
00228         { (char*) "separator", required_argument, NULL, 's'},
00229         { (char*) "version", no_argument, NULL, 'V'},
00230         {NULL, 0, NULL, 0}
00231     };
00232 
00233     while ( (c = getopt_long (argc, argv, ":chp:qs:V", long_options, NULL) ) != -1) {
00234 #else // HAVE_GETOPT_LONG
00235     extern char *optarg;
00236     extern int optopt, optind;
00237 
00238     while ( (c = getopt (argc, argv, ":c(copyright)h(help)p:(password)q(quiet)s:(separator)V(version)") ) != -1) {
00239 #endif // HAVE_GETOPT_LONG
00240 
00241         switch (c) {
00242             case 'c':
00243                 show_copyright();
00244                 return 0;
00245             case 'h':
00246                 show_help (argv[0]);
00247                 return 0;
00248             case 'p':
00249                 strncpy (passwd, optarg, MAX_PASSWD);
00250                 cmdline_pw = true;
00251                 break;
00252             case 'q':
00253                 quiet = true;
00254                 break;
00255             case 's':
00256                 sscanf (optarg, "%c", &separator);
00257                 break;
00258             case 'V':
00259                 show_version();
00260                 return 0;
00261             case ':':
00262                 std::cerr << "-" << (char) optopt << " without argument"
00263                           << std::endl;
00264                 return ERR_CMDLINE;
00265             case '?':
00266                 std::cerr << "unknown argument" << " '" << (char) optopt << "'"
00267                           << std::endl;
00268                 return ERR_CMDLINE;
00269         }
00270     }
00271 
00272     if (optind < argc && (argc - optind) == 2) {
00273         char tmp[MAX_FILEPATH];
00274         strncpy (tmp, argv[optind], MAX_FILEPATH);
00275         srcfile = tmp;
00276         strncpy (tmp, argv[++optind], MAX_FILEPATH);
00277         dstfile = tmp;
00278     } else {
00279         std::cerr << "Missing argument." << std::endl;
00280 
00281         if (!quiet)
00282             show_help (argv[0]);
00283 
00284         return ERR_CMDLINE;
00285     }
00286 
00287     if ( dstfile.find (YAPET::CONSTS::Consts::getDefaultSuffix(),
00288                        dstfile.length() -
00289                        YAPET::CONSTS::Consts::getDefaultSuffix().length() )
00290             == std::string::npos )
00291         dstfile += YAPET::CONSTS::Consts::getDefaultSuffix();
00292 
00293     if (access (dstfile.c_str(), F_OK) == 0) {
00294         std::cerr << dstfile << " already exists. Aborting." << std::endl;
00295         return ERR_FILEEXISTS;
00296     }
00297 
00298     try {
00299         // We read the password from stdin only if the user did not provide the
00300         // -s switch.
00301         if (!cmdline_pw) {
00302             std::cout << "Please enter the password for " << dstfile << ": ";
00303             std::cout.flush();
00304             std::string pw1;
00305             disable_echo();
00306             std::getline (std::cin, pw1);
00307             std::cout << std::endl;
00308             enable_echo();
00309             std::cout << "Please re-type the password: ";
00310             std::cout.flush();
00311             std::string pw2;
00312             disable_echo();
00313             std::getline (std::cin, pw2);
00314             std::cout << std::endl;
00315             enable_echo();
00316             std::cout << std::endl;
00317 
00318             if (pw1 != pw2) {
00319                 std::cerr << "Passwords do not match." << std::endl;
00320                 return ERR_PASSWDMISMATCH;
00321             }
00322 
00323             strncpy (passwd, pw1.c_str(), MAX_PASSWD);
00324         }
00325 
00326         CSVImport imp (srcfile, dstfile, separator, !quiet);
00327         imp.import (passwd);
00328 
00329         if (imp.hadErrors() && !quiet) {
00330             std::cout << "Had " << imp.numErrors() << " errors:" << std::endl;
00331             imp.printLog();
00332             std::cout << std::endl;
00333             std::cout << dstfile << " created with errors." << std::endl;
00334         } else {
00335             if (!quiet) {
00336                 std::cout << dstfile << " successfully created." << std::endl;
00337             }
00338         }
00339     } catch (std::exception& ex) {
00340         std::cerr << ex.what() << std::endl;
00341         return ERR_FATAL;
00342     }
00343 
00344     return 0;
00345 }

Generated on Sun Sep 19 2010 15:37:14 for YAPET by  doxygen 1.7.1