summaryrefslogtreecommitdiff
path: root/support/dktools/ex-sto.ctr
blob: 5290392b4e1336be0c6f76d9c203cbe0fa962d9e (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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
%%	options

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



%%	module


/**	@file
	Test program for storage containers.

The ex-sto program is a test and demonstration program for
storage containers.

The program stores information about persons:
- surname,
- family name and
- age.

The program reads input line by line, each input line is a command.
The following commands can be used:
- add _surname_ _family-name_ _age_
- del f _family-name_
- del s _surname_
- del a _age_
- read _filename_
- print
- tree
- find f _family-name_
- find s _surname_
- find a _age_
- quit

The add command can be used to add persons.

The del command deletes one or multiple records matching the specified
family name, surname or age.

The read command reads an input file typically containing add commands.

The find command searches for the record for a given family name,
surname or age.

The print command prints all records beginning at the current record
(the first record in the container or the element found in the last
find command) until the end of the container.

The tree command command prints the tree structures of a container.

The quit command can be used to exit the application.

Input to the program might look like
@code
add	Erwin	Unsinn		30
add	David	Zeppelin	40
add	Johann	Unfug		50
add	Alter	Sack		99
add	Bla	Blubb		33
print
find	a	40
find	a	45
del	s	Johann
print
tree
quit
@endcode

@see ex-sto.c

@example ex-sto.c
*/

#include "dk4conf.h"
#include "dk4types.h"
#include "dk4mem.h"
#include "dk4sto.h"
#include "dk4str8.h"

#include <string.h>

$(trace-include)



/**	Description of a person.
*/
typedef struct {
  char *famname;	/**< Family name */
  char *surname;	/**< Surname */
  unsigned long age;	/**< Age in years */
} Person;

/** Storage, persons sorted by family name. */
static dk4_sto_t *s1;

/** Storage, persons sorted by surname. */
static dk4_sto_t *s2;

/** Storage, persons sorted by age. */
static dk4_sto_t *s3;

/** Storage, persons unsorted. */
static dk4_sto_t *s4;

/** Iterator for storage sorted by family name. */
static dk4_sto_it_t	*i1;

/** Iterator for storage sorted by surname. */
static dk4_sto_it_t	*i2;

/** Iterator for storage sorted by age. */
static dk4_sto_it_t	*i3;

/** Iterator for unsorted storage. */
static dk4_sto_it_t	*i4;

/** Flag, input processing can be continued. */
static int can_continue;

/** Flag, stdin is connected to terminal. */
static int stdin_is_tty = 1;

static void read_file(FILE *);



/**	Retrieve age from pointer.
	The function retrieves an age evaluation of a pointer.
	The pointer target is either a struct Person or an
	unsigned long.
	@param	p	Pointer to person or unsigned long.
	@param	cr	Pointer object type:
	- 1: \a p points to a struct Person,
	- anything else: \a p points to an unsigned long.
	@return	The age.
*/
unsigned long age_fct(void const *p, int cr)
{
  unsigned long back;
  switch(cr) {
    case 1: {
      back = *((unsigned long const *)p);
    } break;
    default: {
      back = ((Person const *)p)->age;
    } break;
  }
  return back;
}



/**	Compare object against other object or text.
	@param	p1	Left pointer (from storage).
	@param	p2	Right pointer (from dk4sto_it_find_like()).
	@param	cr	Comparison criteria:
	- 0 indicates: p1 and p2 are pointers to struct Person,
	  compare by family name.
	- 1 indicates: p1 is a pointer to a struct Person, p2 is a
	  pointer to a string. Compare the family name of p1
	  against the string.
	- 2 indicates: p1 and p2 are pointers to struct Person,
	  compare by surname.
	- 3 indicates: p1 is a pointer to a struct person, p2 is a
	  pointer to a string. Compare the surname of p1 against
	  the string.
	@return	Comparison result:
	- Positive value, if left pointer is larger (or behind),
	- Negative value, if left pointer is smaller (or before),
	- 0 if both pointers evaluate equal.
*/
int compare_fct(void const *p1, void const *p2, int cr)
{
  int back = 0;
  switch(cr) {
    case 0: {
      back = strcmp(((Person *)p1)->famname, ((Person *)p2)->famname);
    } break;
    case 1: {
      back = strcmp(((Person *)p1)->famname, (char *)p2);
    } break;
    case 2: {
      back = strcmp(((Person *)p1)->surname, ((Person *)p2)->surname);
    } break;
    case 3: {
      back = strcmp(((Person *)p1)->surname, (char *)p2);
    } break;
  }
  return back;
}



/**	Print a prompt.
*/
static void print_prompt(void)
{
  if(stdin_is_tty) {
    printf("\nstotest: ");
  }
}



/**	Print one persons record.
	@param	pers	Person structure.
	@param	fout	Output file.
*/
static void print_person(Person *pers, FILE *fout)
{
  fprintf(
    fout,
    "%s %s %lu\n",
    ((pers->surname) ? pers->surname : "-"),
    ((pers->famname) ? pers->famname : "-"),
    pers->age
  );
}



/**	Print persons record from an iterators current
	position until the end of the container.
	@param	i	Storage iterator.
	@param	fout	Output file.
*/
static void print_it(dk4_sto_it_t *i, FILE *fout)
{
  void *p;
  dk4sto_it_reset(i);
  while((p = dk4sto_it_next(i)) != NULL) {
    print_person((Person *)p, fout);
    fprintf(fout, "\n");
  }
}



/**	Print storage tree node including subtree.
	This function is used to show the tree structure.
	In your applications you should not access the internal
	members of a dk4_sto_t or dk4_sto_node_t directly.
	@param	n	Storage node.
	@param	num	Indent depth.
	@param	f	Output file.
	@param	c	Key character for node.
*/
void print_node(dk4_sto_node_t *n, int num, FILE *f, char c)
{
  int i;
  Person *pers;
  if(n) {
    for(i = 0; i < num; i++) {
      fprintf(f, "  ");
    }
    fprintf(f, "%c %5d (%d)", c, num, n->b);
    pers = (Person *)(n->o);
    if(pers) {
      if(pers->surname) fprintf(f, " %s ", pers->surname);
      else fprintf(f, " - ");
      if(pers->famname) fprintf(f, " %s ", pers->famname);
      else fprintf(f, " - ");
      fprintf(f, " %lu", pers->age);
    }
    fprintf(f, " \tparent:");
    if(n->p) {
      if((n->p)->o) {
	pers = (Person *)((n->p)->o);
	if(pers->surname) fprintf(f, " %s ", pers->surname);
	else fprintf(f, " - ");
	if(pers->famname) fprintf(f, "%s ", pers->famname);
	else fprintf(f, "- ");
	fprintf(f, " %lu", pers->age);
      }
    }
    fprintf(f, "\n");
    print_node(n->l, (num+1), f, 'l');
    print_node(n->r, (num+1), f, 'r');
  }
}



/**	Print the tree structure.
	In your applications you should not access the internal
	members of a dk4_sto_t or dk4_sto_node_t directly.
	@param	s	Storage to print.
	@param	f	Output file.
*/
void print_tree(dk4_sto_t *s, FILE *f)
{
  if((s->h) && (s->t)) {
    print_node(s->r, 0, f, '+');
  }
}



/**	Free a persons record (release memory.)
	@param	pers	Pointer to record to release.
*/
static void free_person(Person *pers)
{
  char *cptr;
  if(pers) {
    cptr = pers->famname;
    dk4mem_free(cptr);
    cptr = pers->surname;
    dk4mem_free(cptr);
    pers->famname = pers->surname = NULL;
    dk4mem_free(pers);
  }
}



/**	Handle command without arguments.
	@param	str	Command (first text in input line).
*/
static
void
handle_cmd_1(char *str)
{
  if(strcasecmp(str, "quit") == 0) {
    can_continue = 0;
  }
  if(strcasecmp(str, "print") == 0) {
    fprintf(stdout, "Sorted by family name:\n");
    print_it(i1, stdout);
    fprintf(stdout, "Sorted by surname:\n");
    print_it(i2, stdout);
    fprintf(stdout, "Sorted by age:\n");
    print_it(i3, stdout);
    fprintf(stdout, "Unsorted:\n");
    print_it(i4, stdout);
  }
  if(strcasecmp(str, "tree") == 0) {
    fprintf(stdout, "Sorted by family name:\n");
    print_tree(s1, stdout);
    fprintf(stdout, "Sorted by surname:\n");
    print_tree(s2, stdout);
    fprintf(stdout, "Sorted by age:\n");
    print_tree(s3, stdout);
    fprintf(stdout, "Unsorted:\n");
    print_tree(s4, stdout);
  }
}



/**	Handle command with one argument.
	@param	cmd	Command to handle.
	@param	arg1	Command argument.
*/
static void handle_cmd_2(char *cmd, char *arg1)
{
  if(strcasecmp(cmd, "read") == 0) {
    FILE *in;
    in = fopen(arg1, "r");
    if(in) {
      read_file(in);
      fclose(in);
    }
  }
}



/**	Handle command with 2 arguments.
	@param	cmd	Command to handle.
	@param	arg1	Command argument 1.
	@param	arg2	Command argument 2.
*/
static void handle_cmd_3(char *cmd, char *arg1, char *arg2)
{
  void *p;
  if(strcasecmp(cmd, "find") == 0) {
    if(strcasecmp(arg1, "f") == 0) {
      p = dk4sto_it_find_like(i1, arg2, 1);
      if(p) {
	print_person(p,stdout); printf("\n");
      }
      printf("Followers:\n");
      while((p = (Person *)dk4sto_it_next(i1)) != NULL) {
	print_person(p,stdout); printf("\n");
      }
    }
    if(strcasecmp(arg1, "s") == 0) {
      p = dk4sto_it_find_like(i2, arg2, 3);
      if(p) {
	print_person(p,stdout); printf("\n");
      }
      printf("Followers:\n");
      while((p = (Person *)dk4sto_it_next(i2)) != NULL) {
	print_person(p,stdout); printf("\n");
      }
    }
    if(strcasecmp(arg1, "a") == 0) {
      unsigned long age;
      if(sscanf(arg2, "%lu", &age) == 1) {
	p = dk4sto_it_find_like(i3, (void *)(&age), 1);
	if(p) {
	  print_person(p,stdout); printf("\n");
	}
	printf("Followers:\n");
	while((p = (Person *)dk4sto_it_next(i3)) != NULL) {
	  print_person(p,stdout); printf("\n");
	}
      }
    }
  }
  if(strcasecmp(cmd, "del") == 0) {
    if(strcasecmp(arg1, "f") == 0) {
      while((p = dk4sto_it_find_like(i1, arg2, 1)) != NULL) {
	dk4sto_remove(s1,p,NULL);
	dk4sto_remove(s2,p,NULL);
	dk4sto_remove(s3,p,NULL);
	dk4sto_remove(s4,p,NULL);
	free_person(p);
      }
    }
    if(strcasecmp(arg1, "s") == 0) {
      while((p = dk4sto_it_find_like(i2, arg2, 3)) != NULL) {
	dk4sto_remove(s1,p,NULL);
	dk4sto_remove(s2,p,NULL);
	dk4sto_remove(s3,p,NULL);
	dk4sto_remove(s4,p,NULL);
	free_person(p);
      }
    }
    if(strcasecmp(arg1, "a") == 0) {
      unsigned long age;
      if(sscanf(arg2, "%lu", &age) == 1) {
	while((p = dk4sto_it_find_like(i3, (void *)(&age), 1)) != NULL) {
	  dk4sto_remove(s1,p,NULL);
	  dk4sto_remove(s2,p,NULL);
	  dk4sto_remove(s3,p,NULL);
	  dk4sto_remove(s4,p,NULL);
	  free_person(p);
	}
      }
    }
  }
}



/**	Handle command with 3 arguments.
	@param	cmd	Command to handle.
	@param	arg1	Command argument 1.
	@param	arg2	Command argument 2.
	@param	arg3	Command argument 3.
*/
static void handle_cmd_4(char *cmd, char *arg1, char *arg2, char *arg3)
{
  if(strcasecmp(cmd, "add") == 0) {
    unsigned long age;
    Person *pers;
    if(sscanf(arg3, "%lu", &age) == 1) {
      pers = dk4mem_new(Person,1,NULL);
      if(pers) {
	pers->famname = dk4str8_dup(arg2, NULL);
	pers->surname = dk4str8_dup(arg1, NULL);
	pers->age = age;
	if(!((pers->famname) && (pers->surname))) {
	  free_person(pers);
	} else {
	  if(!dk4sto_add(s4, (void *)pers, NULL)) {
	    free_person(pers);
	  } else {
	    dk4sto_add(s1, (void *)pers, NULL);
	    dk4sto_add(s2, (void *)pers, NULL);
	    dk4sto_add(s3, (void *)pers, NULL);
	  }
	}
      }
    }
  }
}



/**	Read input file.
	@param	in	File to read from.
*/
void read_file(FILE *in)
{

  char line[1024];
  char *lbegin;
  int i;
  char str1[sizeof(line)];
  char str2[sizeof(line)];
  char str3[sizeof(line)];
  char str4[sizeof(line)];
  int old_can_continue;
  old_can_continue = can_continue;
  can_continue = 1;
  while(can_continue) {
    if(in == stdin) print_prompt();
    if(fgets(line, sizeof(line), in)) {
      lbegin = dk4str8_start(line, NULL);
      if(lbegin) {
        dk4str8_normalize(lbegin, NULL);
	printf("# Processing \"%s\"\n", lbegin);
        i = sscanf(lbegin, "%s %s %s %s", str1, str2, str3, str4);
        switch(i) {
          case 1: {
            handle_cmd_1(str1);
          } break;
          case 2: {
            handle_cmd_2(str1, str2);
          } break;
          case 3: {
            handle_cmd_3(str1, str2, str3);
          } break;
          case 4: {
            handle_cmd_4(str1, str2, str3, str4);
          } break;
        }
      }
    } else {
	  can_continue = 0;
    }
  }
  can_continue = old_can_continue;
}



/**	Main program.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	Exit status code.
*/
int main(int arg, char *argv[])
{
  void *p; Person *pers; char *cptr;
  /* Check whether or not we are connected to a terminal */
  /* stdin_is_tty = dksf_echo_test_tty(); */
  stdin_is_tty = 1;
  /* Create storage containers */
  s1 = dk4sto_open(NULL);
  s2 = dk4sto_open(NULL);
  s3 = dk4sto_open(NULL);
  s4 = dk4sto_open(NULL);
  /* Check whether all containers were created successfully */
  if(s1 && s2 && s3 && s4) {
    /* Set comparison and evaluation functions */
    dk4sto_set_comp(s1, compare_fct, 0);
    dk4sto_set_comp(s2, compare_fct, 2);
    dk4sto_set_eval_ul(s3, age_fct, 0);
    /* Create storage container iterators */
    i1 = dk4sto_it_open(s1, NULL);
    i2 = dk4sto_it_open(s2, NULL);
    i3 = dk4sto_it_open(s3, NULL);
    i4 = dk4sto_it_open(s4, NULL);
    /* Check whether all iterators were created successfully */
    if(i1 && i2 && i3 && i4) {
      /* Process input */
      read_file(stdin);
      /* Release all the records */
      dk4sto_it_reset(i4);
      while((p = dk4sto_it_next(i4)) != NULL) {
	free_person((Person *)p);
      }
    }
  }
  /* Release the iterators */
  if(i1) dk4sto_it_close(i1);
  if(i2) dk4sto_it_close(i2);
  if(i3) dk4sto_it_close(i3);
  if(i4) dk4sto_it_close(i4);
  /* Release the storage containers */
  if(s1) dk4sto_close(s1);
  if(s2) dk4sto_close(s2);
  if(s3) dk4sto_close(s3);
  if(s4) dk4sto_close(s4);
  exit(0); return 0;
}