| 1 |
/* |
| 2 |
* Aiksaurus - An open source thesaurus library |
| 3 |
* Copyright (C) 2001 by Jared Davis |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
* 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <cstdio> |
| 23 |
#include <cassert> |
| 24 |
#include <iostream> |
| 25 |
#include <strstream> |
| 26 |
#include "Arguments.h" |
| 27 |
using namespace std; |
| 28 |
|
| 29 |
|
| 30 |
static const int BUF_SIZE = 256; |
| 31 |
|
| 32 |
class MeaningStream |
| 33 |
{ |
| 34 |
private: |
| 35 |
|
| 36 |
FILE* d_file_ptr; |
| 37 |
int d_position; |
| 38 |
int d_end; |
| 39 |
unsigned char d_buffer[BUF_SIZE * 2]; |
| 40 |
|
| 41 |
public: |
| 42 |
|
| 43 |
MeaningStream(FILE* file_ptr); |
| 44 |
bool read(int& x); |
| 45 |
}; |
| 46 |
|
| 47 |
MeaningStream::MeaningStream(FILE* file_ptr) |
| 48 |
: d_file_ptr(file_ptr), d_position(0), d_end(0) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
bool MeaningStream::read(int& x) |
| 54 |
{ |
| 55 |
bool ret = true; |
| 56 |
|
| 57 |
if (d_position < d_end) |
| 58 |
{ |
| 59 |
x = d_buffer[d_position++] << 8; |
| 60 |
x |= d_buffer[d_position++]; |
| 61 |
} |
| 62 |
|
| 63 |
else |
| 64 |
{ |
| 65 |
d_end = fread(d_buffer, 1, BUF_SIZE * 2, d_file_ptr); |
| 66 |
|
| 67 |
if (d_end) |
| 68 |
{ |
| 69 |
d_position = 0; |
| 70 |
x = d_buffer[d_position++] << 8; |
| 71 |
x |= d_buffer[d_position++]; |
| 72 |
} |
| 73 |
|
| 74 |
else |
| 75 |
{ |
| 76 |
ret = false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
const char* copyright = |
| 86 |
"/*\n" |
| 87 |
" * Aiksaurus - An open source thesaurus library\n" |
| 88 |
" * Copyright (C) 2001 by Jared Davis\n" |
| 89 |
" *\n" |
| 90 |
" * This program is free software; you can redistribute it and/or\n" |
| 91 |
" * modify it under the terms of the GNU General Public License\n" |
| 92 |
" * as published by the Free Software Foundation; either version 2\n" |
| 93 |
" * of the License, or (at your option) any later version.\n" |
| 94 |
" *\n" |
| 95 |
" * This program is distributed in the hope that it will be useful,\n" |
| 96 |
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 97 |
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
| 98 |
" * GNU General Public License for more details.\n" |
| 99 |
" *\n" |
| 100 |
" * You should have received a copy of the GNU General Public License\n" |
| 101 |
" * along with this program; if not, write to the Free Software\n" |
| 102 |
" * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\n" |
| 103 |
" * 02111-1307, USA.\n" |
| 104 |
" *\n" |
| 105 |
" */\n\n"; |
| 106 |
|
| 107 |
const char* autogen = |
| 108 |
"/*\n" |
| 109 |
" * This file is automatically generated by the MkOffsetTable \n" |
| 110 |
" * program. It should not be necessary to modify this file by \n" |
| 111 |
" * hand. Please see the MkOffsetTable program for more info.\n" |
| 112 |
" *\n" |
| 113 |
" */\n\n"; |
| 114 |
|
| 115 |
|
| 116 |
int main(int argc, const char** argv) |
| 117 |
{ |
| 118 |
Arguments A(argc, argv); |
| 119 |
int split_size; |
| 120 |
string infile; |
| 121 |
A.get("--split", split_size, 10); |
| 122 |
A.get("--input", infile, ""); |
| 123 |
|
| 124 |
FILE* in = fopen(infile.c_str(), "r"); |
| 125 |
|
| 126 |
MeaningStream s(in); |
| 127 |
|
| 128 |
cout << copyright << autogen << endl; |
| 129 |
|
| 130 |
cout << "const int AiksaurusImpl::MeaningsFile::s_offsetModulus = " << split_size << ";\n\n"; |
| 131 |
|
| 132 |
cout << "const int AiksaurusImpl::MeaningsFile::s_offsetData[] = \n{\n"; |
| 133 |
|
| 134 |
cout << "\t0,\t // 0 lines\n"; |
| 135 |
|
| 136 |
int lines_read = 0; |
| 137 |
int bytes_read = 0; |
| 138 |
int max_line_length = 0; |
| 139 |
int this_line_length = 0; |
| 140 |
|
| 141 |
int x; |
| 142 |
while(s.read(x)) |
| 143 |
{ |
| 144 |
bytes_read += 2; |
| 145 |
this_line_length += 2; |
| 146 |
|
| 147 |
if (x == 0xFFFF) |
| 148 |
{ |
| 149 |
lines_read++; |
| 150 |
|
| 151 |
if (this_line_length > max_line_length) |
| 152 |
max_line_length = this_line_length; |
| 153 |
|
| 154 |
this_line_length = 0; |
| 155 |
|
| 156 |
if ((lines_read % split_size) == 0) |
| 157 |
{ |
| 158 |
cout << "\t" << bytes_read << "," |
| 159 |
<< "\t // " << lines_read << " lines\n"; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
cout << "};\n\n"; |
| 165 |
|
| 166 |
cout << "const int AiksaurusImpl::MeaningsFile::s_dataMaxLineLength = " |
| 167 |
<< max_line_length << ";\n\n"; |
| 168 |
|
| 169 |
cout << "\n// total bytes read: " << bytes_read << endl; |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|