File: text.c

package info (click to toggle)
vilistextum 2.6.9-1.3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 940 kB
  • sloc: sh: 3,814; ansic: 2,618; makefile: 59
file content (455 lines) | stat: -rw-r--r-- 10,477 bytes parent folder | download
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
/*
 * Copyright (c) 1998-2006 Patric Mller
 * bhaak@gmx.net
 * http://bhaak.dyndns.org/vilistextum/
 *
 * Released under the GNU GPL Version 2 - http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "multibyte.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "text.h"
#include "main.h"
#include "html.h"
#include "debug.h"
#include "fileio.h"
#include "util.h"

int LEFT = 1,
  CENTER = 2,
  RIGHT  = 3;

int breite=76,
	hr_breite=76,
	paragraph=0,

	tab=4,         /* tabulator */
	spaces=0,      /* spaces at beginning of line */
	nooutput=0,    /* for SCRIPT, etc: no output */ 
	orderedlist=0, /* OL */
	
	div_test=0;

CHAR wort[DEF_STR_LEN];

CHAR zeile[DEF_STR_LEN];
int  zeilen_len=0,       /* apparent length of the line  */
  zeilen_len_old=0,
  zeilen_pos=0,       /* true length of line */
  wort_len=0,         /* apparent length of the word */
  wort_pos=0,         /* true length of word */
  anz_leere_zeilen=0, /* how many line were blank */
  noleadingblanks=0;  /* remove blanks lines at the start of the output */

CHAR ch;

/* ------------------------------------------------ */

void center_zeile()
{
  int i,j;
#ifdef proc_debug
  printf("\ncenter_zeile()\n");
#endif

  if (!palm)
  {
		/* ensure that the string is not the empty string */
    if (zeilen_len!=0)
    {
			/* ensure that centering is possible */
			if (zeilen_pos<breite) {
				j=(breite-zeilen_len)/2;

#ifdef center_debug
				printf(" center-zeile; zeilen_len: %d; j: %d; breite: %d; zeilen_pos: %d\n",zeilen_len,j,breite,zeilen_pos);
				printf(" %s\n",zeile);
#endif
				for (i=zeilen_pos+j; i>=0; i--)
				{
#ifdef center_debug
					printf(" %c",zeile[i]);
					printf("1: i: %d; j: %d; i+j: %d\n",i,j,i+j);
#endif
					zeile[i+j]=zeile[i];
				}
				for (i=0; i<j; i++) { zeile[i]=' '; }
#ifdef center_debug
				printf("2: i: %d; j: %d; i+j: %d\n",i,j,i+j);
				printf(" \n%s\n\n",zeile);
				printf(" --------------------------------------------------\n");
#endif
			}
		}
#ifdef proc_debug
  printf("center_zeile() ende\n");
#endif
  }
} /* end center_zeile */

/* ------------------------------------------------ */

void right_zeile()
{
  int i,j;
#ifdef proc_debug
  printf("\nright_zeile()\n");
#endif

  if (!palm)
  {
    if (zeilen_len!=0)
    {
      j=breite-zeilen_len;

#ifdef right_debug
      printf(" right-zeile; len: %d; j: %d\n",zeilen_len,j);
      printf(" %s\n",zeile);
#endif
      for (i=zeilen_pos+j+2; i>=0; i--)
      {
#ifdef right_debug
	printf(" %c",zeile[i]);
#endif
	zeile[i+j]=zeile[i];
      }
      for (i=0; i<j; i++) { zeile[i]=' '; }
#ifdef right_debug
      printf(" \n%s\n\n",zeile);
      printf(" --------------------------------------------------\n");
#endif
    }
  }
#ifdef proc_debug
  printf("right_zeile() ende\n");
#endif
} /* end right_zeile */

/* ------------------------------------------------ */

/* return true, if z is all spaces or nonbreakable space */
int only_spaces(CHAR *z)
{
  int len=STRLEN(z);
	int i, ret=1;
	CHAR j;

	for (i=0; i<len; i++) { j=z[i]; ret = (ret && ((j==' ')||(j==160))); }
	return(ret);
} /* end only_spaces */

/* ------------------------------------------------ */

void clear_line() {
	zeile[0]='\0';
	zeilen_len=0; zeilen_pos=0;
}

/* ------------------------------------------------ */

