]>
Commit | Line | Data |
---|---|---|
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 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 | |
73 | up = 1; /* If other punctuation (eg, -), upper */ | |
74 | } | |
75 | } | |
76 | ||
77 | void LookForJrAndIII(char *nm, int *pends_jr, int *pends_sr, int *pends_ii, | |
78 | int *pends_iii, int *pends_iv, int *pends_v) | |
79 | { | |
80 | int len = strlen(nm); | |
81 | ||
82 | if (len >= 4 && !strcmp(nm + len - 3, " JR")) | |
83 | { | |
84 | *pends_jr = 1; | |
85 | nm[len - 3] = '\0'; | |
86 | } | |
87 | else if (len >= 4 && !strcmp(nm + len - 3, " SR")) | |
88 | { | |
89 | *pends_sr = 1; | |
90 | nm[len - 3] = '\0'; | |
91 | } | |
92 | else if (len >= 4 && !strcmp(nm + len - 3, " IV")) | |
93 | { | |
94 | *pends_iv = 1; | |
95 | nm[len - 3] = '\0'; | |
96 | } | |
97 | else if (len >= 5 && !strcmp(nm + len - 4, " JR.")) | |
98 | { | |
99 | *pends_jr = 1; | |
100 | nm[len - 4] = '\0'; | |
101 | } | |
102 | else if (len >= 5 && !strcmp(nm + len - 4, " III")) | |
103 | { | |
104 | *pends_iii = 1; | |
105 | nm[len - 4] = '\0'; | |
106 | } | |
107 | else if (len >= 4 && !strcmp(nm + len - 3, " II")) | |
108 | { | |
109 | *pends_ii = 1; | |
110 | nm[len - 3] = '\0'; | |
111 | } | |
112 | else if (len >= 3 && !strcmp(nm + len - 2, " V")) | |
113 | { | |
114 | *pends_v = 1; | |
115 | nm[len - 2] = '\0'; | |
116 | } | |
117 | } | |
118 | ||
119 | /* XXX no way to avoid possible buffer overrun here since we don't know | |
120 | how long nm is and we're trying to make it one longer */ | |
121 | void LookForSt(char *nm) /* ST PIERRE, etc. */ | |
122 | { | |
123 | char temp[256]; | |
124 | ||
125 | if (!strcmp(nm, "ST ")) | |
126 | { | |
127 | strcpy(temp, nm + 3); | |
128 | strcpy(nm, "ST. "); | |
129 | strcat(nm, temp); | |
130 | } | |
131 | } | |
132 | ||
133 | void LookForO(char *nm) /* O BRIEN, etc. */ | |
134 | { | |
135 | if (!strcmp(nm, "O ") && isalpha(nm[2])) | |
136 | nm[1] = '\''; | |
137 | } | |
138 | ||
139 | void TrimTrailingSpace(char *ip) | |
140 | { | |
141 | char *p; | |
142 | for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--) | |
143 | *p = '\0'; | |
144 | } | |
145 | ||
146 | void GetMidInit(char *nm, char *mi) | |
147 | { | |
148 | while (*nm && !isspace(*nm)) | |
149 | nm++; | |
150 | if (*nm) | |
151 | *nm++ = '\0'; | |
152 | while (*nm && isspace(*nm)) | |
153 | nm++; | |
154 | if (*nm) | |
155 | *mi++ = *nm; | |
156 | *mi = '\0'; | |
157 | } | |
158 | ||
159 | /* Function Name: RemoveHyphens | |
160 | * Description: Removes all hyphens from the string passed to it. | |
161 | * Arguments: str - the string to remove the hyphens from | |
162 | * Returns: none | |
163 | */ | |
164 | ||
165 | void RemoveHyphens(char *str) | |
166 | { | |
167 | char *hyphen; | |
168 | ||
169 | while ((hyphen = strchr(str, '-'))) | |
170 | strcpy(hyphen, hyphen + 1); | |
171 | } |