home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / TEXT.C < prev    next >
C/C++ Source or Header  |  1993-11-16  |  8KB  |  304 lines

  1. /*  $Id: text.c,v 1.1 1993/07/13 10:11:40 anjo Exp $
  2.  *  
  3.  *  File    text.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    Printing text line by line
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    04/07/93  (Created)
  12.  *          11/07/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22.  
  23. /*------------------------------------------------------------
  24.  *  Initialisation
  25.  *------------------------------------------------------------*/
  26.  
  27. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  28. @node newTextBuffer
  29. @deftypefun TextBuffer newTextBuffer (FILE *@var{fd}, int @var{columns})
  30. Creates a new text-buffer associated with file @var{fd} and with a
  31. capacity of @var{columns} characters.
  32. @end deftypefun
  33. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34.  
  35. TextBuffer
  36. newTextBuffer(FILE *fd, int columns)
  37. { TextBuffer tb;
  38.  
  39.   tb = alloc(sizeof(struct textbuffer));
  40.   
  41.   tb->file = fd;
  42.   tb->columns = columns;
  43.   tb->maxColumns = columns + 100;
  44.   tb->line = alloc(sizeof(char) * (tb->maxColumns+1));
  45.   tb->current = tb->line;
  46.   tb->count = 0;
  47.   tb->lastSpace = -1;
  48.   
  49.   return tb;
  50. }
  51.  
  52.  
  53. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54. @node freeTextBuffer
  55. @deftypefun void freeTextBuffer (TextBuffer @var{tb})
  56. First prints any remaining text in @var{tb} (using @code{flushTextBuffer})
  57. and then reclaims the memory associated with text-buffer @var{tb}.
  58. @end deftypefun
  59. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  60.  
  61. void
  62. freeTextBuffer(TextBuffer tb)
  63. { if (tb->count > 0)
  64.     flushTextBuffer(tb);
  65.   unalloc(tb->line);
  66.   unalloc(tb);
  67. }
  68.  
  69.  
  70. /*------------------------------------------------------------
  71.  *  Adding text
  72.  *------------------------------------------------------------*/
  73.  
  74. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  75. @node stringTextBuffer
  76. @deftypefun void stringTextBuffer (TextBuffer @var{tb}, char *@var{s})
  77. Appends the string @var{s} to the text-buffer @var{tb}.  If this overflows
  78. the text-buffer, the current line will be written to the file associated with
  79. the text-buffer (broken at a white space character).
  80. @end deftypefun
  81. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  82.  
  83. void
  84. stringTextBuffer(TextBuffer tb, char *s)
  85. { char *t = tb->current;
  86.   int count = tb->count;
  87.  
  88.   while (*s)
  89.   { *t = '\0';
  90.     if (count >= tb->columns)
  91.     { if (isspace(*s))            /* No need for special actions */
  92.       { flushTextBuffer(tb);
  93.     count = tb->count;
  94.     t = tb->current;
  95.     tb->lastSpace = -1;
  96.     s++;
  97.     continue;
  98.       }
  99.  
  100.       if (tb->lastSpace != -1)        /* Break at last space seen */
  101.       { tb->line[tb->lastSpace] = '\0';
  102.     flushTextBuffer(tb);
  103.     strcpy(tb->line, &tb->line[tb->lastSpace+1]);
  104.     count = tb->count = strlen(tb->line);
  105.     t = tb->current = &tb->line[count];
  106.     tb->lastSpace = -1;
  107.     continue;
  108.       }
  109.  
  110.                     /* Line too long */
  111.       *t++ = *s++;
  112.       count++;
  113.       if (count > tb->maxColumns)
  114.       { fprintf(stderr, "** Internal error: Extremely long line (%d chars)\n",
  115.         count);
  116.     exit(1);
  117.       }
  118.       continue;
  119.     }
  120.  
  121.     if (*s == '\n')
  122.     { flushTextBuffer(tb);
  123.       count = tb->count;
  124.       t = tb->current;
  125.       s++;
  126.       continue;
  127.     }
  128.  
  129.     if (isspace(*s))
  130.       tb->lastSpace = count;
  131.  
  132.     if (*s == '\t')
  133.     { *t++ = '\t';
  134.       count = (count+8)/8;
  135.       s++;
  136.       continue;
  137.     }
  138.  
  139.     *t++ = *s++;
  140.     count++;
  141.   }
  142.   *t = '\0';
  143.   tb->count = count;
  144.   tb->current = t;
  145. }
  146.  
  147.  
  148. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  149. @node stringSpaceTextBuffer
  150. @deftypefun void stringSpaceTextBuffer (TextBuffer @var{tb}, char *@var{s})
  151. Similar to @code{stringTextBuffer} but ensures that there is a space
  152. character at the end of @var{tb} before appending the string @var{s}.
  153. @end deftypefun
  154. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  155.  
  156. void
  157. stringSpaceTextBuffer(TextBuffer tb, char *s)
  158. { if (tb->count > 0 && !isspace(*tb->current))
  159.     stringTextBuffer(tb, " ");
  160.   stringTextBuffer(tb, s);
  161. }
  162.  
  163.  
  164. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  165. @node stringNewlineTextBuffer
  166. @deftypefun void stringNewlineTextBuffer (TextBuffer @var{tb}, char *@var{s})
  167. Similar to @code{stringTextBuffer} but ensures that string @var{s}
  168. starts on a new line.
  169. @end deftypefun
  170. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  171.  
  172. void
  173. stringNewlineTextBuffer(TextBuffer tb, char *s)
  174. { if (tb->count > 0)
  175.     stringTextBuffer(tb, "\n");
  176.   stringTextBuffer(tb, s);
  177. }
  178.  
  179.  
  180. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  181. @node stringRawTextBuffer
  182. @deftypefun void stringRawTextBuffer (TextBuffer @var{tb}, char *@var{s})
  183. First flushes the text-buffer (writing any remaining text to the
  184. file) and then appends string @var{s} to the file as well.  This
  185. is useful for text that has to be put on a single line that would
  186. otherwise overflow the line.
  187. @end deftypefun
  188. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  189.  
  190. void
  191. stringRawTextBuffer(TextBuffer tb, char *s)
  192. { if (tb->count)
  193.     flushTextBuffer(tb);
  194.   fprintf(tb->file, "%s\n", s);
  195. }
  196.  
  197.  
  198. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  199. @node formatTextBuffer
  200. @deftypefun void formatTextBuffer (TextBuffer @var{tb}, char *@var{format}, @dots{})
  201. @code{formatTextBuffer} is similar @code{printf} but operates on
  202. text-buffer @var{tb}.  An example is:
  203. @example
  204. @{ TextBuffer tb;
  205.   int count;
  206.  
  207.   ...
  208.   formatTextBuffer(tb, "Count %d lines", count);
  209. @}
  210. @end example
  211. @end deftypefun
  212. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  213.  
  214. void
  215. formatTextBuffer(TextBuffer tb, char *format, ...)
  216. { va_list args;
  217.   char buf[MAX_LINE_SIZE+1];
  218.  
  219.   va_start(args, format);
  220.   vsprintf(buf, format, args);
  221.   stringTextBuffer(tb, buf);
  222.   va_end(args);
  223. }
  224.  
  225.  
  226. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  227. @node formatSpaceTextBuffer
  228. @deftypefun void formatSpaceTextBuffer (TextBuffer @var{tb}, char *@var{format}, @dots{})
  229. Similar to @code{formatTextBuffer} but ensures a space character precedes
  230. the output.
  231. @end deftypefun
  232. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  233.  
  234. void
  235. formatSpaceTextBuffer(TextBuffer tb, char *format, ...)
  236. { va_list args;
  237.   char buf[MAX_LINE_SIZE+1];
  238.  
  239.   va_start(args, format);
  240.   vsprintf(buf, format, args);
  241.   stringSpaceTextBuffer(tb, buf);
  242.   va_end(args);
  243. }
  244.  
  245.  
  246. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  247. @node formatNewlineTextBuffer
  248. @deftypefun void formatNewlineTextBuffer (TextBuffer @var{tb}, char *@var{format}, @dots{})
  249. Similar to @code{formatTextBuffer} but ensures output starts on a newline.
  250. @end deftypefun
  251. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  252.  
  253. void
  254. formatNewlineTextBuffer(TextBuffer tb, char *format, ...)
  255. { va_list args;
  256.   char buf[MAX_LINE_SIZE+1];
  257.  
  258.   va_start(args, format);
  259.   vsprintf(buf, format, args);
  260.   stringNewlineTextBuffer(tb, buf);
  261.   va_end(args);
  262. }
  263.  
  264.  
  265. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  266. @node formatRawTextBuffer
  267. @deftypefun void formatRawTextBuffer (TextBuffer @var{tb}, char *@var{format}, @dots{})
  268. Similar to @code{stringRawTextBuffer}, but using @code{printf} style
  269. arguments.
  270. @end deftypefun
  271. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  272.  
  273. void
  274. formatRawTextBuffer(TextBuffer tb, char *format, ...)
  275. { va_list args;
  276.   char buf[MAX_LINE_SIZE+1];
  277.  
  278.   va_start(args, format);
  279.   vsprintf(buf, format, args);
  280.   stringRawTextBuffer(tb, buf);
  281.   va_end(args);
  282. }
  283.  
  284.  
  285. /*------------------------------------------------------------
  286.  *  Private functions
  287.  *------------------------------------------------------------*/
  288.  
  289. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  290. @node flushTextBuffer
  291. @deftypefun void flushTextBuffer (TextBuffer @var{tb})
  292. Flushes the contents of the text-buffer @var{tb} to the associated
  293. file.  This function is not normally used by applications.
  294. @end deftypefun
  295. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  296.  
  297. void
  298. flushTextBuffer(TextBuffer tb)
  299. { fprintf(tb->file, "%s\n", tb->line);
  300.   tb->count = 0;
  301.   tb->current = tb->line;
  302.   tb->line[0] = '\0';
  303. }
  304.