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