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
|
/*
This file is part of pybliographer
Copyright (C) 1998-1999 Frederic GOBRY
Email : gobry@idiap.ch
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: struct.c,v 1.1.2.2 2003/09/02 14:35:33 fredgo Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <strings.h>
#include <ctype.h>
#include "bibtex.h"
static GMemChunk * struct_chunk = NULL;
BibtexStruct *
bibtex_struct_new (BibtexStructType type) {
BibtexStruct * s;
if (struct_chunk == NULL) {
struct_chunk = g_mem_chunk_new ("BibtexStruct",
sizeof (BibtexStruct),
sizeof (BibtexStruct) * 16,
G_ALLOC_AND_FREE);
}
s = g_chunk_new (BibtexStruct, struct_chunk);
s->type = type;
switch (type) {
case BIBTEX_STRUCT_LIST:
s->value.list = NULL;
break;
case BIBTEX_STRUCT_TEXT:
s->value.text = NULL;
break;
case BIBTEX_STRUCT_REF:
s->value.ref = NULL;
break;
case BIBTEX_STRUCT_COMMAND:
s->value.com = NULL;
break;
case BIBTEX_STRUCT_SUB:
s->value.sub = g_new (BibtexStructSub, 1);
s->value.sub->content = NULL;
s->value.sub->encloser = BIBTEX_ENCLOSER_BRACE;
break;
case BIBTEX_STRUCT_SPACE:
s->value.unbreakable = FALSE;
break;
default:
g_assert_not_reached ();
}
return s;
}
void
bibtex_struct_destroy (BibtexStruct * s,
gboolean remove_content) {
GList * current;
g_return_if_fail (s != NULL);
switch (s->type) {
case BIBTEX_STRUCT_TEXT:
if (remove_content)
g_free (s->value.text);
break;
case BIBTEX_STRUCT_REF:
if (remove_content)
g_free (s->value.ref);
break;
case BIBTEX_STRUCT_COMMAND:
if (remove_content)
g_free (s->value.com);
break;
case BIBTEX_STRUCT_LIST:
if (remove_content) {
current = s->value.list;
while (current) {
bibtex_struct_destroy ((BibtexStruct *) current->data,
remove_content);
current = current->next;
}
}
g_list_free (s->value.list);
break;
case BIBTEX_STRUCT_SUB:
if (remove_content)
bibtex_struct_destroy ((BibtexStruct *) s->value.sub->content,
remove_content);
g_free (s->value.sub);
break;
case BIBTEX_STRUCT_SPACE:
break;
default:
g_assert_not_reached ();
break;
}
g_chunk_free (s, struct_chunk);
}
/* -------------------------------------------------- */
void
bibtex_struct_display (BibtexStruct * source) {
GList * list;
g_return_if_fail (source != NULL);
switch (source->type) {
case BIBTEX_STRUCT_TEXT:
printf ("Text(%s)", source->value.text);
break;
case BIBTEX_STRUCT_REF:
printf ("Ref(%s)", source->value.ref);
break;
case BIBTEX_STRUCT_COMMAND:
printf ("Command(%s)", source->value.com);
break;
case BIBTEX_STRUCT_SUB:
printf ("Sub(");
bibtex_struct_display (source->value.sub->content);
printf (")");
break;
case BIBTEX_STRUCT_LIST:
printf ("List(");
list = source->value.list;
while (list) {
bibtex_struct_display ((BibtexStruct *) list->data);
list = list->next;
}
printf (")\n");
break;
case BIBTEX_STRUCT_SPACE:
printf ("Space()");
break;
default:
printf ("Argggg(%d)", source->type);
}
}
BibtexStruct *
bibtex_struct_copy (BibtexStruct * source) {
BibtexStruct * target;
GList * list;
g_return_val_if_fail (source != NULL, NULL);
target = bibtex_struct_new (source->type);
switch (source->type) {
case BIBTEX_STRUCT_TEXT:
target->value.text = g_strdup (source->value.text);
break;
case BIBTEX_STRUCT_REF:
target->value.ref = g_strdup (source->value.ref);
break;
case BIBTEX_STRUCT_COMMAND:
target->value.com = g_strdup (source->value.com);
break;
case BIBTEX_STRUCT_SUB:
target->value.sub->encloser = source->value.sub->encloser;
target->value.sub->content =
bibtex_struct_copy (source->value.sub->content);
break;
case BIBTEX_STRUCT_LIST:
list = source->value.list;
while (list) {
target->value.list =
g_list_append (target->value.list,
bibtex_struct_copy ((BibtexStruct *)
list->data));
list = list->next;
}
break;
case BIBTEX_STRUCT_SPACE:
target->value.unbreakable = source->value.unbreakable;
break;
default:
g_assert_not_reached ();
break;
}
return target;
}
BibtexStruct *
bibtex_struct_append (BibtexStruct * s1,
BibtexStruct * s2) {
BibtexStruct * ret;
gchar * tmp;
if (s1 == NULL && s2 == NULL) return NULL;
if (s1 == NULL) return s2;
if (s2 == NULL) return s1;
if (s1->type == BIBTEX_STRUCT_TEXT &&
s2->type == BIBTEX_STRUCT_TEXT) {
/* Fusion de deux bouts de texte */
tmp = s1->value.text;
s1->value.text = g_strconcat (s1->value.text,
s2->value.text,
NULL);
g_free (tmp);
bibtex_struct_destroy (s2, TRUE);
return (s1);
}
if (s1->type == BIBTEX_STRUCT_LIST &&
s2->type == BIBTEX_STRUCT_LIST) {
/* Fusion de deux listes simples */
s1->value.list = g_list_concat (s1->value.list,
s2->value.list);
bibtex_struct_destroy (s2, FALSE);
return (s1);
}
/* One or the other is a list... */
if (s1->type == BIBTEX_STRUCT_LIST) {
/* append the second to the first... */
s1->value.list = g_list_append (s1->value.list, s2);
return (s1);
}
if (s2->type == BIBTEX_STRUCT_LIST) {
/* append the second to the first... */
s2->value.list = g_list_prepend (s2->value.list, s1);
return (s2);
}
/* Dans le cas general, creer une nouvelle liste contenant seulement les
deux elements passes en entree */
ret = bibtex_struct_new (BIBTEX_STRUCT_LIST);
ret->value.list = g_list_append (ret->value.list, s1);
ret->value.list = g_list_append (ret->value.list, s2);
return ret;
}
static gchar *
bibtex_real_string (BibtexStruct * s,
BibtexFieldType type,
GHashTable * dico,
gboolean as_bibtex,
gint level,
gboolean * loss,
gboolean at_beginning,
gboolean strip_first_layer,
gboolean as_latex) {
gchar * text = NULL, * tmp;
GList * list;
BibtexStruct * tmp_s;
gboolean first;
GString * string;
g_return_val_if_fail (s != NULL, NULL);
switch (s->type) {
case BIBTEX_STRUCT_SPACE:
if (as_bibtex || type == BIBTEX_VERBATIM) {
if (s->value.unbreakable) {
text = g_strdup ("~");
}
else {
text = g_strdup (" ");
}
}
else {
/* unbreakable spaces are lost */
if (s->value.unbreakable) {
if (loss) * loss = TRUE;
}
text = g_strdup (" ");
}
break;
case BIBTEX_STRUCT_COMMAND:
if (as_bibtex) {
text = g_strconcat ("\\", s->value.com, NULL);
}
else {
text = bibtex_accent_string (s, NULL, loss);
}
break;
case BIBTEX_STRUCT_TEXT:
text = g_strdup (s->value.text);
if ((! as_bibtex || as_latex) &&
level == 1 &&
type == BIBTEX_TITLE) {
if (at_beginning) {
text [0] = toupper (text [0]);
g_strdown (text + 1);
}
else {
g_strdown (text);
}
}
break;
case BIBTEX_STRUCT_REF:
g_strdown (s->value.ref);
if (as_bibtex && ! as_latex) {
text = g_strdup (s->value.ref);
}
else {
if (loss) * loss = TRUE;
if (dico) {
tmp_s = (BibtexStruct *)
g_hash_table_lookup (dico, s->value.ref);
if (tmp_s) {
text = bibtex_real_string (tmp_s, type,
dico,
as_bibtex,
level, loss, at_beginning,
strip_first_layer, as_latex);
}
else {
bibtex_warning ("reference `%s' undefined", s->value.text);
text = g_strdup ("<undefined>");
}
}
else {
text = g_strdup ("<undefined>");
}
}
break;
case BIBTEX_STRUCT_LIST:
string = g_string_new ("");
list = s->value.list;
first = TRUE;
while (list != NULL) {
tmp_s = (BibtexStruct *) list->data;
list = list->next;
if (! as_bibtex &&
tmp_s->type == BIBTEX_STRUCT_COMMAND) {
/* Passer a la fonction le flot suivant */
tmp = bibtex_accent_string (tmp_s, & list, loss);
g_string_append (string, tmp);
g_free (tmp);
}
else {
if (level == 0 && as_bibtex && ! first && ! as_latex) {
g_string_append (string, " # ");
}
tmp = bibtex_real_string(tmp_s, type, dico, as_bibtex, level,
loss, at_beginning && first,
strip_first_layer, as_latex);
g_string_append(string, tmp);
g_free(tmp);
}
first = FALSE;
}
text = string->str;
g_string_free (string, FALSE);
break;
case BIBTEX_STRUCT_SUB:
if (as_bibtex) {
tmp = bibtex_real_string (s->value.sub->content,
type, dico,
as_bibtex,
/* climb one level if we are in pure
bibtex, or if we are in latex and {} */
level + 1,
loss, at_beginning, FALSE, as_latex);
if (strip_first_layer) {
text = tmp;
}
else {
/* As bibtex, add the encloser */
switch (s->value.sub->encloser) {
case BIBTEX_ENCLOSER_BRACE:
text = g_strdup_printf ("{%s}", tmp);
break;
case BIBTEX_ENCLOSER_QUOTE:
text = g_strdup_printf ("\"%s\"", tmp);
break;
default:
g_assert_not_reached ();
break;
}
g_free (tmp);
}
}
else {
/* As simple text, add nothing. */
text = bibtex_real_string (s->value.sub->content,
type, dico,
as_bibtex,
level + 1,
loss, at_beginning, FALSE, as_latex);
}
break;
default:
g_assert_not_reached ();
break;
}
return text;
}
BibtexStruct *
bibtex_struct_flatten (BibtexStruct * s) {
GList * list, * newlist, * tmp;
BibtexStruct * tmp_s;
gboolean stable;
g_return_val_if_fail (s != NULL, NULL);
switch (s->type) {
case BIBTEX_STRUCT_LIST:
/* Aplanir une liste */
stable = FALSE;
while (! stable) {
stable = TRUE;
list = s->value.list;
newlist = NULL;
while (list) {
tmp_s = (BibtexStruct *) list->data;
/* Si on trouve une liste dans la liste... */
if (tmp_s->type == BIBTEX_STRUCT_LIST) {
/* Il faudra repasser un coup apres... */
stable = FALSE;
tmp = tmp_s->value.list;
/* Incorporer tous les elements de la deuxieme liste */
while (tmp) {
newlist = g_list_append (newlist, tmp->data);
tmp = tmp->next;
}
/* ...et detruire l'ancien element */
bibtex_struct_destroy (tmp_s, FALSE);
}
else {
newlist = g_list_append (newlist,
bibtex_struct_flatten (tmp_s));
}
list = list->next;
}
g_list_free (s->value.list);
s->value.list = newlist;
}
break;
case BIBTEX_STRUCT_SUB:
s->value.sub->content =
bibtex_struct_flatten (s->value.sub->content);
break;
default:
break;
}
return s;
}
gchar *
bibtex_struct_as_string (BibtexStruct * s,
BibtexFieldType type,
GHashTable * dico,
gboolean * loss) {
g_return_val_if_fail (s != NULL, NULL);
return bibtex_real_string (s, type, dico, FALSE, 0, loss, TRUE,
FALSE, FALSE);
}
gchar *
bibtex_struct_as_bibtex (BibtexStruct * s) {
g_return_val_if_fail (s != NULL, NULL);
return bibtex_real_string (s, BIBTEX_OTHER, NULL, TRUE, 0, NULL, TRUE,
FALSE, FALSE);
}
gchar *
bibtex_struct_as_latex (BibtexStruct * s,
BibtexFieldType type,
GHashTable * dico) {
g_return_val_if_fail (s != NULL, NULL);
return bibtex_real_string (s, type, dico, TRUE, 0, NULL, TRUE,
TRUE, TRUE);
}
|