File: ting.h

package info (click to toggle)
mcl 1%3A14-137-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 10,704 kB
  • ctags: 5,125
  • sloc: ansic: 53,217; sh: 4,448; perl: 3,967; makefile: 422
file content (488 lines) | stat: -rw-r--r-- 11,965 bytes parent folder | download | duplicates (9)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*   (C) Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005 Stijn van Dongen
 *   (C) Copyright 2006, 2007, 2008, 2009 Stijn van Dongen
 *
 * This file is part of tingea.  You can redistribute and/or modify tingea
 * under the terms of the GNU General Public License; either version 3 of the
 * License or (at your option) any later version.  You should have received a
 * copy of the GPL along with tingea, in the file COPYING.
*/

#ifndef tingea_ting
#define tingea_ting

#include "types.h"

typedef struct
{  char     *str
;  dim      len
;  dim      mxl
;
}  mcxTing  ;


/*  **************************************************************************
 * *
 **            Implementation notes (a few).
 *
*

  Synopsis
 *    This little module provides an objectified C-string compatible interface
 *    to strings.

 * Some philosophy
 *    Do not mind the overhead of 2*sizeof(int) bytes per ting.
 *    If you have humongous amounts of small strings, use C-strings. Optionally
 *    you can do the needed char manipulation in a ting used as scratch-space
 *    using the ting interface, then create a normal string once you are done.
 *    Use mcxTinguish to avoid copying if needed.

 *    Feel free to use the str member with routines from <string.h>
 *    or perhaps from ding.h. Just remember to treat it as a const object
 *    when doing so.

 * Applications
 *    Zoem is the macro processor that processes the zoem language. It is very
 *    string-heavy and its string operations are entirely based on the ting
 *    module.

 * Caveat
 *    Nearly all routines return a NULL pointer to indicate malloc failure.

 * Data structure
 *    str:  array of chars
 *
 *    len:  length of string in str (excluding '\0')
 *          *(str+len) == '\0'
 *
 *    mxl:  current allocated number of writable char's (excluding '\0')
 *          Allocated amount is mxl+1
 *          *(str+mxl) == '\0' - but don't count on that.

 * Notes
 *    mcxTingEnsure is the only routine allowed to fiddle with mxl.
 *    (apart from mcxTingInit which sets it to zero).
 *
 *    Future idea
 *       mcxTingFinalize,  (realloc to length)
 * 
 *    Routines marked `!' will NOT accept ting==NULL argument.
 *    Routines marked `#' should not be called other than by ting routines
 * 
 *                _ #Init  _
 *               /          \
 *          Ensure   <--- #Instantiate
 *         /       \               \
 *   Empty       !Splice        New,Write,Print,PrintAfter
 *   NWrite           |
 *                    |
 *           Append,Insert,Delete,PrintSplice

 * TODO
 *    the library should be able to deal with embedded \0's. Really.
*/



/*  **************************************************************************
 * *
 **   Various instantiation routines.
*/

                          /*     Accepts NULL argument.
                           *     void arguments so that it can be used
                           *     as callback.
                           *
                           *     Should ordinarily *not* be used, unless
                           *     you want to initialize a ting allocated
                           *     on the frame stack.
                          */
void* mcxTingInit
(  void* ting
)  ;  
                          /*     Accepts NULL argument.
                           *     Should ordinarily *not* be used.
                          */
mcxTing* mcxTingInstantiate
(  mcxTing*    dst_ting
,  const char* str
)  ;  
                          /*     Accepts NULL argument.  Does not affect
                           *     ting->str.  used for preallocing, e.g. to
                           *     prepare for successive calls of Append.  (see
                           *     also Empty).
                          */
mcxTing* mcxTingEnsure
(  mcxTing*    ting
,  dim         length
)  ;  
                          /*     Accepts NULL argument. The string part is
                           *     set to the empty string.
                           *     Can be used for preallocing.
                          */
mcxTing* mcxTingEmpty
(  mcxTing*    ting
,  dim         length
)  ;



/*  **************************************************************************
 * *
 **   Some freeing routines.
*/
                          /*     Free members and shell struct
                           *     You pass the address of a variable,
                           *     loosely speaking.
                          */
void mcxTingFree
(  mcxTing     **tingpp
)  ;
                          /*     Free members and shell struct
                           *     Use with callbacks, e.g.
                           *     for freeing hash with tings in one go.
                          */
void mcxTingFree_v
(  void        *tingpp
)  ;
                          /*     Free members
                           *     Use for freeing array of ting;
                           *     e.g. as callback in mcxNFree.
                          */
void mcxTingRelease
(  void        *ting
)  ;



/*  **************************************************************************
 * *
 **   A bunch of user-land creation routines.
*/

                          /*     accepts NULL argument, maps to empty string.
                          */
mcxTing* mcxTingNew
(  const char* str
)  ;
                          /*     accepts NULL argument, maps to empty string.
                          */
