]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/writer.c
Fixed some warnings produced by GCC 4.9.2
[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 void out_char(const char *chunk) {
25         static int bufpos=0;
26         int eol_flag=0;
27         const char *p; char *q;
28         if (!wrap_margin) {
29                 fputs(chunk,stdout);
30                 return;
31         }
32
33         for (q=outputbuffer+bufpos,p=chunk;
34                         *p;
35                         *(q++)=*(p++),bufpos++) {
36                         if (*p=='\n') eol_flag=1;
37         }               
38         *q=0;
39                  /* This strcat is safe. wrap margin setting
40                                                          code in main.c ensures that wrap_margin is 
41                                                          less than LINE_BUF_SIZE-strlen(largest chunk)
42                                                          */  
43         if (eol_flag) {
44                 /* End of paragraph */
45                 char *q = map_subst(spec_chars,'\n');
46                 fputs(outputbuffer,stdout);
47                 *outputbuffer=0;
48                 bufpos=0;
49                 if (q) {
50                          fputs(q,stdout);
51                 } else {
52                         fputc('\n',stdout);
53                 }       
54         } else if (bufpos>wrap_margin) {
55                 char *q=outputbuffer,*p=outputbuffer+wrap_margin;
56                 
57                 while (p>outputbuffer&&*p!=' '&& *p!='\t') p--;
58                 if (p==outputbuffer) {
59                         /*worst case - nowhere to wrap. Will use brute force */
60                         int i = wrap_margin;
61                         if (target_charset == NULL) {
62                                 /* NULL target_charest means UTF-8 */
63                                 /* go back to start of nearest utf-8 character */
64                                 while(i>0 && (outputbuffer[i] & 0xC0) == 0x80) i--;
65                         }                       
66                         fwrite(outputbuffer,i,1,stdout);
67
68                         fputc('\n',stdout);
69                         p=outputbuffer+i;
70                 } else {
71                         *p=0;p++;
72                         fputs(outputbuffer,stdout);
73                         fputc('\n',stdout);
74                 }
75                 for(q=outputbuffer;*p;p++,q++) *q=*p;
76                 bufpos=q-outputbuffer;
77                 *q=0;
78         }
79 }
80
81 /************************************************************************/
82 /* Main output function.
83  * Programs which read word-processor files should accumulate paragraphs
84  * in the buffer as array of unicode 16-bit codes and pass to this
85  * function
86  ************************************************************************/ 
87 void output_paragraph(unsigned short int *buffer) {
88         unsigned short int *p;
89         int countout=0;
90         for (p=buffer;*p;p++) {
91                 out_char(convert_char(*p));
92                 countout++;
93         }
94 }