]> andersk Git - moira.git/blame - lib/fixname.c
provide non-blocking interfaces to mr_receive and mr_accept
[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{
44d12d58 53 int cflag; /* convert to lcase, unless at start or following */
54 /* a space or punctuation mark (e.g., '-') */
de56407f 55
5eaef520 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;
de56407f 66 }
67}
68
7ac48069 69void LookForJrAndIII(char *nm, int *pends_jr, int *pends_sr, int *pends_ii,
70 int *pends_iii, int *pends_iv, int *pends_v)
de56407f 71{
44d12d58 72 int len = strlen(nm);
de56407f 73
5eaef520 74 if (len >= 4 && !strcmp(nm + len - 3, " JR"))
75 {
76 *pends_jr = 1;
77 nm[len - 3] = '\0';
de56407f 78 }
7ac48069 79 else if (len >= 4 && !strcmp(nm + len - 3, " SR"))
80 {
81 *pends_sr = 1;
82 nm[len - 3] = '\0';
83 }
5eaef520 84 else if (len >= 4 && !strcmp(nm + len - 3, " IV"))
85 {
86 *pends_iv = 1;
87 nm[len - 3] = '\0';
de56407f 88 }
5eaef520 89 else if (len >= 5 && !strcmp(nm + len - 4, " JR."))
90 {
91 *pends_jr = 1;
92 nm[len - 4] = '\0';
de56407f 93 }
5eaef520 94 else if (len >= 5 && !strcmp(nm + len - 4, " III"))
95 {
96 *pends_iii = 1;
97 nm[len - 4] = '\0';
de56407f 98 }
5eaef520 99 else if (len >= 4 && !strcmp(nm + len - 3, " II"))
100 {
101 *pends_ii = 1;
102 nm[len - 3] = '\0';
9d787e1c 103 }
5eaef520 104 else if (len >= 3 && !strcmp(nm + len - 2, " V"))
105 {
106 *pends_v = 1;
107 nm[len - 2] = '\0';
9d787e1c 108 }
de56407f 109}
110
a3bbcc2b 111/* XXX no way to avoid possible buffer overrun here since we don't know
112 how long nm is and we're trying to make it one longer */
7ac48069 113void LookForSt(char *nm) /* ST PIERRE, etc. */
de56407f 114{
5eaef520 115 char temp[256];
de56407f 116
5eaef520 117 if (!strcmp(nm, "ST "))
118 {
119 strcpy(temp, nm + 3);
120 strcpy(nm, "ST. ");
121 strcat(nm, temp);
de56407f 122 }
123}
124
7ac48069 125void LookForO(char *nm) /* O BRIEN, etc. */
de56407f 126{
5eaef520 127 if (!strcmp(nm, "O ") && isalpha(nm[2]))
128 nm[1] = '\'';
de56407f 129}
130
7ac48069 131void TrimTrailingSpace(char *ip)
de56407f 132{
44d12d58 133 char *p;
5eaef520 134 for (p = ip + strlen(ip) - 1; p >= ip && isspace(*p); p--)
135 *p = '\0';
de56407f 136}
137
7ac48069 138void GetMidInit(char *nm, char *mi)
de56407f 139{
5eaef520 140 while (*nm && !isspace(*nm))
141 nm++;
142 if (*nm)
143 *nm++ = '\0';
144 while (*nm && isspace(*nm))
145 nm++;
146 if (*nm)
147 *mi++ = *nm;
148 *mi = '\0';
de56407f 149}
This page took 0.102553 seconds and 5 git commands to generate.