summaryrefslogtreecommitdiff
path: root/support/dktools/dk4stt.ctr
blob: c25271ed272c1f87e854e0f2fa232734d4355359 (plain)
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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd


%%	header

/**	@file
	String table data type.
	Normally there is no need for you to deal with string tables
	manually. String tables are handled by the dk4_app_t related
	functions.

	CRT on Windows: Optional.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4STRM_H_INCLUDED
#include "dk4strm.h"
#endif

#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif

/**	String table.
*/
typedef struct {
  dkChar	 *shrtfn;	/**< Short file name. */
  dkChar	**strings;	/**< Strings read from file. */
  size_t	  nstrings;	/**< Number of strings. */
} dk4_string_table_t;



#ifdef __cplusplus
extern "C" {
#endif

/**	Create string table from file.
	@param	fn	Short file name to store in structure.
	@param	sz	Number of lines required.
	@param	erp	Error report, may be NULL.
	@return	Pointer to new string table structure on success, NULL
	on error.
*/
dk4_string_table_t *
dk4stt_open(const dkChar *fn, size_t sz, dk4_er_t *erp);

/**	Apply a text stream to a string table.
	@param	tptr	String table to configure.
	@param	strm	Text stream, opened for reading.
	@param	pre	Processing encoding.
	@param	erp1	Error report for decoding/encoding, may be NULL.
	@param	erp2	Error report for processing, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk4stt_apply_stream(
  dk4_string_table_t	*tptr,
  dk4_stream_t		*strm,
  int			 pre,
  dk4_er_t		*erp1,
  dk4_er_t		*erp2
);

/**	Destroy a string table, free allocated memory.
	@param	ptr	String table to destroy.
*/
void
dk4stt_close(dk4_string_table_t *ptr);

/**	Compare two string tables by short name.
	@param	l	Left pointer (always string table).
	@param	r	Right pointer (string table or short name).
	@param	cr	Comparison criteria
			(0=string table/string table,
			1=string table/short name).
	@return	Comparison result.
*/
int
dk4stt_compare(const void *l, const void *r, int cr);

#ifdef __cplusplus
}
#endif


%%	module

#include "dk4error.h"
#include "dk4mem.h"
#include "dk4strd.h"
#include "dk4strm.h"
#include "dk4stt.h"
#include "dk4tspdk.h"
#include "dk4enc.h"


$!trace-include



void
dk4stt_close(dk4_string_table_t *ptr)
{
  dkChar		**stptr;
  size_t		  nstrings;
  $? "+ dk4stt_close"
  if (NULL != ptr) {
    if (NULL != ptr->strings) {
      stptr = ptr->strings;
      nstrings = ptr->nstrings;
      while (0 < nstrings--) {
        dk4mem_release(*stptr);
        stptr++;
      }
      dk4mem_release(ptr->strings);
    }
    dk4mem_release(ptr->shrtfn);
    ptr->nstrings = 0;
    dk4mem_free(ptr);
  } $? "- dk4stt_close"
}



