Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

makexindex.cpp

Go to the documentation of this file.
00001 // makexindex.cpp
00002 //
00003 // Copyright (C) 2004, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // Convert an ASCII cross index to binary
00011 
00012 #include <iostream>
00013 #include <fstream>
00014 #include <iomanip>
00015 #include <string>
00016 #include <celutil/basictypes.h>
00017 #include <celutil/bytes.h>
00018 
00019 using namespace std;
00020 
00021 
00022 static string inputFilename;
00023 static string outputFilename;
00024 
00025 
00026 void Usage()
00027 {
00028     cerr << "Usage: makexindex [input file] [output file]\n";
00029 }
00030 
00031 
00032 bool parseCommandLine(int argc, char* argv[])
00033 {
00034     int i = 1;
00035     int fileCount = 0;
00036 
00037     while (i < argc)
00038     {
00039         if (argv[i][0] == '-')
00040         {
00041             cerr << "Unknown command line switch: " << argv[i] << '\n';
00042             return false;
00043         }
00044         else
00045         {
00046             if (fileCount == 0)
00047             {
00048                 // input filename first
00049                 inputFilename = string(argv[i]);
00050                 fileCount++;
00051             }
00052             else if (fileCount == 1)
00053             {
00054                 // output filename second
00055                 outputFilename = string(argv[i]);
00056                 fileCount++;
00057             }
00058             else
00059             {
00060                 // more than two filenames on the command line is an error
00061                 return false;
00062             }
00063             i++;
00064         }
00065     }
00066 
00067     return true;
00068 }
00069 
00070 
00071 static void writeUint(ostream& out, uint32 n)
00072 {
00073     LE_TO_CPU_INT32(n, n);
00074     out.write(reinterpret_cast<char*>(&n), sizeof n);
00075 }
00076 
00077 
00078 static void writeShort(ostream& out, int16 n)
00079 {
00080     LE_TO_CPU_INT16(n, n);
00081     out.write(reinterpret_cast<char*>(&n), sizeof n);
00082 }
00083 
00084 
00085 bool WriteCrossIndex(istream& in, ostream& out)
00086 {
00087     // Write the header
00088     out.write("CELINDEX", 8);
00089 
00090     // Write the version
00091     writeShort(out, 0x0100);
00092     
00093     unsigned int record = 0;
00094     while (!in.eof())
00095     {
00096         unsigned int catalogNumber;
00097         unsigned int celCatalogNumber;
00098 
00099         in >> catalogNumber;
00100         if (in.eof())
00101             return true;
00102 
00103         in >> celCatalogNumber;
00104         if (!in.good())
00105         {
00106             cerr << "Error parsing record #" << record << '\n';
00107             return false;
00108         }
00109 
00110         writeUint(out, (uint32) catalogNumber);
00111         writeUint(out, (uint32) celCatalogNumber);
00112 
00113         record++;
00114     }
00115 
00116     return true;
00117 }
00118 
00119 
00120 int main(int argc, char* argv[])
00121 {
00122     if (!parseCommandLine(argc, argv) || inputFilename.empty())
00123     {
00124         Usage();
00125         return 1;
00126     }
00127 
00128     istream* inputFile = &cin;
00129     if (!inputFilename.empty())
00130     {
00131         inputFile = new ifstream(inputFilename.c_str(), ios::in);
00132         if (!inputFile->good())
00133         {
00134             cerr << "Error opening input file " << inputFilename << '\n';
00135             return 1;
00136         }
00137     }
00138 
00139     ostream* outputFile = &cout;
00140     if (!outputFilename.empty())
00141     {
00142         outputFile = new ofstream(outputFilename.c_str(), ios::out | ios::binary);
00143         if (!outputFile->good())
00144         {
00145             cerr << "Error opening output file " << outputFilename << '\n';
00146             return 1;
00147         }
00148     }
00149 
00150     bool success = WriteCrossIndex(*inputFile, *outputFile);
00151 
00152     return success ? 0 : 1;
00153 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1