1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
/* GTS - Library for the manipulation of triangulated surfaces
* Copyright (C) 1999 Stphane Popinet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include "gts.h"
#include "gts-private.h"
#include "config.h"
const guint gts_major_version = GTS_MAJOR_VERSION;
const guint gts_minor_version = GTS_MINOR_VERSION;
const guint gts_micro_version = GTS_MICRO_VERSION;
const guint gts_interface_age = 1;
const guint gts_binary_age = 1;
static gboolean char_in_string (char c, const char * s)
{
while (*s != '\0')
if (*(s++) == c)
return TRUE;
return FALSE;
}
static GtsFile * file_new (void)
{
GtsFile * f;
f = g_malloc (sizeof (GtsFile));
f->fp = NULL;
f->s = f->s1 = NULL;
f->curline = 1;
f->curpos = 1;
f->token = g_string_new ("");
f->type = '\0';
f->error = NULL;
f->next_token = '\0';
f->scope = f->scope_max = 0;
f->delimiters = g_strdup (" \t");
f->comments = g_strdup (GTS_COMMENTS);
f->tokens = g_strdup ("\n{}()=");
return f;
}
/**
* gts_file_new:
* @fp: a file pointer.
*
* Returns: a new #GtsFile.
*/
GtsFile * gts_file_new (FILE * fp)
{
GtsFile * f;
g_return_val_if_fail (fp != NULL, NULL);
f = file_new ();
f->fp = fp;
gts_file_next_token (f);
return f;
}
/**
* gts_file_new_from_string:
* @s: a string.
*
* Returns: a new #GtsFile.
*/
GtsFile * gts_file_new_from_string (const gchar * s)
{
GtsFile * f;
g_return_val_if_fail (s != NULL, NULL);
f = file_new ();
f->s1 = f->s = g_strdup (s);
gts_file_next_token (f);
return f;
}
/**
* gts_file_destroy:
* @f: a #GtsFile.
*
* Frees all the memory allocated for @f.
*/
void gts_file_destroy (GtsFile * f)
{
g_return_if_fail (f != NULL);
g_free (f->delimiters);
g_free (f->comments);
g_free (f->tokens);
if (f->error)
g_free (f->error);
if (f->s1)
g_free (f->s1);
g_string_free (f->token, TRUE);
g_free (f);
}
/**
* gts_file_verror:
* @f: a @GtsFile.
* @format: the standard sprintf() format string.
* @args: the list of parameters to insert into the format string.
*
* Sets the @error field of @f using g_strdup_vprintf().
*
* This function can be called only once and disables any other
* operation on @f (gts_file_close() excepted).
*/
void gts_file_verror (GtsFile * f,
const gchar * format,
va_list args)
{
g_return_if_fail (f != NULL);
g_return_if_fail (format != NULL);
g_assert (f->type != GTS_ERROR);
f->error = g_strdup_vprintf (format, args);
f->type = GTS_ERROR;
}
/**
* gts_file_error:
* @f: a @GtsFile.
* @format: the standard sprintf() format string.
* @...: the parameters to insert into the format string.
*
* Sets the @error field of @f using gts_file_verror().
*
* This function can be called only once and disables any other
* operation on @f (gts_file_close() excepted).
*/
void gts_file_error (GtsFile * f,
const gchar * format,
...)
{
va_list args;
g_return_if_fail (f != NULL);
g_return_if_fail (format != NULL);
va_start (args, format);
gts_file_verror (f, format, args);
va_end (args);
}
static gint next_char (GtsFile * f)
{
if (f->fp)
return fgetc (f->fp);
else if (*f->s == '\0')
return EOF;
return *(f->s++);
}
/**
* gts_file_getc :
* @f: a #GtsFile.
*
* Returns: the next character in @f or EOF if the end of the file is
* reached or if an error occured.
*/
gint gts_file_getc (GtsFile * f)
{
gint c;
g_return_val_if_fail (f != NULL, EOF);
if (f->type == GTS_ERROR)
return EOF;
c = next_char (f);
f->curpos++;
while (char_in_string (c, f->comments)) {
while (c != EOF && c != '\n')
c = next_char (f);
if (c == '\n') {
f->curline++;
f->curpos = 1;
c = next_char (f);
}
}
switch (c) {
case '\n':
f->curline++;
f->curpos = 1;
break;
case '{':
f->scope++;
break;
case '}':
if (f->scope == 0) {
f->line = f->curline;
f->pos = f->curpos - 1;
gts_file_error (f, "no matching opening brace");
c = EOF;
}
else
f->scope--;
}
return c;
}
/**
* gts_file_read:
* @f: a #GtsFile.
* @ptr: a pointer.
* @size: size of an element.
* @nmemb: number of elements.
*
* Reads @nmemb elements of data, each @size bytes long, from @f,
* storing them at the location given by @ptr.
*
* Returns: the number of elements read.
*/
guint gts_file_read (GtsFile * f, gpointer ptr, guint size, guint nmemb)
{
guint i, n;
gchar * p;
g_return_val_if_fail (f != NULL, 0);
g_return_val_if_fail (ptr != NULL, 0);
g_return_val_if_fail (f->fp != NULL, 0);
if (f->type == GTS_ERROR)
return 0;
n = fread (ptr, size, nmemb, f->fp);
for (i = 0, p = ptr; i < n*size; i++, p++) {
f->curpos++;
if (*p == '\n') {
f->curline++;
f->curpos = 1;
}
}
return n;
}
/**
* gts_file_getc_scope :
* @f: a #GtsFile.
*
* Returns: the next character in @f in the scope defined by
* @f->scope_max or EOF if the end of the file is reached or if an
* error occured.
*/
gint gts_file_getc_scope (GtsFile * f)
{
gint c;
g_return_val_if_fail (f != NULL, EOF);
if (f->type == GTS_ERROR)
return EOF;
if (f->scope <= f->scope_max)
c = gts_file_getc (f);
else {
c = gts_file_getc (f);
while (c != EOF && f->scope > f->scope_max)
c = gts_file_getc (f);
}
return c;
}
/**
* gts_file_next_token:
* @f: a #GtsFile.
*
* Reads next token from @f and updates its @token and @delim fields.
*/
void gts_file_next_token (GtsFile * f)
{
gint c;
gboolean in_string = FALSE;
g_return_if_fail (f != NULL);
if (f->type == GTS_ERROR)
return;
f->token->str[0] = '\0';
f->token->len = 0;
if (f->next_token != '\0') {
if (char_in_string (f->next_token, f->tokens)) {
f->line = f->curline;
f->pos = f->curpos - 1;
g_string_append_c (f->token, f->next_token);
f->type = f->next_token;
f->next_token = '\0';
return;
}
else {
c = f->next_token;
f->next_token = '\0';
}
}
else
c = gts_file_getc_scope (f);
f->type = GTS_NONE;
while (c != EOF && (!in_string || !char_in_string (c, f->delimiters))) {
if (in_string) {
if (char_in_string (c, f->tokens)) {
f->next_token = c;
break;
}
g_string_append_c (f->token, c);
}
else if (!char_in_string (c, f->delimiters)) {
in_string = TRUE;
f->line = f->curline;
f->pos = f->curpos - 1;
g_string_append_c (f->token, c);
if (char_in_string (c, f->tokens)) {
f->type = c;
break;
}
}
c = gts_file_getc_scope (f);
}
if (f->type == GTS_NONE && f->token->len > 0) {
gchar * a;
a = f->token->str;
while (*a != '\0' && char_in_string (*a, "+-")) a++;
if (*a == '\0') {
f->type = GTS_STRING;
return;
}
a = f->token->str;
while (*a != '\0' && char_in_string (*a, "+-0123456789")) a++;
if (*a == '\0') {
f->type = GTS_INT;
return;
}
a = f->token->str;
while (*a != '\0' && char_in_string (*a, "+-eE.")) a++;
if (*a == '\0') {
f->type = GTS_STRING;
return;
}
a = f->token->str;
while (*a != '\0' && char_in_string (*a, "+-0123456789eE.")) a++;
if (*a == '\0') {
f->type = GTS_FLOAT;
return;
}
a = f->token->str;
if (!strncmp (a, "0x", 2) ||
!strncmp (a, "-0x", 3) ||
!strncmp (a, "+0x", 3)) {
while (*a != '\0' && char_in_string (*a, "+-0123456789abcdefx")) a++;
if (*a == '\0') {
f->type = GTS_INT;
return;
}
a = f->token->str;
while (*a != '\0' && char_in_string (*a, "+-0123456789abcdefx.p")) a++;
if (*a == '\0') {
f->type = GTS_FLOAT;
return;
}
}
f->type = GTS_STRING;
}
}
/**
* gts_file_first_token_after:
* @f: a #GtsFile.
* @type: a #GtsTokenType.
*
* Finds and sets the first token of a type different from @type
* occuring after a token of type @type.
*/
void gts_file_first_token_after (GtsFile * f, GtsTokenType type)
{
g_return_if_fail (f != NULL);
while (f->type != GTS_ERROR &&
f->type != GTS_NONE &&
f->type != type)
gts_file_next_token (f);
while (f->type == type)
gts_file_next_token (f);
}
/**
* gts_file_assign_start:
* @f: a #GtsFile.
* @vars: a %GTS_NONE terminated array of #GtsFileVariable.
*
* Opens a block delimited by braces to read a list of optional
* arguments specified by @vars.
*
* If an error is encountered the @error field of @f is set.
*/
void gts_file_assign_start (GtsFile * f, GtsFileVariable * vars)
{
GtsFileVariable * var;
g_return_if_fail (f != NULL);
g_return_if_fail (vars != NULL);
var = vars;
while (var->type != GTS_NONE)
(var++)->set = FALSE;
if (f->type != '{') {
gts_file_error (f, "expecting an opening brace");
return;
}
f->scope_max++;
gts_file_next_token (f);
}
/**
* gts_file_assign_next:
* @f: a #GtsFile.
* @vars: a %GTS_NONE terminated array of #GtsFileVariable.
*
* Assigns the next optional argument of @vars read from @f.
*
* Returns: the variable of @vars which has been assigned or %NULL if
* no variable has been assigned (if an error has been encountered the
* @error field of @f is set).
*/
GtsFileVariable * gts_file_assign_next (GtsFile * f, GtsFileVariable * vars)
{
GtsFileVariable * var;
gboolean found = FALSE;
g_return_val_if_fail (f != NULL, NULL);
g_return_val_if_fail (vars != NULL, NULL);
while (f->type == '\n')
gts_file_next_token (f);
if (f->type == '}') {
f->scope_max--;
gts_file_next_token (f);
return NULL;
}
if (f->type == GTS_ERROR)
return NULL;
var = vars;
while (f->type != GTS_ERROR && var->type != GTS_NONE && !found) {
if (!strcmp (var->name, f->token->str)) {
found = TRUE;
if (var->unique && var->set)
gts_file_error (f, "variable `%s' was already set at line %d:%d",
var->name, var->line, var->pos);
else {
var->line = f->line;
var->pos = f->pos;
gts_file_next_token (f);
if (f->type != '=')
gts_file_error (f, "expecting `='");
else {
var->set = TRUE;
switch (var->type) {
case GTS_FILE:
break;
case GTS_INT:
gts_file_next_token (f);
if (f->type != GTS_INT) {
gts_file_error (f, "expecting an integer");
var->set = FALSE;
}
else if (var->data)
*((gint *) var->data) = atoi (f->token->str);
break;
case GTS_UINT:
gts_file_next_token (f);
if (f->type != GTS_INT) {
gts_file_error (f, "expecting an integer");
var->set = FALSE;
}
else if (var->data)
*((guint *) var->data) = atoi (f->token->str);
break;
case GTS_FLOAT:
gts_file_next_token (f);
if (f->type != GTS_INT && f->type != GTS_FLOAT) {
gts_file_error (f, "expecting a number");
var->set = FALSE;
}
else if (var->data)
*((gfloat *) var->data) = atof (f->token->str);
break;
case GTS_DOUBLE:
gts_file_next_token (f);
if (f->type != GTS_INT && f->type != GTS_FLOAT) {
gts_file_error (f, "expecting a number");
var->set = FALSE;
}
else if (var->data)
*((gdouble *) var->data) = atof (f->token->str);
break;
case GTS_STRING:
gts_file_next_token (f);
if (f->type != GTS_INT &&
f->type != GTS_FLOAT &&
f->type != GTS_STRING) {
gts_file_error (f, "expecting a string");
var->set = FALSE;
}
else if (var->data)
*((gchar **) var->data) = g_strdup (f->token->str);
break;
default:
g_assert_not_reached ();
}
}
}
}
else
var++;
}
if (!found)
gts_file_error (f, "unknown identifier `%s'", f->token->str);
else if (f->type != GTS_ERROR) {
g_assert (var->set);
gts_file_next_token (f);
return var;
}
return NULL;
}
/**
* gts_file_assign_variables:
* @f: a #GtsFile.
* @vars: an array of #GtsFileVariable.
*
* Assigns all the variables belonging to @vars found in @f.
*
* If an error is encountered the @error field of @f is set.
*/
void gts_file_assign_variables (GtsFile * f, GtsFileVariable * vars)
{
g_return_if_fail (f != NULL);
g_return_if_fail (vars != NULL);
gts_file_assign_start (f, vars);
while (gts_file_assign_next (f, vars))
;
}
/**
* gts_file_variable_error:
* @f: a #GtsFile.
* @vars: an array of #GtsFileVariable.
* @name: the name of a variable in @vars.
* @format: the standard sprintf() format string.
* @...: the parameters to insert into the format string.
*
* Sets the @error field of @f using gts_file_verror().
*
* String @name must match one of the variable names in @vars.
*
* If variable @name has been assigned (using gts_file_assign_variables())
* sets the @line and @pos fields of @f to the line and position where
* it has been assigned.
*/
void gts_file_variable_error (GtsFile * f,
GtsFileVariable * vars,
const gchar * name,
const gchar * format,
...)
{
va_list args;
GtsFileVariable * var;
g_return_if_fail (f != NULL);
g_return_if_fail (vars != NULL);
g_return_if_fail (name != NULL);
g_return_if_fail (format != NULL);
var = vars;
while (var->type != GTS_NONE && strcmp (var->name, name))
var++;
g_return_if_fail (var->type != GTS_NONE); /* @name not found in @vars */
if (var->set) {
f->line = var->line;
f->pos = var->pos;
}
va_start (args, format);
gts_file_verror (f, format, args);
va_end (args);
}
#ifdef DEBUG_FUNCTIONS
static GHashTable * ids = NULL;
static guint next_id = 1;
guint id (gpointer p)
{
g_return_val_if_fail (p != NULL, 0);
g_return_val_if_fail (ids != NULL, 0);
g_assert (g_hash_table_lookup (ids, p));
return GPOINTER_TO_UINT (g_hash_table_lookup (ids, p));
}
void id_insert (gpointer p)
{
g_return_if_fail (p != NULL);
if (ids == NULL) ids = g_hash_table_new (NULL, NULL);
g_assert (g_hash_table_lookup (ids, p) == NULL);
g_hash_table_insert (ids, p, GUINT_TO_POINTER (next_id++));
}
void id_remove (gpointer p)
{
g_assert (g_hash_table_lookup (ids, p));
g_hash_table_remove (ids, p);
}
void gts_write_triangle (GtsTriangle * t,
GtsPoint * o,
FILE * fptr)
{
gdouble xo = o ? o->x : 0.0;
gdouble yo = o ? o->y : 0.0;
gdouble zo = o ? o->z : 0.0;
g_return_if_fail (t != NULL && fptr != NULL);
fprintf (fptr, "(hdefine geometry \"t%d\" { =\n", id (t));
fprintf (fptr, "OFF 3 1 0\n"
"%g %g %g\n%g %g %g\n%g %g %g\n3 0 1 2\n})\n"
"(geometry \"t%d\" { : \"t%d\"})\n"
"(normalization \"t%d\" none)\n",
GTS_POINT (GTS_SEGMENT (t->e1)->v1)->x - xo,
GTS_POINT (GTS_SEGMENT (t->e1)->v1)->y - yo,
GTS_POINT (GTS_SEGMENT (t->e1)->v1)->z - zo,
GTS_POINT (GTS_SEGMENT (t->e1)->v2)->x - xo,
GTS_POINT (GTS_SEGMENT (t->e1)->v2)->y - yo,
GTS_POINT (GTS_SEGMENT (t->e1)->v2)->z - zo,
GTS_POINT (gts_triangle_vertex (t))->x - xo,
GTS_POINT (gts_triangle_vertex (t))->y - yo,
GTS_POINT (gts_triangle_vertex (t))->z - zo,
id (t), id (t), id (t));
}
void gts_write_segment (GtsSegment * s,
GtsPoint * o,
FILE * fptr)
{
gdouble xo = o ? o->x : 0.0;
gdouble yo = o ? o->y : 0.0;
gdouble zo = o ? o->z : 0.0;
g_return_if_fail (s != NULL && fptr != NULL);
fprintf (fptr, "(geometry \"s%d\" { =\n", id (s));
fprintf (fptr, "VECT 1 2 0 2 0 %g %g %g %g %g %g })\n"
"(normalization \"s%d\" none)\n",
GTS_POINT (s->v1)->x - xo,
GTS_POINT (s->v1)->y - yo,
GTS_POINT (s->v1)->z - zo,
GTS_POINT (s->v2)->x - xo,
GTS_POINT (s->v2)->y - yo,
GTS_POINT (s->v2)->z - zo,
id (s));
}
#endif /* DEBUG_FUNCTIONS */
|