mcxTing* mcxTingNNew
(  const char* str
,  dim         n
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingWrite
(  mcxTing*    ting
,  const char* str
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingNWrite
(  mcxTing*    ting
,  const char* str
,  dim         n
)  ;


                          /*    usurps argument.
                          */
mcxTing* mcxTingify
(  char* str
)  ;


                          /*    destroys argument, returns str member.
                          */
char* mcxTinguish
(  mcxTing*    ting
)  ;


/*  **************************************************************************
 * *
 **   Appending, inserting, deleting, shrinking.
 *    Some can be used as creation routine (e.g. append).
*/


#define TING_INS_CENTER      -3
#define TING_INS_OVERRUN     -4
#define TING_INS_OVERWRITE   -5

/*
 *    The offset argument can be negative, for subscripting from the end.
 *
 *    The n_delete argument can be nonnegative, or one of
 *    TING_INS_CENTER     center, (try to) overwrite without changing length.
 *    TING_INS_OVERRUN    overwrite till the end.
 *    TING_INS_OVERWRITE  (try to) overwrite without changing length.
 *
 *    The n_copy argument is not magical like the previous two.
 *
*/
                          /*     does NOT accept NULL argument.
                          */
mcxstatus mcxTingSplice
(  mcxTing*    ting
,  const char* pstr
,  ofs         d_offset   /*     negative offset refers to end     */
,  ofs         n_delete   /*     special modes as documented above */
,  dim         n_copy
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingInsert
(  mcxTing*    ting
,  const char* str
,  ofs         offset
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingNInsert
(  mcxTing*    ting
,  const char* str
,  ofs         offset     /*  of ting->str */
,  dim         length     /*  of str       */
)  ;

                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingAppend
(  mcxTing*    ting
,  const char* str
)  ;

                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingKAppend
(  mcxTing*    ting
,  const char* str
,  dim         n
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingNAppend
(  mcxTing*    ting
,  const char* str
,  dim         n
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingDelete
(  mcxTing*    ting
,  ofs         offset
,  dim         width
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing* mcxTingShrink
(  mcxTing*    ting
,  ofs         length
)  ;



/* Fails only for memory reason */

mcxstatus mcxTingTackc
(  mcxTing*  ting
,  unsigned char c
)  ;


/* Fails if last char is not the same as c */

mcxstatus mcxTingTickc
(  mcxTing*  ting
,  unsigned char c
)  ;


/*  **************************************************************************
 * *
 **   A bunch of printf like routines.
*/

                          /*     Accepts NULL argument.
                          */
mcxTing* mcxTingPrint
(  mcxTing*    ting
,  const char* fmt
,  ...
)
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif
   ;
                          /*     Accepts NULL argument.
                          */
mcxTing*  mcxTingPrintAfter
(  mcxTing*    dst
,  const char* fmt
,  ...
)
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif
   ;
                          /*     Accepts NULL argument.
                           *     same offset and delete interface as
                           *     mcxTingSplice.
                          */
mcxTing*  mcxTingPrintSplice
(  mcxTing*    dst
,  ofs         offset
,  ofs         n_delete   /* count of chars to delete, special modes */
,  const char* fmt
,  ...
)
#ifdef __GNUC__
__attribute__ ((format (printf, 4, 5)))
#endif
   ;


/*  **************************************************************************
 * *
 **   Miscellaneous.
*/

char*  mcxTingStr
(  const mcxTing* ting
)  ;


char* mcxTingSubStr
(  const mcxTing* ting
,  ofs            offset
,  ofs            length  /*     use -1 to indicate remainder of string */
)  ;

                          /*     accepts NULL argument.
                          */
mcxTing*  mcxTingRoman
(  mcxTing*    dst
,  long        x
,  mcxbool     ucase
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing*  mcxTingInteger
(  mcxTing*    dst
,  long        x
)  ;
                          /*     accepts NULL argument.
                          */
mcxTing*  mcxTingDouble
(  mcxTing* dst
,  double   x
,  int      decimals
)  ;


/*  **************************************************************************
 * *
 **   Comparing.
*/

                  /* compare two mcxTing* pointers */
int mcxTingCmp
(  const void* t1
,  const void* t2
)  ;

                  /* compare two mcxTing* pointers */
int mcxTingRevCmp
(  const void* t1
,  const void* t2
)  ;

                  /* compare two mcxTing** pointers */
int mcxTingPCmp
(  const void* t1
,  const void* t2
)  ;

                  /* compare two mcxTing** pointers */
int mcxTingPRevCmp
(  const void* t1
,  const void* t2
)  ;

                  /* compare two mcxKV** pointers by key as mcxTing* */
int mcxPKeyTingCmp
(  const void* k1
,  const void* k2
)  ;

                  /* compare two mcxKV** pointers by key as mcxTing* */
int mcxPKeyTingRevCmp
(  const void* k1
,  const void* k2
)  ;


/*  **************************************************************************
 * *
 **   Hashing.
*/

u32 (*mcxTingHFieByName(const char* id))(const void* ting)  ;


u32 mcxTingELFhash
(  const void* ting
)  ;

u32 mcxTingHash
(  const void* ting
)  ;

u32 mcxTingBJhash
(  const void* ting
)  ;

u32 mcxTingCThash
(  const void* ting
)  ;

u32 mcxTingDPhash
(  const void* ting
)  ;

u32 mcxTingBDBhash
(  const void* ting
)  ;

u32 mcxTingGEhash
(  const void* ting
)  ;

u32 mcxTingOAThash
(  const void* ting
)  ;

u32 mcxTingFNVhash
(  const void* ting
)  ;

u32 mcxTingSvDhash
(  const void* ting
)  ;

u32 mcxTingSvD2hash
(  const void* ting
)  ;

u32 mcxTingSvD1hash
(  const void* ting
)  ;

u32 mcxTingDJBhash
(  const void* ting
)  ;

#endif