source: trunk/third/moira/regtape/student.pc @ 24319

Revision 24319, 6.7 KB checked in by broder, 15 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: student.pc 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 * Load data into Moira from Registrar's Office data file
4 *
5 * Copyright (C) 1990-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#include <moira_site.h>
13#include <moira_schema.h>
14#include "common.h"
15
16#include <ctype.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <time.h>
21
22EXEC SQL INCLUDE sqlca;
23
24RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/regtape/student.pc $ $Id: student.pc 3956 2010-01-05 20:56:56Z zacheiss $");
25
26/* File format is:
27
280-8     MIT ID
299-38    last name
3039-68   first name
3169-98   middle name
3299      year
33100-103 department
34104-133 address
35134-173 city
36174-175 state
37176-184 zip code
38185-204 phone
39205-234 office address
40235-254 office phone
41
42*/
43
44#define LOC_ID 0
45#define LEN_ID 9
46#define LOC_LAST (LOC_ID + LEN_ID)
47#define LEN_LAST 30
48#define LOC_FIRST (LOC_LAST + LEN_LAST)
49#define LEN_FIRST 30
50#define LOC_MIDDLE (LOC_FIRST + LEN_FIRST)
51#define LEN_MIDDLE 30
52#define LOC_YEAR (LOC_MIDDLE + LEN_MIDDLE)
53#define LEN_YEAR 1
54#define LOC_COURSE (LOC_YEAR + LEN_YEAR)
55#define LEN_COURSE 4
56#define LOC_ADDRESS (LOC_COURSE + LEN_COURSE)
57#define LEN_ADDRESS 30
58#define LOC_CITY (LOC_ADDRESS + LEN_ADDRESS)
59#define LEN_CITY 40
60#define LOC_STATE (LOC_CITY + LEN_CITY)
61#define LEN_STATE 2
62#define LOC_ZIP (LOC_STATE + LEN_STATE)
63#define LEN_ZIP 9
64#define LOC_PHONE (LOC_ZIP + LEN_ZIP)
65#define LEN_PHONE 20
66#define LOC_OADDR (LOC_PHONE + LEN_PHONE)
67#define LEN_OADDR 30
68#define LOC_OPHONE (LOC_OADDR + LEN_OADDR)
69#define LEN_OPHONE 20
70
71EXEC SQL BEGIN DECLARE SECTION;
72int who;
73char *prog = "stuload";
74EXEC SQL END DECLARE SECTION;
75
76char *whoami;
77
78struct entry *get_next_entry(FILE *in);
79void process_entry(struct entry *e, int secure);
80
81int main(int argc, char **argv)
82{
83  FILE *in;
84  struct entry *e;
85  int i, wait = 0;
86  char buf[80], *file = NULL;
87  EXEC SQL BEGIN DECLARE SECTION;
88  char *db = "moira";
89  EXEC SQL END DECLARE SECTION;
90
91  whoami = strrchr(argv[0], '/');
92  if (whoami)
93    whoami++;
94  else
95    whoami = argv[0];
96
97  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
98  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
99
100  for (i = 1; i < argc; i++)
101    {
102      if (!strcmp(argv[i], "-w"))
103        wait++;
104      if (file)
105        {
106          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
107          exit(1);
108        }
109      else
110        file = argv[i];
111    }
112
113  if (!file)
114    {
115      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
116      exit(1);
117    }
118
119  in = fopen(file, "r");
120  if (!in)
121    {
122      fprintf(stderr, "Unable to open %s for input\n", file);
123      exit(1);
124    }
125
126  initialize_sms_error_table();
127
128  EXEC SQL CONNECT :db IDENTIFIED BY :db;
129  if (sqlca.sqlcode)
130    {
131      dbmserr("connecting", sqlca.sqlcode);
132      exit(1);
133    }
134
135  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
136
137  while ((e = get_next_entry(in)))
138    {
139      process_entry(e, 1);
140      EXEC SQL COMMIT WORK;
141      if (sqlca.sqlcode)
142        {
143          dbmserr("committing work", sqlca.sqlcode);
144          exit(1);
145        }
146      if (wait)
147        {
148          printf("Next");
149          fflush(stdout);
150          fgets(buf, sizeof(buf), stdin);
151        }
152    }
153
154  exit(0);
155}
156
157
158struct entry *get_next_entry(FILE *in)
159{
160  static struct entry e;
161  static char buf[BUFSIZ], classbuf[10];
162  static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2];
163  static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE];
164  static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1];
165  static char id[LEN_ID + 1], course[LEN_COURSE + 1];
166  static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
167  static char city[LEN_CITY + 1];
168  static char state[LEN_STATE + 1], phone[LEN_PHONE + 1];
169  static char ophone[LEN_OPHONE + 1], title[128];
170  static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1];
171  static int nyear = 0;
172  int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
173  char *p;
174
175  if (nyear == 0)
176    {
177      struct tm *tm;
178      struct timeval tv;
179
180      gettimeofday(&tv, NULL);
181      tm = localtime(&tv.tv_sec);
182      nyear = tm->tm_year;
183      if (tm->tm_mon >= 5)
184        nyear++;
185    }
186
187  if (!fgets(buf, sizeof(buf), in))
188    return NULL;
189
190  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
191  strtrim(id);
192  strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1);
193  strtrim(last);
194  strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1);
195  strtrim(first);
196  strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1);
197  strtrim(middle);
198  strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1);
199  strtrim(year);
200  strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1);
201  strtrim(course);
202  strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1);
203  strtrim(address);
204  strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1);
205  strtrim(city);
206  strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1);
207  strtrim(state);
208  strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1);
209  strtrim(zip);
210  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
211  strtrim(phone);
212  strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1);
213  strtrim(oaddr);
214  strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1);
215  strtrim(ophone);
216
217  e.first = first;
218  e.last = last;
219  e.middle = middle;
220  ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
221  LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
222                  &ends_ii, &ends_v);
223  if (middle[1] == '.' && middle[2] == '\0')
224    middle[1] = '\0';
225  e.name = namebuf;
226  if (*middle)
227    sprintf(e.name, "%s %s %s", first, middle, last);
228  else
229    sprintf(e.name, "%s %s", first, last);
230
231  e.id = id;
232
233  e.xtitle = title;
234  if (year[0] == 'G' || year[0] == 'N')
235    {
236      e.type = "G";
237      sprintf(title, "Grad Student");
238    }
239  else
240    {
241      e.type = classbuf;
242      sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
243      sprintf(title, "Undergrad (class of %s)", classbuf);
244    }
245
246  e.dept = course;
247
248  e.oaddr = oaddr;
249  fixaddress(e.oaddr);
250  e.ophone = e.xphone2 = ophone;
251  fixphone(e.ophone);
252
253  e.haddr = addrbuf;
254  strlcpy(e.haddr, address, sizeof(addrbuf));
255  if (*city)
256    {
257      strlcat(e.haddr, " ", sizeof(addrbuf));
258      strlcat(e.haddr, city, sizeof(addrbuf));
259    }
260  if (*state)
261    {
262      strlcat(e.haddr, " ", sizeof(addrbuf));
263      strlcat(e.haddr, state, sizeof(addrbuf));
264      strlcat(e.haddr, zip, sizeof(addrbuf));
265    }
266  fixaddress(e.haddr);
267
268  e.hphone = e.xphone1 = phone;
269  fixphone(e.hphone);
270
271  e.xaddress = xaddrbuf;
272  strlcpy(e.xaddress, address, sizeof(xaddrbuf));
273  strlcat(e.xaddress, " |", sizeof(xaddrbuf));
274  if (*city)
275    {
276      strlcat(e.xaddress, city, sizeof(xaddrbuf));
277      if (*state)
278        {
279          strlcat(e.xaddress, " ", sizeof(xaddrbuf));
280          strlcat(e.xaddress, state, sizeof(xaddrbuf));
281          strlcat(e.xaddress, zip, sizeof(xaddrbuf));
282        }
283    }
284  else
285    strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));
286
287  return &e;
288}
Note: See TracBrowser for help on using the repository browser.