summaryrefslogtreecommitdiff
path: root/btparse/tests/name_test.c
blob: 28d0546d610b07b8c9ad1c2eef2bd570d2ffd15a (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
/*
 * name_test.c
 *
 * GPW 1997/11/03
 *
 * $Id$
 */

#include <stdlib.h>
#include <stdio.h>
#include "btparse.h"


static void
print_tokens (char *partname, char **tokens, int num_tokens)
{
   int  i;

   if (tokens)
   {
      printf ("%s tokens = (", partname);
      for (i = 0; i < num_tokens; i++)
      {
         printf ("%s%c", tokens[i], i == num_tokens-1 ? ')' : '|');
      }
      putchar ('\n');
   }
}


static void
dump_name (bt_name * name)
{
   printf ("total number of tokens = %d\n", name->tokens->num_items);
   print_tokens ("first", name->parts[BTN_FIRST], name->part_len[BTN_FIRST]);
   print_tokens ("von", name->parts[BTN_VON], name->part_len[BTN_VON]);
   print_tokens ("last", name->parts[BTN_LAST], name->part_len[BTN_LAST]);
   print_tokens ("jr", name->parts[BTN_JR], name->part_len[BTN_JR]);
}


static void
show_formatted_name (char * msg, bt_name_format * format, bt_name * name)
{
   char * fname;

   fname = bt_format_name (name, format);
   printf ("%s = (%s)\n", msg, fname);
   free (fname);
}


static void
process_name (char * name_string, int line_num, int name_num)
{
   bt_name * name;
   bt_name_format * format;

   printf ("original name = %s\n", name_string);
   name = bt_split_name (name_string, "stdin", line_num, name_num);
   if (! (name && name->tokens))
   {
      fprintf (stderr, "empty name\n");
      return;
   }

   dump_name (name);

   /* First "vljf", unabbreviated first name. */
   format = bt_create_name_format ("vljf", FALSE);
   show_formatted_name ("fname 1", format, name);

   /* Now abbreviate first name stupidly (ie. with no post-token text) */
   bt_set_format_options (format, BTN_FIRST, TRUE, BTJ_MAYTIE, BTJ_SPACE);
   show_formatted_name ("fname 2", format, name);

   /* Add those missing post-token periods */
   bt_set_format_text (format, BTN_FIRST, NULL, NULL, NULL, ".");
   show_formatted_name ("fname 3", format, name);

   /* Drop the periods and force no space between first-name tokens */
   bt_set_format_text (format, BTN_FIRST, NULL, NULL, NULL, "");
   bt_set_format_options (format, BTN_FIRST, TRUE, BTJ_NOTHING, BTJ_SPACE);
   show_formatted_name ("fname 4", format, name);

   /* Finish with this format, and create a new one: "fvlj", abbreviated. */
   bt_free_name_format (format);
   format = bt_create_name_format ("fvlj", TRUE);
   show_formatted_name ("fname 5", format, name);

   /* Degenerate to "no periods, no spaces" abbrev again */
   bt_set_format_text (format, BTN_FIRST, NULL, NULL, NULL, "");
   bt_set_format_options (format, BTN_FIRST, TRUE, BTJ_NOTHING, BTJ_SPACE);
   show_formatted_name ("fname 6", format, name);

   /* OK, let's play at something a little more "custom": kindergarten-
    * style names (full first name, abbreviated last name, forget about
    * 'von' and 'jr'.
    */
   bt_free_name_format (format);
   format = bt_create_name_format ("fl", FALSE);
   bt_set_format_text (format, BTN_LAST, NULL, NULL, NULL, ".");
   bt_set_format_options (format, BTN_LAST, TRUE, BTJ_MAYTIE, BTJ_SPACE);
   show_formatted_name ("fname 7", format, name);

   /* 'von' and 'last' only, abbreviated with no periods or spaces */
   bt_free_name_format (format);
   format = bt_create_name_format ("vl", FALSE);
   bt_set_format_options (format, BTN_VON, TRUE, BTJ_NOTHING, BTJ_NOTHING);
   bt_set_format_options (format, BTN_LAST, TRUE, BTJ_NOTHING, BTJ_NOTHING);
   show_formatted_name ("fname 8", format, name);

   bt_free_name_format (format);
   bt_free_name (name);

} /* process_name () */


int
main (void)
{
   char   line[1024];
   int    line_num;
   int    len;
   bt_stringlist * names;
   int    i;

   while (! feof (stdin))
   {
      if (fgets (line, 1024, stdin) == NULL)
         break;

      len = strlen (line);
      if (line[len-1] == '\n') line[len-1] = '\0';
      line_num++;

      names = bt_split_list (line, "and", "stdin", line_num, "name");
      if (names == NULL)
         printf ("empty or invalid string\n");
      else
      {
         if (names->num_items > 1)
            printf ("%d names in string\n", names->num_items);

         for (i = 0; i < names->num_items; i++)
         {
            if (names->items[i])
               process_name (names->items[i], line_num, i+1);
         }
         bt_free_list (names);
      }
   }
   return 0;
}