Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
   head();
   setFREQ();
   try {
      char *iname = NULL, *oname = NULL;
      fstream *fo = NULL;
      // обработка аргументов
      for( int i = 1; i < argc; i++ ) {
         if( argv[i][0] == '-' && argv[i][1] ) { // ключи
            for( char *s = argv[i]+1; *s; s++ ) {
               switch( *s ) {

                  case 'v': // set verbose
                     opt_v = 1;
                     if( opt_q )
                        throw SHOW_USAGE;
                     break;

                  case 'q': // set verbose
                     opt_q = 1;
                     if( opt_v )
                        throw SHOW_USAGE;
                     break;

                  case 'o': // файл для вывода
                     if( !*(s+1) ) {
                        if( ++i >= argc )
                           throw SHOW_USAGE;
                        oname = argv[i];
                     } else
                        oname = s+1;
                     if( strcmp( oname, "-" ) == 0 )
                        fo = new fstream(1);   // 1 - stdout
                     else
                        fo = new fstream(oname,
                             ios::out
#ifdef DOS
                             | ios::binary
#endif
                             );
                     if( !*fo ) {
                        cerr << "emagic: Error creating file " << iname << "\n";
                        throw FILE_ERROR;
                     }
                     goto NEXT_ARG;

                  case 'n': // глубина перебора
                     if( !*(s+1) ) {
                        if( ++i >= argc )
                           throw SHOW_USAGE;
                        s = argv[i];
                     } else
                        s++;
                     if( *s < '0' || *s > '9' )
                        throw SHOW_USAGE;
                     opt_n = atoi(s);
                     if( opt_n > 10 || opt_n < 1 )
                        throw SHOW_USAGE;
                     goto NEXT_ARG;

                  case 'w':
                     warranty();
                     return 0;

                  default:
                     throw SHOW_USAGE; // unknown option

               } // switch
            } // цикл по символам (ключам)
         } else { // параметры (не начинается с '-')
            // последовательно обрабатываем файл за файлом
            iname = argv[i];

            int filepos = -1; // для работы с файлом "на месте"
            if( !oname )
               if( strcmp( iname, "-" ) == 0 )
                  fo = new fstream(1);   // 1 - stdout
               else {
                  fo = new fstream(iname,
                       ios::out | ios::in | ios::ate
#ifdef DOS
                       | ios::binary
#endif
                       );
                  fo->seekp( filepos = 0 );
               }

            fstream *fi;
            if( strcmp( iname, "-" ) == 0 )
               fi = new fstream(0);
            else if( !oname )
               fi = fo;
            else
               fi = new fstream( iname, ios::in
#ifdef DOS
               | ios::binary
#endif
               );

            if( !*fi || !*fo ) {
               cerr << "emagic: Error opening file " << iname << "\n";
               throw FILE_ERROR;
            }

            uchar *buf = new uchar[RECODEBUFLEN+1];

            fi->seekg(0);
            int done = 0; int check = 1;
            uchar *cbuf = NULL;

            while( !done ) {
               fi->read( buf, RECODEBUFLEN );
               if( fi->bad() ) {
                  cerr << "Error reading from input file " << iname << "\n";
                  throw FILE_ERROR;
               }
               int bread = fi->good() ? RECODEBUFLEN : fi->gcount();
               if( fi->eof() ) {
                  fi->clear();
                  done = 1;
               }
               buf[bread]=0;

               if( check ) {  // определяем нужный тип перекодировки
                  check = 0;

                  // строим таблицу перекодировки
                  int i;
                  for( i = 0; i < 128; i++ )
                    custom[i] = i | 0x80;
                  custom[128] = 0;

                  cbuf = makesample( buf );

                  if( cbuf ) {
                     if( opt_v )
                        cerr << "Sample of " << strlen((char*)cbuf) <<
                             " bytes read from file " << iname << "\n";
                     rec[0] = process[1] = process[0] = -1;

                     // определяем кодировку
                     exb = new uchar[CHARBUFLEN+2];

                     recode( cbuf, RCUSTOM, exb );
                     maxweight = weight( cbuf, exb );
               
                     tryall( cbuf, 0 );
                     
                     delete exb;
                     exb = NULL;

                     if( !opt_q ) {
                        cerr << iname << ": ";
                        if( process[1] < 0 )
                           cerr << "no recoding seems to be necessary";
                        else
                           cerr << "the decoding sequence seems to be: ";
                     }

                     for( i = 1; process[i] >= 0; i++ ) {
                        if( !opt_q )
                           cerr << RNAME[process[i]] << ' ';
                        recode( custom, process[i], NULL );
                     }
                     if( !opt_q )
                        cerr << endl;
                  } else if( !opt_q ) 
                     cerr << iname << ": no Russian text found\n";
               }

               if( filepos >= 0 )
                  fo->seekp( filepos );
               recode( buf, RCUSTOM, NULL );
               fo->write( buf, bread );
               if( filepos >= 0 )
                  filepos = fo->tellp();
               if( fo->bad() ) {
                  cerr << "Error writing output to " << (oname ? oname : iname ) << "\n";
                  throw FILE_ERROR;
               }
               if( filepos >= 0 )
                  fi->seekg( filepos );
            }
            if( fi!=fo ) {
               fi->close();
               delete fi;
            }
            if( !oname ) {
               fo->close();
               delete fo;
            }
            delete buf; buf = NULL;
            if( cbuf ) {
               delete cbuf; 
               cbuf = NULL;
            }
         }
         NEXT_ARG: ;
      }
      // работа завершена (или ее не было)
      if( !iname )
         throw SHOW_USAGE;

      if( oname ) {
         fo->close();
         delete fo;
      }

   } catch( int errno ) {
      if( errno == SHOW_USAGE )
         usage();
      return errno;
#ifdef MSDOS
   } catch( xalloc x ) {
      cerr << "emagic: Cannot allocate memory for recoding buffer\n";
      return NO_MEMORY;
#endif
   } catch( ... ) {
      cerr << "emagic: Unhandled exception caught. Exiting. (:-0)\n";
      return 99;
   }
   return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int c;
    int i,d;
    double xstep, llimit, ulimit;  /* x-spacing, lower limit, upper limit */
    int calclimit, verbose;       /* calculate limits, be verbose */
    
    progname=strrchr(argv[0], '/');
    if (progname == NULL) progname=argv[0];
    else                  progname++;
    
    /*
     * These are mostly preliminary assignments, the actual values will
     * be calculated when evaluating the spline expression depending on
     */
    
    verbose=0; calclimit=1; xstep=DXSTEP; llimit=ulimit=0.0; d=INTERVALLS;
    while ((c = getopt_long(argc, argv, "a:?hl:n:u:vVW",
                            long_options, (int *)0)) != EOF) {
        switch (c) {
            case 0:   break;
            case 'a': xstep=strtod(optarg, NULL);
                if (fpclassify(xstep) == FP_ZERO) xstep=DXSTEP;
                break;
            case 'l': llimit=strtod(optarg, NULL); calclimit=0; break;
            case 'h':
            case '?': usage(); break;
            case 'n': d=atoi(optarg); break;
            case 'u': ulimit=strtod(optarg, NULL); calclimit=0; break;
            case 'v': verbose++; break;
            case 'V': fprintf(stderr, "%s version %s\n"
                              "Akima spline interpolation (c) 1996-1998 David Frey.\n"
                              "This is free software; see the GNU General Public Licence version 2 or "
                              "later\n"
                              "for copying conditions.\n"
                              "There is NO warranty.  See %s --licence for details.\n",
                              progname, VERSION, progname);
                return 0; break;
            case 'W': warranty(); break;
            default : break;
        }
    }
    
    if (ulimit < llimit) {
        double temp;
        
        temp=ulimit; ulimit=llimit; llimit=temp;
    }
    
    if (argc > optind) {
        int s;
        
        s=0;
        for (i=optind; i < argc; i++) {
            readandcalcspline(NULL, argv[i],
                              d, xstep, llimit, ulimit, calclimit, verbose,
                              &s);
            
            if ((s > 0) && (i < argc-1)) printf("\n");
            s++;
        }
    } else {
        int s;
        
        s=0;
        readandcalcspline(stdin, "stdin",
                          d, xstep, llimit, ulimit, calclimit, verbose,
                          &s);
    }
    return 0;
}