00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00049 inputFilename = string(argv[i]);
00050 fileCount++;
00051 }
00052 else if (fileCount == 1)
00053 {
00054
00055 outputFilename = string(argv[i]);
00056 fileCount++;
00057 }
00058 else
00059 {
00060
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
00088 out.write("CELINDEX", 8);
00089
00090
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 }