/* print line */
void print_zeile()
{
  int printzeile;
#ifdef proc_debug
  printf("\nprint_zeile()\n");
  printf("align[align_nr]: %d\n", get_align());
#endif
 
	if ((shrink_lines) && only_spaces(zeile)) 
  { 
		clear_line();
		anz_leere_zeilen++;	
  } else {
		anz_leere_zeilen=0;
  }	

  /* Don't allow leading blank lines.
		 That means the first line of the output is never an empty line */
	if (noleadingblanks==0) { noleadingblanks = !only_spaces(zeile); }

  if (shrink_lines==0)
  {
		printzeile = (!((zeilen_len==0)&&(zeilen_len_old==0)));
  } else {
		printzeile = (!((anz_leere_zeilen>shrink_lines)||(noleadingblanks==0)));
  }

  /* fprintf(stderr, "anz_leere_zeilen %d shrink_lines %d zeilen_len %d zeilen_len_old %d noleadingblanks %d nooutput %d printzeile %d zeile %ls \n", anz_leere_zeilen, shrink_lines, zeilen_len, zeilen_len_old, noleadingblanks, nooutput, printzeile, zeile); */

  if (printzeile) 
	{
	  if (get_align()==LEFT)   {}
	  if (get_align()==CENTER) { center_zeile(); }
	  if (get_align()==RIGHT)  { right_zeile(); }

	  if (!nooutput) { output_string(zeile); }
#ifdef debug
	  else { print_error("keine ausgabe von ", zeile); }
#endif
		
	  zeilen_len_old=zeilen_len;
	  clear_line();
  }
#ifdef proc_debug
  printf("print_zeile() ende\n");
#endif
} /* end print_zeile */

/* ------------------------------------------------ */

int is_zeile_empty()
{
	return(zeile[0]=='\0');
} /* end is_zeile_empty */

/* ------------------------------------------------ */

void status()
{
	printf(" paragraph: %d; div_test: %d; align[align_nr]: %d; z_o: %d\n",paragraph, div_test, get_align(), zeilen_len_old);

#ifdef wort_ende_debug
  printf(" zeile: %s\n",zeile);
  printf(" wort: %s\n",wort);
	printf(" nooutput:   %d\n", nooutput);
  printf(" zeilen_len: %d ; wort_len: %d\n",zeilen_len,wort_len);
  printf(" zeilen_pos: %d ; wort_pos: %d\n",zeilen_pos,wort_pos);
#endif
}

/* ------------------------------------------------ */

void zeile_plus_wort(CHAR *s, int wl, int wp)
{
  int i=zeilen_pos,
      j=0;
#ifdef proc_debug
  printf("\nzeile_plus_wort()\n");
#endif

	if (zeilen_pos+wp<DEF_STR_LEN-1) {
		while (i<zeilen_pos+wp) { zeile[i] = s[j]; j++; i++; }
		zeile[i] = '\0';
		zeilen_len += wl; zeilen_pos += wp;
	}
#ifdef proc_debug
  printf("zeile_plus_wort() ende\n");
#endif
} /* end zeile_plus_wort */

/* ------------------------------------------------ */

void wort_plus_string_nocount(CHAR *s)
{
  int len=STRLEN(s),
      i=wort_pos,
      j=0;
#ifdef proc_debug
  printf("\nwort_plus_string_nocount()\n");
#endif

  if (!palm)
  {
		if (wort_pos+len<DEF_STR_LEN-1) {
			while (i<wort_pos+len) { wort[i] = s[j]; j++; i++; }
			wort[i] = '\0';
			wort_pos += len;
		}
  }

#ifdef proc_debug
  printf("wort_plus_string_nocount() ende\n");
#endif
} /* end wort_plus_string_nocount */

/* ------------------------------------------------ */

void wort_plus_string(CHAR *s)
{
  int len=STRLEN(s),
		  i=wort_pos,
		  j=0;
#ifdef wort_ende_debug
  printf("\nwort_plus_string()\n");
#endif
#ifdef wort_ende_debug
  printf("s: %ls len: %d\n", s,len);
#endif

	if (wort_pos+len<DEF_STR_LEN-1) {
		while (i<wort_pos+len) { wort[i] = s[j]; j++; i++; }
		wort[i] = '\0';
		wort_pos += len; wort_len += len;
	}

#ifdef proc_debug
  printf("wort_plus_string() ende\n");
#endif
} /* end wort_plus_string */

/* ------------------------------------------------ */

void wort_plus_ch(int c)
{

#ifdef default_debug
  printf("==wort_plus_ch(%d)\n", c);
#endif
	if (wort_pos<DEF_STR_LEN-1) {
		wort[wort_pos++] = c;
		wort_len++;
	}
} /* end wort_plus */

/* ------------------------------------------------ */

