]> andersk Git - moira.git/blob - lib/fixname.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / lib / fixname.c
1 /* $Id $
2  *
3  * Put a name into Moira-canonical form
4  *
5  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12
13 #include <ctype.h>
14 #include <string.h>
15
16 RCSID("$Header$");
17
18 #define LAST_LEN                100
19 #define FIRST_LEN               100
20
21 void FixName(char *ilnm, char *ifnm, char *last, char *first, char *middle)
22 {
23   int ends_jr = 0, ends_sr = 0;
24   int ends_iii = 0, ends_iv = 0, ends_ii = 0, ends_v = 0;
25
26   uppercase(ilnm);
27   uppercase(ifnm);
28
29   /* Last name ... */
30
31   TrimTrailingSpace(ilnm);
32   LookForJrAndIII(ilnm, &ends_jr, &ends_sr,
33                   &ends_ii, &ends_iii, &ends_iv, &ends_v);
34   LookForSt(ilnm);
35   LookForO(ilnm);
36   FixCase(ilnm);
37   strncpy(last, ilnm, LAST_LEN);
38
39   /* First name  & middle initial ... */
40
41   TrimTrailingSpace(ifnm);
42   LookForJrAndIII(ifnm, &ends_jr, &ends_sr,
43                   &ends_ii, &ends_iii, &ends_iv, &ends_v);
44
45   GetMidInit(ifnm, middle);
46
47   FixCase(ifnm);
48   strncpy(first, ifnm, FIRST_LEN);
49 }
50
51 void FixCase(char *p)
52 {
53   int cflag;    /* convert to lcase, unless at start or following */
54                 /* a space or punctuation mark (e.g., '-') */
55
56   for (cflag = 0; *p; p++)
57     {
58       if (cflag && isupper(*p))
59         *p = tolower(*p);
60       else if (!cflag && islower(*p))
61         *p = toupper(*p);
62       if (isalpha(*p))
63         cflag = 1;
64       else
65         cflag = 0;
66     }
67 }
68
69 void LookForJrAndIII(char *nm, int *pends_jr, int *pends_sr, int *pends_ii,
70                      int *pends_iii, int *pends_iv, int *pends_v)
71 {
72   int len = strlen(nm);
73
74   if (len >= 4 && !strcmp(nm + len - 3, " JR"))
75     {
76       *pends_jr = 1;
77       nm[len - 3] = '\0';
78     }
79   else if (len >= 4 && !strcmp(nm + len - 3, " SR"))
80     {
81       *pends_sr = 1;
82       nm[len - 3] = '\0';
83     }
84   else if (len >= 4 && !strcmp(nm + len - 3, " IV"))
85     {
86       *pends_iv = 1;
87       nm[len - 3] = '\0';
88     }
89   else if (len >= 5 && !strcmp(nm + len - 4, " JR."))
90     {
91       *pends_jr = 1;
92       nm[len - 4] = '\0';
93     }
94   else if (len >= 5 && !strcmp(nm + len - 4, " III"))
95     {
96       *pends_iii = 1;
97       nm[len - 4] = '\0';
98     }
99   else if (len >= 4 && !strcmp(nm + len - 3, " II"))
100     {
101       *pends_ii = 1;
102       nm[len - 3] = '\0';
103     }
104   else if (len >= 3 && !strcmp(nm + len - 2, " V"))
105     {
106       *pends_v = 1;
107       nm[len - 2] = '\0';
108     }
109 }
110
111 void LookForSt(char *nm)                /* ST PIERRE, etc. */
112 {
113   char temp[256];
114
115   if (!strcmp(nm, "ST "))
116     {
117       strcpy(temp, nm + 3);
118       strcpy(nm, "ST. ");
119       strcat(nm, temp);
120     }
121 }
122
123 void LookForO(char *nm)         /* O BRIEN, etc. */
124 {
125   if (!strcmp(nm, "O ") && isalpha(nm[2]))
126     nm[1] = '\'';
127 }
128
129 void TrimTrailingSpace(char *ip)
130 {
131   char *p;
132   for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--)
133     *p = '\0';
134 }
135
136 void GetMidInit(char *nm, char *mi)
137 {
138   while (*nm && !isspace(*nm))
139     nm++;
140   if (*nm)
141     *nm++ = '\0';
142   while (*nm && isspace(*nm))
143     nm++;
144   if (*nm)
145     *mi++ = *nm;
146   *mi = '\0';
147 }
This page took 0.045741 seconds and 5 git commands to generate.