| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library 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 GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <cstdlib> |
| 38 |
#include <ctype.h> |
| 39 |
#include "Man.h" |
| 40 |
|
| 41 |
///////////////////////////////////////////////////////////////////////////// |
| 42 |
// |
| 43 |
// Class: Reader |
| 44 |
// |
| 45 |
///////////////////////////////////////////////////////////////////////////// |
| 46 |
|
| 47 |
FILE *Reader::fp; |
| 48 |
int Reader::lineNum; |
| 49 |
char *Reader::fullName = NULL; |
| 50 |
|
| 51 |
#define BUFSIZE 1024 |
| 52 |
|
| 53 |
SbBool |
| 54 |
Reader::open(const char *pathName) |
| 55 |
{ |
| 56 |
if (fullName != NULL) |
| 57 |
free(fullName); |
| 58 |
|
| 59 |
fullName = strdup(pathName); |
| 60 |
|
| 61 |
fp = fopen(fullName, "r"); |
| 62 |
|
| 63 |
if (fp == NULL) { |
| 64 |
fprintf(stderr, "Couldn't open file \"%s\"\n", fullName); |
| 65 |
return FALSE; |
| 66 |
} |
| 67 |
|
| 68 |
lineNum = 1; |
| 69 |
|
| 70 |
return TRUE; |
| 71 |
} |
| 72 |
|
| 73 |
SbBool |
| 74 |
Reader::open(const char *dirName, const char *fileName) |
| 75 |
{ |
| 76 |
SbString name = dirName; |
| 77 |
|
| 78 |
name += "/"; |
| 79 |
name += fileName; |
| 80 |
|
| 81 |
return open(name.getString()); |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
Reader::error(const char *what) |
| 86 |
{ |
| 87 |
fprintf(stderr, "Error reading %s on line %d of \"%s\"\n", |
| 88 |
what, lineNum, fullName); |
| 89 |
} |
| 90 |
|
| 91 |
SbBool |
| 92 |
Reader::readName(SbName &name) |
| 93 |
{ |
| 94 |
char buf[132]; |
| 95 |
|
| 96 |
if (! readBuf(buf, TRUE)) |
| 97 |
return FALSE; |
| 98 |
|
| 99 |
name = buf; |
| 100 |
return TRUE; |
| 101 |
} |
| 102 |
|
| 103 |
SbBool |
| 104 |
Reader::readString(SbString &string) |
| 105 |
{ |
| 106 |
char buf[2048]; |
| 107 |
|
| 108 |
if (! readBuf(buf, FALSE)) |
| 109 |
return FALSE; |
| 110 |
|
| 111 |
string = buf; |
| 112 |
return TRUE; |
| 113 |
} |
| 114 |
|
| 115 |
SbBool |
| 116 |
Reader::readBracketed(SbString &string) |
| 117 |
{ |
| 118 |
char buf[BUFSIZE]; |
| 119 |
int c, c2, i = 0; |
| 120 |
|
| 121 |
string.makeEmpty(); |
| 122 |
|
| 123 |
skipWhiteSpace(); |
| 124 |
|
| 125 |
// Read open brace character |
| 126 |
if (get() != OPEN_CHAR) |
| 127 |
return FALSE; |
| 128 |
|
| 129 |
skipWhiteSpace(); |
| 130 |
|
| 131 |
// Read characters into buffer until close brace or EOF |
| 132 |
while ((c = get()) != EOF && c != CLOSE_CHAR) { |
| 133 |
|
| 134 |
// Check for backslash-open/close sequence |
| 135 |
if (c == '\\') { |
| 136 |
if ((c2 = get()) == OPEN_CHAR || c2 == CLOSE_CHAR) |
| 137 |
c = c2; |
| 138 |
else |
| 139 |
putBack(c2); |
| 140 |
} |
| 141 |
|
| 142 |
buf[i++] = c; |
| 143 |
|
| 144 |
// If the buffer is full, add it to the string and get ready |
| 145 |
// for more |
| 146 |
if (i == BUFSIZE - 1) { |
| 147 |
buf[i] = '\0'; |
| 148 |
string += buf; |
| 149 |
i = 0; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (c != CLOSE_CHAR) |
| 154 |
return FALSE; |
| 155 |
|
| 156 |
// Add rest of buffer to string |
| 157 |
if (i > 0) { |
| 158 |
buf[i] = '\0'; |
| 159 |
string += buf; |
| 160 |
} |
| 161 |
|
| 162 |
return TRUE; |
| 163 |
} |
| 164 |
|
| 165 |
SbBool |
| 166 |
Reader::readUpTo(int endChar, SbString &string) |
| 167 |
{ |
| 168 |
char buf[BUFSIZE]; |
| 169 |
int c, i = 0; |
| 170 |
|
| 171 |
string.makeEmpty(); |
| 172 |
|
| 173 |
skipWhiteSpace(); |
| 174 |
|
| 175 |
// Read characters into buffer until given char or EOF |
| 176 |
while ((c = get()) != EOF && c != endChar) { |
| 177 |
|
| 178 |
buf[i++] = c; |
| 179 |
|
| 180 |
// If the buffer is full, add it to the string and get ready |
| 181 |
// for more |
| 182 |
if (i == BUFSIZE - 1) { |
| 183 |
buf[i] = '\0'; |
| 184 |
string += buf; |
| 185 |
i = 0; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if (c != endChar) |
| 190 |
return FALSE; |
| 191 |
|
| 192 |
putBack(c); |
| 193 |
|
| 194 |
// Add rest of buffer to string |
| 195 |
if (i > 0) { |
| 196 |
buf[i] = '\0'; |
| 197 |
string += buf; |
| 198 |
} |
| 199 |
|
| 200 |
return TRUE; |
| 201 |
} |
| 202 |
|
| 203 |
SbBool |
| 204 |
Reader::readBuf(char *buf, SbBool isName) |
| 205 |
{ |
| 206 |
int c; |
| 207 |
|
| 208 |
skipWhiteSpace(); |
| 209 |
|
| 210 |
// See if stuff is quoted |
| 211 |
if ((c = get()) == QUOTE_CHAR) { |
| 212 |
char *b = buf; |
| 213 |
|
| 214 |
c = get(); |
| 215 |
|
| 216 |
while (c != EOF && c != QUOTE_CHAR) { |
| 217 |
*b++ = c; |
| 218 |
c = get(); |
| 219 |
} |
| 220 |
*b = '\0'; |
| 221 |
|
| 222 |
if (c != QUOTE_CHAR) |
| 223 |
return FALSE; |
| 224 |
} |
| 225 |
|
| 226 |
else { |
| 227 |
char *b = buf; |
| 228 |
|
| 229 |
putBack(c); |
| 230 |
|
| 231 |
// If reading a name, stop at a separator character |
| 232 |
while ((c = get()) != EOF && ! isspace(c)) { |
| 233 |
if (isName && c == SEP_CHAR) |
| 234 |
break; |
| 235 |
*b++ = c; |
| 236 |
} |
| 237 |
*b = '\0'; |
| 238 |
|
| 239 |
if (b == buf) |
| 240 |
return FALSE; |
| 241 |
|
| 242 |
if (c != EOF) |
| 243 |
putBack(c); |
| 244 |
} |
| 245 |
|
| 246 |
return TRUE; |
| 247 |
} |
| 248 |
|
| 249 |
void |
| 250 |
Reader::skipWhiteSpace() |
| 251 |
{ |
| 252 |
int c; |
| 253 |
|
| 254 |
while (1) { |
| 255 |
|
| 256 |
// Skip over space characters |
| 257 |
while ((c = get()) != EOF && isspace(c)) |
| 258 |
; |
| 259 |
|
| 260 |
// Check for comment just after the space |
| 261 |
if (c == COMMENT_CHAR) { |
| 262 |
|
| 263 |
// Skip all characters up to next newline |
| 264 |
while ((c = get()) != EOF && c != '\n') |
| 265 |
; |
| 266 |
} |
| 267 |
|
| 268 |
// No comment? Stop! |
| 269 |
else |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
// Put the last character (if any) back |
| 274 |
if (c != EOF) |
| 275 |
putBack(c); |
| 276 |
} |
| 277 |
|
| 278 |
int |
| 279 |
Reader::get() |
| 280 |
{ |
| 281 |
int c = getc(fp); |
| 282 |
|
| 283 |
if (c == '\n') |
| 284 |
lineNum++; |
| 285 |
|
| 286 |
return c; |
| 287 |
} |
| 288 |
|
| 289 |
void |
| 290 |
Reader::putBack(int c) |
| 291 |
{ |
| 292 |
if (c == '\n') |
| 293 |
lineNum--; |
| 294 |
|
| 295 |
ungetc(c, fp); |
| 296 |
} |
| 297 |
|
| 298 |
void |
| 299 |
Reader::cleanString(SbString &string, SbBool inCode, SbBool changeSbName) |
| 300 |
{ |
| 301 |
const char *s = string.getString(); |
| 302 |
int len = string.getLength(); |
| 303 |
char realbuf[2048], *buf, *b; |
| 304 |
SbString cleanString; |
| 305 |
|
| 306 |
|
| 307 |
if (len >= 2048) |
| 308 |
buf = new char[len + 1]; |
| 309 |
else |
| 310 |
buf = realbuf; |
| 311 |
|
| 312 |
// Remove leading spaces |
| 313 |
while (isspace(*s)) |
| 314 |
s++; |
| 315 |
|
| 316 |
b = buf; |
| 317 |
|
| 318 |
while (*s != '\0') { |
| 319 |
|
| 320 |
// Copy non-space stuff |
| 321 |
while (*s != '\0' && ! isspace(*s)) |
| 322 |
*b++ = *s++; |
| 323 |
|
| 324 |
// Condense spaces |
| 325 |
if (isspace(*s)) { |
| 326 |
// Don't allow spaces after open parens in code |
| 327 |
if (inCode && b > buf && *(b-1) == '(') |
| 328 |
; |
| 329 |
else |
| 330 |
*b++ = ' '; |
| 331 |
s++; |
| 332 |
while (isspace(*s)) |
| 333 |
s++; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Remove trailing space, if any |
| 338 |
if (b > buf && *(b - 1) == ' ') |
| 339 |
b--; |
| 340 |
|
| 341 |
// Terminate string |
| 342 |
*b = '\0'; |
| 343 |
|
| 344 |
// Replace "const SbName &" with "const char *" in the C version |
| 345 |
// but only if the class name is not SbName. |
| 346 |
if ((Writer::getLanguage() == C) && changeSbName && |
| 347 |
(ClassDef::getCurClass()->getName() != "SbName")) { |
| 348 |
for (b = buf; *b != '\0'; b++) |
| 349 |
if ( !strncmp(b,"const SbName &", 14) ) { |
| 350 |
b[0] = 'c'; |
| 351 |
b[1] = 'o'; |
| 352 |
b[2] = 'n'; |
| 353 |
b[3] = 's'; |
| 354 |
b[4] = 't'; |
| 355 |
b[5] = ' '; |
| 356 |
b[6] = 'c'; |
| 357 |
b[7] = 'h'; |
| 358 |
b[8] = 'a'; |
| 359 |
b[9] = 'r'; |
| 360 |
b[10]= ' '; |
| 361 |
b[11]= '*'; |
| 362 |
b[12]= ' '; |
| 363 |
b[13]= ' '; |
| 364 |
// We just created two extra spaces at the end. So we |
| 365 |
// need to move everything over. |
| 366 |
char *q = b+12; |
| 367 |
while (*q != '\0') { |
| 368 |
*q = q[2]; |
| 369 |
q++; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
// Replace references (&) with pointers (*) in the C version |
| 375 |
if (Writer::getLanguage() == C) |
| 376 |
for (b = buf; *b != '\0'; b++) |
| 377 |
if (*b == '&') |
| 378 |
*b = '*'; |
| 379 |
|
| 380 |
string = buf; |
| 381 |
|
| 382 |
if (len >= 2048) |
| 383 |
delete [] buf; |
| 384 |
} |