dk4_string_table_t *
dk4stt_open(const dkChar *fn, size_t sz, dk4_er_t *erp)
{
  dk4_string_table_t	 *back	= NULL;
  dkChar		**stptr	= NULL;
  int			  ok	= 0;
  $? "+ dk4stt_open \"%!ds\" %u", TR_DKSTR(fn), (unsigned)sz
  if ((NULL != fn) && (0 < sz)) {
    back = dk4mem_new(dk4_string_table_t,1,erp);
    if (NULL != back) {
      back->strings = NULL;
      back->nstrings = 0;
      back->shrtfn = dk4str_dup(fn, erp);
      if (NULL != back->shrtfn) {
        back->nstrings = sz;
        back->strings = dk4mem_new(DK4_PDKCHAR,sz,erp);
	if (NULL != back->strings) {
	  stptr = back->strings;
	  while (0 < sz--) { *(stptr++) = NULL; }
	  ok = 1;
	}
      }
      if (1 > ok) {
        dk4stt_close(back);
	back = NULL;
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } $? "- dk4stt_open PTR=%d", TR_IPTR(back)
  return back;
}



int
dk4stt_compare(const void *l, const void *r, int cr)
{
  int				 back	=	0;
  const dk4_string_table_t	*pl;
  const dk4_string_table_t	*pr;

  if (NULL != l) {
    if (NULL != r) {
      pl = (const dk4_string_table_t *)l;
      switch (cr) {
        case 1: {
	  if (NULL != pl->shrtfn) {
	    back = dk4str_cmp(pl->shrtfn, (const dkChar *)r);
	  } else {
	    back = -1;
	  }
	} break;
	default: {
	  pr = (const dk4_string_table_t *)r;
	  if (NULL != pl->shrtfn) {
	    if (NULL != pr->shrtfn) {
	      back = dk4str_cmp(pl->shrtfn, pr->shrtfn);
	    } else {
	      back = 1;
	    }
	  } else {
	    if (NULL != pr->shrtfn) {
	      back = -1;
	    }
	  }
	} break;
      }
    } else {
      back = 1;
    }
  } else {
    if (NULL != r) {
      back = -1;
    }
  }
  return back;
}



/**	Structure to read a string table from stream.
*/
typedef struct {
  dk4_string_table_t	*dptr;		/**< String table to configure. */
  size_t		 curpos;	/**< Current line number. */
} dk4_string_table_reader_t;



/**	Handler function for text lines.
	@param	obj	Object to modify while processing the character.
	@param	line	Text line to process.
	@param	lineno	Current line number.
	@param	erp	Error report, may be NULL.
	@return	DK4_TSP_RES_OK		if the character was processed
					successfully,
		DK4_TSP_RES_ERROR	if there was an error but we can
					continue,
		DK4_TSP_RES_FATAL	if there was a fatal error so we
					should abort processing.
*/
static
int
dk4stt_line_handler(
  void		*obj,
  dkChar	*line,
  dk4_um_t	 lineno,
  dk4_er_t	*erp
)
{
  dk4_string_table_reader_t	*reader;		/* Reader structure */
#if TRACE_DEBUG
  dkChar			*olcp;			/* Original pointer */
#endif
  dkChar			*linecp;		/* Copy of line */
  int		 		 back = DK4_TSP_RES_ERROR;
  $? "+ dk4stt_line_handler \"%!ds\"", line
  reader = (dk4_string_table_reader_t *)obj;
  if (dkT('#') != line[0]) {	$? ". data line %lu", (unsigned long)(reader->curpos)
    if (reader->curpos < reader->dptr->nstrings) {	$? ". need processing"
      linecp = dk4str_dup(line, NULL);
      if (NULL != linecp) {				$? ". copy allocated"
#if TRACE_DEBUG
	olcp = linecp;
#endif
        /* Save copy of line to string table */
        (reader->dptr->strings)[reader->curpos] = linecp;
	reader->curpos += 1;
	back = DK4_TSP_RES_OK;
	/* Handle backslash escape sequences, remove trailing newline */
	while (dkT('\0') != *linecp) {
	  if (dkT('\\') == *linecp) {
	    if (dkT('r') == linecp[1]) {
	      *linecp = dkT('\r');
	      dk4str_cpy_to_left(&(linecp[1]), &(linecp[2]));
	    } else {
	      if (dkT('n') == linecp[1]) {
	        *linecp = dkT('\n');
		dk4str_cpy_to_left(&(linecp[1]), &(linecp[2]));
	      } else {
	        if (dkT('t') == linecp[1]) {
		  *linecp = dkT('\t');
		  dk4str_cpy_to_left(&(linecp[1]), &(linecp[2]));
		} else {
	          dk4str_cpy_to_left(linecp, &(linecp[1]));
		  if (dkT('\0') == *linecp) { linecp--; }
		}
	      }
	    }
	  } else {
	    if (dkT('\n') == *linecp) {
	      *linecp = dkT('\0');
	      linecp--;
	    } else {
	      if ((dkT('\r') == *linecp) && (dkT('\n') == linecp[1])) {
	        *linecp = dkT('\0');
		linecp--;
	      }
	    }
	  }
	  linecp++;
	}	$? ". processing result: \"%!ds\"", olcp
      } else {						$? "! memory"
        /* Not enough memory. */
        back = DK4_TSP_RES_FATAL;
      }
    } else {						$? ". additional line"
      /* More lines than expected, ignore additional lines. */
      back = DK4_TSP_RES_OK;
    }
  } else {						$? ". comment line"
    /* Comment line, no processing required. */
    back = DK4_TSP_RES_OK;
  } $? "- dk4stt_line_handler %d", back
  return back;
}



int
dk4stt_apply_stream(
  dk4_string_table_t	*tptr,
  dk4_stream_t		*strm,
  int			 pre,
  dk4_er_t		*erp1,
  dk4_er_t		*erp2
)
{
  dkChar			line[1024];	/* Input line buffer */
  char				buf[4096];	/* Buffer for stream input */
  dk4_string_table_reader_t	reader;		/* Reader structure */
  dk4_tspdk_t			tspdk;		/* Text stream processor */
  dk4_er_t			e1;		/* Error report f decoding */
  dk4_er_t			e2;		/* Error report f processing */
  size_t			rdb;		/* Bytes read */
  int				cc;		/* Flag: Can continue */
  int		 		back	= 0;
  $? "+ dk4stt_apply_stream"
  if ((NULL != tptr) && (NULL != strm)) {		$? ". args ok"
    dk4error_init(&e1);
    dk4error_init(&e2);
    reader.dptr		= tptr;
    reader.curpos	= 0;
    back = dk4tspdk_setup_line(
      &tspdk, (void *)(&reader), dk4stt_line_handler,
      line, DK4_SIZEOF(line,dkChar),
      pre, DK4_FILE_ENCODING_UTF8, &e2
    );
    if (0 < back) {					$? ". init ok"
#if VERSION_BEFORE_20150821
      cc = 1;
#endif
      do {
        cc = 0;
	rdb = sizeof(buf);
	if (0 < dk4stream_read(buf, &rdb, strm, NULL)) {
	  if (0 < rdb) {				$? ". bytes found"
	    switch (dk4tspdk_add_bytes(&tspdk, (unsigned char *)buf, rdb)) {
	      case DK4_TSP_RES_OK: {			$? ". success"
	        cc = 1;
	      } break;
	      case DK4_TSP_RES_ERROR: {			$? "! error"
	        cc = 1;
	      } break;
	      default: {				$? "! fatal error"
	        back = 0;
	      } break;
	    }
	  } else {					$? ". no byte found"
	  }
	} else {					$? ". end of input"
	}
      } while (cc);
      if (0 < back) {					$? ". flush final bytes"
	if (DK4_TSP_RES_FATAL == dk4tspdk_finish(&tspdk)) {
	  back = 0;					$? "! flush failed"
	}
	if (0 < back) {					$? ". flush succeeded"
	  if (reader.curpos < (reader.dptr)->nstrings) {
	    back = 0;					$? "! too few lines"
	    $? ". curpos = %lu", (unsigned long)(reader.curpos)
	    $? ". nstrings = %lu", (unsigned long)((reader.dptr)->nstrings)
	  }
	}
      }
    } else {						$? "! init"
    }
  } else {						$? "! args"
    dk4error_set_simple_error_code(erp2, DK4_E_INVALID_ARGUMENTS);
  } $? "- dk4stt_apply_stream %d", back
  return back;
}