summaryrefslogtreecommitdiff
path: root/support/dktools/dk4wxstt.cpp
blob: ecca665f24ebf7d27b1ef5bcf75e1ce1768ace36 (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
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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4wxstt.cpt
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk4wxstt.cpp The dk4wxstt module.
*/


#line 146 "dk4wxstt.cpt"



#include "dk4mem.h"
#include "dk4enc.h"
#include "dk4strd.h"
#include "dk4wxtypes.h"
#include "dk4strx.h"
#include "dk4wxstt.h"
#include "dk4tspwx.h"





#line 160 "dk4wxstt.cpt"



void
dk4wxstt_shutdown(dk4_wx_string_table_t *ptr)
{
  wxChar		**stptr;
  size_t		  nstrings;
  

#line 169 "dk4wxstt.cpt"
  if (NULL != ptr) {				

#line 170 "dk4wxstt.cpt"
    if (NULL != ptr->strings) {			

#line 171 "dk4wxstt.cpt"
      stptr = ptr->strings;
      nstrings = ptr->nstrings;
      while (0 < nstrings--) {			

#line 174 "dk4wxstt.cpt"
        dk4mem_release(*stptr);
	stptr++;
      }						

#line 177 "dk4wxstt.cpt"
      dk4mem_release(ptr->strings);
    } 
  }
  

#line 181 "dk4wxstt.cpt"
}


void
dk4wxstt_close(dk4_wx_string_table_t *ptr)
{
  

#line 188 "dk4wxstt.cpt"
  if (NULL != ptr) {				

#line 189 "dk4wxstt.cpt"
    dk4wxstt_shutdown(ptr);			

#line 190 "dk4wxstt.cpt"
    dk4mem_release(ptr->shrtfn);
    ptr->nstrings = 0;				

#line 192 "dk4wxstt.cpt"
    dk4mem_free(ptr);
  }
  

#line 195 "dk4wxstt.cpt"
}



dk4_wx_string_table_t *
dk4wxstt_open(const dkChar *fn, size_t sz, dk4_er_t *erp)
{
  dk4_wx_string_table_t		 *back	= NULL;
  wxChar			**stptr	= NULL;
  int				  ok	= 0;
  

#line 206 "dk4wxstt.cpt"
  if ((NULL != fn) && (0 < sz)) {			

#line 207 "dk4wxstt.cpt"
    back = dk4mem_new(dk4_wx_string_table_t,1,erp);
    if (NULL != back) {					

#line 209 "dk4wxstt.cpt"
      back->strings = NULL;
      back->nstrings = 0;
      back->shrtfn = dk4str_dup(fn, erp);
      if (NULL != back->shrtfn) {			

#line 213 "dk4wxstt.cpt"
        back->nstrings = sz;
	back->strings = dk4mem_new(DK4_PWXCHAR,sz,erp);
	if (NULL != back->strings) {			

#line 216 "dk4wxstt.cpt"
	  stptr = back->strings;
	  while (0 < sz--) { *(stptr++) = NULL; }
	  ok = 1;
	}
      }
      if (1 > ok) {					

#line 222 "dk4wxstt.cpt"
        dk4wxstt_close(back);
	back = NULL;
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  

#line 230 "dk4wxstt.cpt"
  return back;
}



int
dk4wxstt_compare(const void *l, const void *r, int cr)
{
  const dk4_wx_string_table_t	*lptr;
  const dk4_wx_string_table_t	*rptr;
  int				 back = 0;
  if (NULL != l) {
    if (NULL != r) {
      lptr = (const dk4_wx_string_table_t *)l;
      switch (cr) {
        case 1: {
	  if (NULL != lptr->shrtfn) {
	    back = dk4str_pathcmp(lptr->shrtfn, (const dkChar *)r);
	  } else {
	    back = -1;
	  }
	} break;
	default : {
	  rptr = (const dk4_wx_string_table_t *)r;
	  if (NULL != lptr->shrtfn) {
	    if (NULL != rptr->shrtfn) {
	      back = dk4str_pathcmp(lptr->shrtfn, rptr->shrtfn);
	    } else { back = 1; }
	  } else {
	    if (NULL != rptr->shrtfn) { back = -1; }
	  }
	} break;
      }
    } else { back = 1; }
  } else {
    if (NULL != r) { back = -1; }
  }
  return back;
}



/**	Support structure for reading string table from stream.
*/
typedef struct {
  dk4_wx_string_table_t	*dptr;		/**< String table to set up. */
  size_t		 curpos;	/**< Current string number. */
} dk4_wx_string_table_reader_t;



#if DK4_SIZEOF_WXCHAR > 1
#if DK4_SIZEOF_WXCHAR > 2
#define	TO_SIZED_CHAR(x)	((dk4_c32_t)(x))
#else
#define	TO_SIZED_CHAR(x)	((dk4_c16_t)(x))
#endif
#else
#define	TO_SIZED_CHAR(x)	((char)(x))
#endif

/**	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
dk4wxstt_line_handler(
  void		*obj,
#if DK4_SIZEOF_WXCHAR > 1
#if DK4_SIZEOF_WXCHAR > 2
  dk4_c32_t	*line,
#else
  dk4_c16_t	*line,
#endif
#else
  char		*line,
#endif
  dk4_um_t	 lineno,
  dk4_er_t	*erp
)
{
  dk4_wx_string_table_reader_t		*reader;
  wxChar				*linecp;
  int					 back	= DK4_TSP_RES_ERROR;
  

#line 324 "dk4wxstt.cpt"
  reader = (dk4_wx_string_table_reader_t *)obj;
  if (wxT('#') != (wxChar)(*line)) {	

#line 326 "dk4wxstt.cpt"
    if (reader->curpos < reader->dptr->nstrings) {	

#line 327 "dk4wxstt.cpt"
      linecp = dk4strx_dup((wxChar *)line, NULL);
      if (NULL != linecp) {
        (reader->dptr->strings)[reader->curpos] = linecp;
	reader->curpos += 1;
	back = DK4_TSP_RES_OK;
	while (wxT('\0') != (wxChar)(*linecp)) {
	  if (wxT('\\') == (wxChar)(*linecp)) {
	    if (wxT('r') == (wxChar)(linecp[1])) {
	      *linecp = TO_SIZED_CHAR(wxT('\r'));
	      dk4strx_cpy_to_left(
	        (wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
	      );
	    } else {
	      if (wxT('n') == wxChar(linecp[1])) {
	        *linecp = TO_SIZED_CHAR(wxT('\n'));
		dk4strx_cpy_to_left(
		  (wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
		);
	      } else {
	        if (wxT('t') == (wxChar)(linecp[1])) {
		  *linecp = TO_SIZED_CHAR(wxT('\t'));
		  dk4strx_cpy_to_left(
		    (wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
		  );
		} else {
		  dk4strx_cpy_to_left(
		    (wxChar *)(linecp), (wxChar *)(&(linecp[1]))
		  );
		  if (wxT('\0') == (wxChar)(*linecp)) { linecp--; }
		}
	      }
	    }
	  } else {
	    if (wxT('\n') == (wxChar)(*linecp)) {
	      *linecp = TO_SIZED_CHAR(wxT('\0'));
	      linecp--;
	    } else {
	      if ((wxT('\r') == (wxChar)(*linecp))
	          && (wxT('\n') == (wxChar)(linecp[1])))
	      {
	        *linecp = TO_SIZED_CHAR(wxT('\0'));
		linecp--;
	      }
	    }
	  }
	  linecp++;
	}
      } else {
        back = DK4_TSP_RES_FATAL;
      }
    } else {					

#line 378 "dk4wxstt.cpt"
      back = DK4_TSP_RES_OK;
    }
  } else {
    back = DK4_TSP_RES_OK;
  } 

#line 383 "dk4wxstt.cpt"
  return back;
}



bool
dk4wxstt_apply_stream(
  dk4_wx_string_table_t	*tptr,
  dk4_stream_t		*strm,
  int			 pre,
  dk4_er_t		*erp1,
  dk4_er_t		*erp2
)
{
  wxChar			 line[1024];	/* Line buffer */
  char				 buf[4096];	/* Input bytes from stream */
  dk4_wx_string_table_reader_t	 reader;	/* Reader structure */
  dk4_tspwx_t			 tspwx;		/* Text stream processor */
  dk4_er_t			 e1;		/* Error report decoding */
  dk4_er_t			 e2;		/* Error report processing */
  size_t			 rdb;		/* Number of bytes read */
  int				 cc;		/* Flag: Can continue */
  bool				 back = false;	/* Function result */
  

#line 407 "dk4wxstt.cpt"
  if ((NULL != tptr) && (NULL != strm)) {		

#line 408 "dk4wxstt.cpt"
    dk4error_init(&e1);
    dk4error_init(&e2);
    reader.dptr   = tptr;
    reader.curpos = 0;
    cc = dk4tspwx_setup_line(
      &tspwx, (void *)(&reader), dk4wxstt_line_handler,
      line, DK4_SIZEOF(line,wxChar),
      pre, DK4_FILE_ENCODING_UTF8, &e2
    ); back = ((cc != 0) ? true : false);
    if (back) {						

#line 418 "dk4wxstt.cpt"
#if VERSION_BEFORE_20150821
      cc = 1;
#endif
      do {
        cc = 0;
	rdb = sizeof(buf);
	if (0 < dk4stream_read(buf, &rdb, strm, NULL)) {
	  if (0 < rdb) {				

#line 426 "dk4wxstt.cpt"
	    switch (dk4tspwx_add_bytes(&tspwx, (unsigned char *)buf, rdb)) {
	      case DK4_TSP_RES_OK: {
	        cc = 1;					

#line 429 "dk4wxstt.cpt"
	      } break;
	      case DK4_TSP_RES_ERROR: {
	        cc = 1;					

#line 432 "dk4wxstt.cpt"
	      } break;
	      default: {
	        back = false;				

#line 435 "dk4wxstt.cpt"
	      } break;
	    }
	  }
	}
      } while (0 != cc);
      if (back) {					

#line 441 "dk4wxstt.cpt"
        if (DK4_TSP_RES_FATAL == dk4tspwx_finish(&tspwx)) {
	  back = false;					

#line 443 "dk4wxstt.cpt"
	}
	if (back) {
	  if (reader.curpos < (reader.dptr)->nstrings) {
	    back = false;				

#line 447 "dk4wxstt.cpt"
	  }
	}
      }
    } else {						

#line 451 "dk4wxstt.cpt"
    }
  } else {
    dk4error_set_simple_error_code(erp2, DK4_E_INVALID_ARGUMENTS);
  }
  

#line 456 "dk4wxstt.cpt"
  return back;
}