Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. /*=============================================================================
  3.    GNU UnRTF, a command-line program to convert RTF documents to other formats.
  4.    Copyright (C) 2000,2001 Zachary Thayer Smith
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.    The author is reachable by electronic mail at tuorfa@yahoo.com.
  21. =============================================================================*/
  22.  
  23.  
  24. /*----------------------------------------------------------------------
  25.  * Module name:    word
  26.  * Author name:    Zach Smith
  27.  * Create date:    01 Sep 00
  28.  * Purpose:        Management of Word objects, which contain strings
  29.  *                 as well as other Words.
  30.  *----------------------------------------------------------------------
  31.  * Changes:
  32.  * 14 Oct 00, tuorfa@yahoo.com: fixed \fs bug (# is 2X the point size).
  33.  * 14 Oct 00, tuorfa@yahoo.com: fixed table data printing.
  34.  * 14 Oct 00, tuorfa@yahoo.com: protection against null entries in \info
  35.  * 14 Oct 00, tuorfa@yahoo.com: fixed printing of <body> again
  36.  * 14 Oct 00, tuorfa@yahoo.com: fixed closure of tables
  37.  * 15 Oct 00, tuorfa@yahoo.com: fixed font attributes preceding <tr><td>
  38.  * 15 Oct 00, tuorfa@yahoo.com: attributes now continue if >1 \cell in group
  39.  * 15 Oct 00, tuorfa@yahoo.com: fixed font-size bug, lack of </head>
  40.  *  7 Nov 00, tuorfa@yahoo.com: fixed \'## translatin bug
  41.  *  8 Apr 01, tuorfa@yahoo.com: added check for out of memory after malloc
  42.  * 21 Apr 01, tuorfa@yahoo.com: bug fixes regarding author, date
  43.  * 21 Apr 01, tuorfa@yahoo.com: added paragraph alignment
  44.  * 21 Apr 01, tuorfa@yahoo.com: fix for words getting lost after \par
  45.  * 24 Jul 01, tuorfa@yahoo.com: moved conversion code to convert.c
  46.  * 22 Sep 01, tuorfa@yahoo.com: moved word_dump to here from parse.c
  47.  * 22 Sep 01, tuorfa@yahoo.com: added function-level comment blocks
  48.  *--------------------------------------------------------------------*/
  49.  
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <ctype.h>
  54. #include <string.h>
  55.  
  56. #include "defs.h"
  57. #include "parse.h"
  58. #include "malloc.h"
  59. #include "main.h"
  60. #include "error.h"
  61. #include "word.h"
  62. #include "hash.h"
  63.  
  64.  
  65.  
  66. /* For word_dump */
  67. static int indent_level=0;
  68.  
  69.  
  70.  
  71.  
  72. /*========================================================================
  73.  * Name:        word_string
  74.  * Purpose:     Obtains the string of a Word object. This involves accessing
  75.  *              the Word hash.
  76.  * Args:        Word*.
  77.  * Returns:     String.
  78.  *=======================================================================*/
  79.  
  80. char *
  81. word_string (Word *w) {
  82.         char *str;
  83.         CHECK_PARAM_NOT_NULL(w);
  84.         if (w->hash_index) str = hash_get_string (w->hash_index);
  85.         else str = NULL;
  86.         return str;
  87. }
  88.  
  89.  
  90.  
  91. /*========================================================================
  92.  * Name:        word_new
  93.  * Purpose:     Instantiates a new Word object.
  94.  * Args:        String.
  95.  * Returns:     Word*.
  96.  *=======================================================================*/
  97.  
  98. Word *
  99. word_new (char *str) {
  100.         Word * w;
  101.  
  102.         w = (Word *) my_malloc(sizeof(Word));
  103.         if (!w)
  104.                 error_handler ("out of memory");
  105.         memset ((void*) w, 0, sizeof(Word));
  106.         if (!w) error_handler ("cannot allocate a Word");
  107.  
  108.         if (str)  w->hash_index = hash_get_index (str);
  109.         else w->hash_index = 0;
  110.  
  111.         return w;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. /*========================================================================
  118.  * Name:        word_free
  119.  * Purpose:     Deallocates a Word object.
  120.  * Args:        Word.
  121.  * Returns:     None.
  122.  *=======================================================================*/
  123.  
  124. void word_free (Word *w) {
  125.         Word *prev;
  126.         Word *w2;
  127.  
  128.         CHECK_PARAM_NOT_NULL(w);
  129.  
  130.         while (w) {
  131.                 w2 = w->child;
  132.                 if (w2)
  133.                         word_free(w2);
  134.  
  135.                 prev = w;
  136.                 w = w->next;
  137.                 my_free ((char*) prev);
  138.         }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*========================================================================
  146.  * Name:        print_indentation
  147.  * Purpose:     Prints padding for the word_dump routine.
  148.  * Args:        Identation level.
  149.  * Returns:     None.
  150.  *=======================================================================*/
  151.  
  152. static void
  153. print_indentation (int level)
  154. {
  155.         int i;
  156.  
  157.         if (level) {
  158.                 for (i=0;i<level;i+=2)
  159.                         printf (". ");
  160.         } else {
  161.                 printf ("\n-----------------------------------------------------------------------\n\n");
  162.         }
  163. }
  164.  
  165.  
  166.  
  167.  
  168. /*========================================================================
  169.  * Name:        word_dump
  170.  * Purpose:     Recursive diagnostic routine to print out a tree of words.
  171.  * Args:        Word tree.
  172.  * Returns:     None.
  173.  *=======================================================================*/
  174.  
  175. void
  176. word_dump (Word *w)
  177. {
  178.         char *s;
  179.  
  180.         CHECK_PARAM_NOT_NULL(w);
  181.  
  182.         printf ("\n");
  183.         indent_level += 2;
  184.         print_indentation (indent_level);
  185.  
  186.         while (w) {
  187.                 s = word_string (w);
  188.                 if (s) {
  189.                         printf ("\"%s\" ", s);
  190.                 } else {
  191.                         if (w->child) {
  192.                                 word_dump (w->child);
  193.                                 printf ("\n");
  194.                                 print_indentation (indent_level);
  195.                         }
  196.                         else
  197.                                 warning_handler ("Word object has no string and no children");
  198.                 }
  199.                 w = w->next;
  200.         }
  201.  
  202.         indent_level -= 2;
  203. }
  204.  
  205.  
  206.