]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/writer.c
Some typo fixes in README
[oss/catdoc.git] / src / writer.c
1 /*****************************************************************/
2 /* Output of unicode buffers with conversion into target encoding*/
3 /* And application of substitution maps                          */
4 /*                                                               */
5 /* This file is part of catdoc project                           */
6 /* (c) Victor Wagner 1996-2003, (c) Alex Ott 2003                    */
7 /*****************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include "catdoc.h"
15 /************************************************************************/
16 /* performs paragraph formatting if wrap_margin is non-zero             */
17 /* gets character sequence and appends it to buffer. If buffer is long  */
18 /* enough, prints its beginning out                                     */
19 /* Input parameter is pointer to the string which represents one unicode*/
20 /* character after character set translation. See convert_char ina      */
21 /* charset.c                                                            */
22 /************************************************************************/
23 static char outputbuffer[LINE_BUF_SIZE]="";
24
25 int para_double_newline = 1;
26
27 void out_char(const char *chunk) {
28         static int bufpos=0;
29         int eol_flag=0;
30         const char *p; char *q;
31         if (!wrap_margin) {
32                 fputs(chunk,stdout);
33                 return;
34         }
35
36         for (q=outputbuffer+bufpos,p=chunk;
37                         *p;
38                         *(q++)=*(p++),bufpos++) {
39                         if (*p=='\n') eol_flag=1;
40         }
41         *q=0;
42                  /* This strcat is safe. wrap margin setting
43                                                          code in main.c ensures that wrap_margin is 
44                                                          less than LINE_BUF_SIZE-strlen(largest chunk)
45                                                          */  
46         if (eol_flag) {
47                 /* End of paragraph */
48                 char *q = map_subst(spec_chars,'\n');
49                 fputs(outputbuffer,stdout);
50                 *outputbuffer=0;
51                 bufpos=0;
52                 if (para_double_newline) {
53                         if (q) {
54                                  fputs(q,stdout);
55                         } else {
56                                 fputc('\n',stdout);
57                         }
58                 }
59         } else if (bufpos>wrap_margin) {
60                 char *q=outputbuffer,*p=outputbuffer+wrap_margin;
61                 
62                 while (p>outputbuffer&&*p!=' '&& *p!='\t') p--;
63                 if (p==outputbuffer) {
64                         /*worst case - nowhere to wrap. Will use brute force */
65                         int i = wrap_margin;
66                         if (target_charset == NULL) {
67                                 /* NULL target_charest means UTF-8 */
68                                 /* go back to start of nearest utf-8 character */
69                                 while(i>0 && (outputbuffer[i] & 0xC0) == 0x80) i--;
70                         }                       
71                         fwrite(outputbuffer,i,1,stdout);
72
73                         fputc('\n',stdout);
74                         p=outputbuffer+i;
75                 } else {
76                         *p=0;p++;
77                         fputs(outputbuffer,stdout);
78                         fputc('\n',stdout);
79                 }
80                 for(q=outputbuffer;*p;p++,q++) *q=*p;
81                 bufpos=q-outputbuffer;
82                 *q=0;
83         }
84 }
85
86 /************************************************************************/
87 /* Main output function.
88  * Programs which read word-processor files should accumulate paragraphs
89  * in the buffer as array of unicode 16-bit codes and pass to this
90  * function
91  ************************************************************************/ 
92 void output_paragraph(unsigned short int *buffer) {
93         unsigned short int *p;
94         int countout=0;
95         for (p=buffer;*p;p++) {
96                 out_char(convert_char(*p));
97                 countout++;
98         }
99 }