1 | /* $Id: fixname.c 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
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("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/lib/fixname.c $ $Id: fixname.c 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
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(unsigned char *p) |
---|
52 | { |
---|
53 | int up; /* Should next letter be uppercase */ |
---|
54 | int pos; /* Position within word */ |
---|
55 | |
---|
56 | for (up = 1, pos = 1; *p; p++, pos++) |
---|
57 | { |
---|
58 | if (!up && isupper(*p)) |
---|
59 | *p = tolower(*p); |
---|
60 | else if (up && islower(*p)) |
---|
61 | *p = toupper(*p); |
---|
62 | |
---|
63 | if (isalpha(*p)) /* If letter, next letter should be lower */ |
---|
64 | up = 0; |
---|
65 | else if (isspace(*p)) /* If space, next letter should be upper */ |
---|
66 | { |
---|
67 | pos = 0; |
---|
68 | up = 1; |
---|
69 | } |
---|
70 | else if (*p == '\'') /* If ', next letter should be upper only */ |
---|
71 | up = (pos == 2); /* if the ' is the 2nd char in the name */ |
---|
72 | else if (*p >= 0x80) /* If the high bit is set, don't touch it. */ |
---|
73 | up = 0; |
---|
74 | else |
---|
75 | up = 1; /* If other punctuation (eg, -), upper */ |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | void LookForJrAndIII(char *nm, int *pends_jr, int *pends_sr, int *pends_ii, |
---|
80 | int *pends_iii, int *pends_iv, int *pends_v) |
---|
81 | { |
---|
82 | int len = strlen(nm); |
---|
83 | |
---|
84 | if (len >= 4 && !strcmp(nm + len - 3, " JR")) |
---|
85 | { |
---|
86 | *pends_jr = 1; |
---|
87 | nm[len - 3] = '\0'; |
---|
88 | } |
---|
89 | else if (len >= 4 && !strcmp(nm + len - 3, " SR")) |
---|
90 | { |
---|
91 | *pends_sr = 1; |
---|
92 | nm[len - 3] = '\0'; |
---|
93 | } |
---|
94 | else if (len >= 4 && !strcmp(nm + len - 3, " IV")) |
---|
95 | { |
---|
96 | *pends_iv = 1; |
---|
97 | nm[len - 3] = '\0'; |
---|
98 | } |
---|
99 | else if (len >= 5 && !strcmp(nm + len - 4, " JR.")) |
---|
100 | { |
---|
101 | *pends_jr = 1; |
---|
102 | nm[len - 4] = '\0'; |
---|
103 | } |
---|
104 | else if (len >= 5 && !strcmp(nm + len - 4, " III")) |
---|
105 | { |
---|
106 | *pends_iii = 1; |
---|
107 | nm[len - 4] = '\0'; |
---|
108 | } |
---|
109 | else if (len >= 4 && !strcmp(nm + len - 3, " II")) |
---|
110 | { |
---|
111 | *pends_ii = 1; |
---|
112 | nm[len - 3] = '\0'; |
---|
113 | } |
---|
114 | else if (len >= 3 && !strcmp(nm + len - 2, " V")) |
---|
115 | { |
---|
116 | *pends_v = 1; |
---|
117 | nm[len - 2] = '\0'; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /* XXX no way to avoid possible buffer overrun here since we don't know |
---|
122 | how long nm is and we're trying to make it one longer */ |
---|
123 | void LookForSt(char *nm) /* ST PIERRE, etc. */ |
---|
124 | { |
---|
125 | char temp[256]; |
---|
126 | |
---|
127 | if (!strcmp(nm, "ST ")) |
---|
128 | { |
---|
129 | strcpy(temp, nm + 3); |
---|
130 | strcpy(nm, "ST. "); |
---|
131 | strcat(nm, temp); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void LookForO(char *nm) /* O BRIEN, etc. */ |
---|
136 | { |
---|
137 | if (!strcmp(nm, "O ") && isalpha(nm[2])) |
---|
138 | nm[1] = '\''; |
---|
139 | } |
---|
140 | |
---|
141 | void TrimTrailingSpace(char *ip) |
---|
142 | { |
---|
143 | char *p; |
---|
144 | for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--) |
---|
145 | *p = '\0'; |
---|
146 | } |
---|
147 | |
---|
148 | void GetMidInit(char *nm, char *mi) |
---|
149 | { |
---|
150 | while (*nm && !isspace(*nm)) |
---|
151 | nm++; |
---|
152 | if (*nm) |
---|
153 | *nm++ = '\0'; |
---|
154 | while (*nm && isspace(*nm)) |
---|
155 | nm++; |
---|
156 | if (*nm) |
---|
157 | *mi++ = *nm; |
---|
158 | *mi = '\0'; |
---|
159 | } |
---|
160 | |
---|
161 | /* Function Name: RemoveHyphens |
---|
162 | * Description: Removes all hyphens from the string passed to it. |
---|
163 | * Arguments: str - the string to remove the hyphens from |
---|
164 | * Returns: none |
---|
165 | */ |
---|
166 | |
---|
167 | void RemoveHyphens(char *str) |
---|
168 | { |
---|
169 | char *hyphen; |
---|
170 | |
---|
171 | while ((hyphen = strchr(str, '-'))) |
---|
172 | strcpy(hyphen, hyphen + 1); |
---|
173 | } |
---|