]> andersk Git - moira.git/blame - lib/fixname.c
Win32 portability mods for Pismere.
[moira.git] / lib / fixname.c
CommitLineData
fa59b86f 1/* $Id$
de56407f 2 *
7ac48069 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>.
de56407f 8 */
9
babbc197 10#include <mit-copyright.h>
7ac48069 11#include <moira.h>
12
de56407f 13#include <ctype.h>
7ac48069 14#include <string.h>
15
16RCSID("$Header$");
de56407f 17
965c2702 18#define LAST_LEN 100
19#define FIRST_LEN 100
de56407f 20
5eaef520 21void FixName(char *ilnm, char *ifnm, char *last, char *first, char *middle)
de56407f 22{
7ac48069 23 int ends_jr = 0, ends_sr = 0;
24 int ends_iii = 0, ends_iv = 0, ends_ii = 0, ends_v = 0;
de56407f 25
5eaef520 26 uppercase(ilnm);
27 uppercase(ifnm);
de56407f 28
5eaef520 29 /* Last name ... */
de56407f 30
5eaef520 31 TrimTrailingSpace(ilnm);
7ac48069 32 LookForJrAndIII(ilnm, &ends_jr, &ends_sr,
33 &ends_ii, &ends_iii, &ends_iv, &ends_v);
5eaef520 34 LookForSt(ilnm);
35 LookForO(ilnm);
36 FixCase(ilnm);
37 strncpy(last, ilnm, LAST_LEN);
de56407f 38
5eaef520 39 /* First name & middle initial ... */
de56407f 40
5eaef520 41 TrimTrailingSpace(ifnm);
7ac48069 42 LookForJrAndIII(ifnm, &ends_jr, &ends_sr,
43 &ends_ii, &ends_iii, &ends_iv, &ends_v);
de56407f 44
5eaef520 45 GetMidInit(ifnm, middle);
46
47 FixCase(ifnm);
5eaef520 48 strncpy(first, ifnm, FIRST_LEN);
de56407f 49}
0d8039b1 50
7ac48069 51void FixCase(char *p)
de56407f 52{
3f9c37b0 53 int up; /* Should next letter be uppercase */
54 int pos; /* Position within word */
de56407f 55
3f9c37b0 56 for (up = 1, pos = 1; *p; p++, pos++)
5eaef520 57 {
3f9c37b0 58 if (!up && isupper(*p))
5eaef520 59 *p = tolower(*p);
3f9c37b0 60 else if (up && islower(*p))
5eaef520 61 *p = toupper(*p);
3f9c37b0 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 */
5eaef520 72 else
3f9c37b0 73 up = 1; /* If other punctuation (eg, -), upper */
de56407f 74 }
75}
76
7ac48069 77void LookForJrAndIII(char *nm, int *pends_jr, int *pends_sr, int *pends_ii,
78 int *pends_iii, int *pends_iv, int *pends_v)
de56407f 79{
44d12d58 80 int len = strlen(nm);
de56407f 81
5eaef520 82 if (len >= 4 && !strcmp(nm + len - 3, " JR"))
83 {
84 *pends_jr = 1;
85 nm[len - 3] = '\0';
de56407f 86 }
7ac48069 87 else if (len >= 4 && !strcmp(nm + len - 3, " SR"))
88 {
89 *pends_sr = 1;
90 nm[len - 3] = '\0';
91 }
5eaef520 92 else if (len >= 4 && !strcmp(nm + len - 3, " IV"))
93 {
94 *pends_iv = 1;
95 nm[len - 3] = '\0';
de56407f 96 }
5eaef520 97 else if (len >= 5 && !strcmp(nm + len - 4, " JR."))
98 {
99 *pends_jr = 1;
100 nm[len - 4] = '\0';
de56407f 101 }
5eaef520 102 else if (len >= 5 && !strcmp(nm + len - 4, " III"))
103 {
104 *pends_iii = 1;
105 nm[len - 4] = '\0';
de56407f 106 }
5eaef520 107 else if (len >= 4 && !strcmp(nm + len - 3, " II"))
108 {
109 *pends_ii = 1;
110 nm[len - 3] = '\0';
9d787e1c 111 }
5eaef520 112 else if (len >= 3 && !strcmp(nm + len - 2, " V"))
113 {
114 *pends_v = 1;
115 nm[len - 2] = '\0';
9d787e1c 116 }
de56407f 117}
118
a3bbcc2b 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 */
7ac48069 121void LookForSt(char *nm) /* ST PIERRE, etc. */
de56407f 122{
5eaef520 123 char temp[256];
de56407f 124
5eaef520 125 if (!strcmp(nm, "ST "))
126 {
127 strcpy(temp, nm + 3);
128 strcpy(nm, "ST. ");
129 strcat(nm, temp);
de56407f 130 }
131}
132
7ac48069 133void LookForO(char *nm) /* O BRIEN, etc. */
de56407f 134{
5eaef520 135 if (!strcmp(nm, "O ") && isalpha(nm[2]))
136 nm[1] = '\'';
de56407f 137}
138
7ac48069 139void TrimTrailingSpace(char *ip)
de56407f 140{
44d12d58 141 char *p;
5eaef520 142 for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--)
143 *p = '\0';
de56407f 144}
145
7ac48069 146void GetMidInit(char *nm, char *mi)
de56407f 147{
5eaef520 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';
de56407f 157}
33b0f7c3 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
165void RemoveHyphens(char *str)
166{
167 char *hyphen;
168
169 while ((hyphen = strchr(str, '-')))
170 strcpy(hyphen, hyphen + 1);
171}
This page took 0.123698 seconds and 5 git commands to generate.