void wort_ende()
{
  int i=0;
#ifdef proc_debug
    printf("wort_ende()\n");
#endif
  if (wort_len > 0)
  {
    wort[wort_pos] = '\0';

    if (zeilen_len+wort_len+1 > breite)
    {
#ifdef wort_ende_debug
      printf(" z0: zeilen_len: %d\n",zeilen_len);
      printf(" z0: zeilen_pos: %d\n",zeilen_pos);
#endif
      print_zeile();
      i=0;
      while (i<spaces) { zeile_plus_wort(ONESPACE,1,1); i++; }
			if (orderedlist>0) { zeile_plus_wort(ONESPACE,1,1); }
      zeile_plus_wort(wort, wort_len, wort_pos);
    }
    else if (zeilen_len != 0)
    {
#ifdef wort_ende_debug
      printf(" z1: zeilen_len: %d ; wort_len %d \n",zeilen_len, wort_len);
      printf(" z1: zeilen_pos: %d ; wort_pos %d\n",zeilen_pos, wort_pos);
#endif
			/* add space + word */
      zeile_plus_wort(ONESPACE,1,1); zeile_plus_wort(wort,wort_len, wort_pos);
    }
    else /* zeilen_len==0 => new beginning of a paragraph */
    {
#ifdef wort_ende_debug
      printf(" z2: zeilen_len: %d\n",zeilen_len);
      printf(" z2: zeilen_pos: %d\n",zeilen_pos);
#endif
      i=0;
      while (i<spaces) { zeile_plus_wort(ONESPACE,1,1); i++; }
			if (orderedlist>0) { zeile_plus_wort(ONESPACE,1,1); }
      zeile_plus_wort(wort,wort_len, wort_pos);
    }
    wort_pos = 0;
    wort_len = 0;
  }

#ifdef proc_debug
  printf("wort_ende() ende\n");
#endif
} /* end wort_ende */

/* ------------------------------------------------ */

void line_break()
{
  wort_ende();
  print_zeile();
} /* end line_break */

/* ------------------------------------------------ */

void paragraphen_ende()
{
  if (paragraph!=0)
  {
#ifdef proc_debug
    printf("paragraph_ende()\n");
    printf("zeilen_len: %d\n", zeilen_len);
#endif
    line_break();
    print_zeile();
    paragraph--;

    pop_align();
  }
#ifdef proc_debug
  printf("paragraphen_ende() ende\n");
#endif
} /* end paragraphen_ende */

/* ------------------------------------------------ */

void neuer_paragraph()
{
#ifdef proc_debug
  printf("neuer_paragraph()\n");
#endif
  /*fprintf(stderr, "paragraph %d\n", paragraph); */
  if (paragraph!=0) { paragraphen_ende(); }
  line_break();
  print_zeile();
  paragraph++;
#ifdef proc_debug
  printf("neuer_paragraph() ende\n");
#endif
} /* end neuer_paragraph */

/* ------------------------------------------------ */

void hr()
{
  int i, hr_width=hr_breite-4, hr_align=CENTER;
#ifdef proc_debug
  printf("hr()\n");
#endif
  while (ch!='>')
  {
    ch=get_attr();
#ifdef attr_debug
    printf(" HR-attribute: %s; content: %s#\n",attr_name,attr_ctnt);
#endif
    if CMP("ALIGN", attr_name)
    {
      uppercase_str(attr_ctnt);
      if CMP("LEFT", attr_ctnt) { hr_align=LEFT;   }
      else if CMP("CENTER",  attr_ctnt) { hr_align=CENTER; }
      else if CMP("RIGHT",   attr_ctnt) { hr_align=RIGHT;  }
      else if CMP("JUSTIFY", attr_ctnt) { hr_align=LEFT;  }			
      else { if (errorlevel>=2) { fprintf(stderr, "No LEFT|CENTER|RIGHT found!\n");} }
    }
    else if CMP("WIDTH", attr_name)
    {
      i=STRLEN(attr_ctnt);
      if (attr_ctnt[i-1]=='%') {
        attr_ctnt[i-1] = '\0';
        hr_width = ATOI(attr_ctnt);
        if (hr_width==100) { hr_width = hr_breite-4; }
        else { hr_width = hr_breite*hr_width/100; }
      } else {
        hr_width = ATOI(attr_ctnt)/8;
        if (hr_width>hr_breite-4) { hr_width = hr_breite-4; }
      }
    }
  }

  neuer_paragraph();
  push_align(hr_align);
  for (i=0; i<hr_width; i++) { wort_plus_ch('-'); }
  paragraphen_ende();
#ifdef proc_debug
  printf("hr() ende\n");
#endif
} /* end hr */