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

Revision 25198, 7.1 KB checked in by jdreed, 13 years ago (diff)
In moira: * Snapshot moira@r4042 (6/28/11) * Update version number to include moira revision number
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 4041 2011-06-23 22:09:46Z 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#define LOC_SCHOOL_CODE (LOC_OPHONE + LEN_OPHONE)
71#define LEN_SCHOOL_CODE 2
72
73EXEC SQL BEGIN DECLARE SECTION;
74int who;
75char *prog = "stuload";
76EXEC SQL END DECLARE SECTION;
77
78char *whoami;
79
80struct entry *get_next_entry(FILE *in);
81void process_entry(struct entry *e, int secure);
82
83int main(int argc, char **argv)
84{
85  FILE *in;
86  struct entry *e;
87  int i, wait = 0;
88  char buf[80], *file = NULL;
89  EXEC SQL BEGIN DECLARE SECTION;
90  char *db = "moira";
91  EXEC SQL END DECLARE SECTION;
92
93  whoami = strrchr(argv[0], '/');
94  if (whoami)
95    whoami++;
96  else
97    whoami = argv[0];
98
99  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
100  setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
101
102  for (i = 1; i < argc; i++)
103    {
104      if (!strcmp(argv[i], "-w"))
105        wait++;
106      if (file)
107        {
108          fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
109          exit(1);
110        }
111      else
112        file = argv[i];
113    }
114
115  if (!file)
116    {
117      fprintf(stderr, "Usage: %s [-w] inputfile\n", whoami);
118      exit(1);
119    }
120
121  in = fopen(file, "r");
122  if (!in)
123    {
124      fprintf(stderr, "Unable to open %s for input\n", file);
125      exit(1);
126    }
127
128  initialize_sms_error_table();
129
130  EXEC SQL CONNECT :db IDENTIFIED BY :db;
131  if (sqlca.sqlcode)
132    {
133      dbmserr("connecting", sqlca.sqlcode);
134      exit(1);
135    }
136
137  EXEC SQL SELECT users_id INTO :who FROM users WHERE login = 'root';
138
139  while ((e = get_next_entry(in)))
140    {
141      /* Don't require secure registration for cross-registered students. */
142      if (!strcmp(e->school_code, "Z"))
143        process_entry(e, 0);
144      else
145        process_entry(e, 1);
146      EXEC SQL COMMIT WORK;
147      if (sqlca.sqlcode)
148        {
149          dbmserr("committing work", sqlca.sqlcode);
150          exit(1);
151        }
152      if (wait)
153        {
154          printf("Next");
155          fflush(stdout);
156          fgets(buf, sizeof(buf), stdin);
157        }
158    }
159
160  exit(0);
161}
162
163
164struct entry *get_next_entry(FILE *in)
165{
166  static struct entry e;
167  static char buf[BUFSIZ], classbuf[10];
168  static char namebuf[LEN_FIRST + LEN_MIDDLE + LEN_LAST + 2];
169  static char addrbuf[USERS_HOME_ADDR_SIZE], xaddrbuf[USERS_XADDRESS_SIZE];
170  static char first[LEN_FIRST + 1], last[LEN_LAST + 1], middle[LEN_MIDDLE + 1];
171  static char id[LEN_ID + 1], course[LEN_COURSE + 1];
172  static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
173  static char city[LEN_CITY + 1];
174  static char state[LEN_STATE + 1], phone[LEN_PHONE + 1];
175  static char ophone[LEN_OPHONE + 1], title[128];
176  static char zip[LEN_ZIP + 1], oaddr[LEN_OADDR + 1];
177  static char school_code[LEN_SCHOOL_CODE + 1];
178  static int nyear = 0;
179  int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
180  char *p;
181
182  if (nyear == 0)
183    {
184      struct tm *tm;
185      struct timeval tv;
186
187      gettimeofday(&tv, NULL);
188      tm = localtime(&tv.tv_sec);
189      nyear = tm->tm_year;
190      if (tm->tm_mon >= 5)
191        nyear++;
192    }
193
194  if (!fgets(buf, sizeof(buf), in))
195    return NULL;
196
197  strlcpy(id, &buf[LOC_ID], LEN_ID + 1);
198  strtrim(id);
199  strlcpy(last, &buf[LOC_LAST], LEN_LAST + 1);
200  strtrim(last);
201  strlcpy(first, &buf[LOC_FIRST], LEN_FIRST + 1);
202  strtrim(first);
203  strlcpy(middle, &buf[LOC_MIDDLE], LEN_MIDDLE + 1);
204  strtrim(middle);
205  strlcpy(year, &buf[LOC_YEAR], LEN_YEAR + 1);
206  strtrim(year);
207  strlcpy(course, &buf[LOC_COURSE], LEN_COURSE + 1);
208  strtrim(course);
209  strlcpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS + 1);
210  strtrim(address);
211  strlcpy(city, &buf[LOC_CITY], LEN_CITY + 1);
212  strtrim(city);
213  strlcpy(state, &buf[LOC_STATE], LEN_STATE + 1);
214  strtrim(state);
215  strlcpy(zip, &buf[LOC_ZIP], LEN_ZIP + 1);
216  strtrim(zip);
217  strlcpy(phone, &buf[LOC_PHONE], LEN_PHONE + 1);
218  strtrim(phone);
219  strlcpy(oaddr, &buf[LOC_OADDR], LEN_OADDR + 1);
220  strtrim(oaddr);
221  strlcpy(ophone, &buf[LOC_OPHONE], LEN_OPHONE + 1);
222  strtrim(ophone);
223  strlcpy(school_code, &buf[LOC_SCHOOL_CODE], LEN_SCHOOL_CODE + 1);
224  strtrim(school_code);
225
226  e.first = first;
227  e.last = last;
228  e.middle = middle;
229  ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
230  LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
231                  &ends_ii, &ends_v);
232  if (middle[1] == '.' && middle[2] == '\0')
233    middle[1] = '\0';
234  e.name = namebuf;
235  if (*middle)
236    sprintf(e.name, "%s %s %s", first, middle, last);
237  else
238    sprintf(e.name, "%s %s", first, last);
239
240  e.id = id;
241
242  e.xtitle = title;
243  if (year[0] == 'G' || year[0] == 'N')
244    {
245      e.type = "G";
246      sprintf(title, "Grad Student");
247    }
248  else
249    {
250      e.type = classbuf;
251      sprintf(classbuf, "%d", nyear + 4 - atoi(year) + 1900);
252      sprintf(title, "Undergrad (class of %s)", classbuf);
253    }
254
255  e.dept = course;
256
257  /* Used to detect cross-registered students. */
258  e.school_code = school_code;
259
260  e.oaddr = oaddr;
261  fixaddress(e.oaddr);
262  e.ophone = e.xphone2 = ophone;
263  fixphone(e.ophone);
264
265  e.haddr = addrbuf;
266  strlcpy(e.haddr, address, sizeof(addrbuf));
267  if (*city)
268    {
269      strlcat(e.haddr, " ", sizeof(addrbuf));
270      strlcat(e.haddr, city, sizeof(addrbuf));
271    }
272  if (*state)
273    {
274      strlcat(e.haddr, " ", sizeof(addrbuf));
275      strlcat(e.haddr, state, sizeof(addrbuf));
276      strlcat(e.haddr, zip, sizeof(addrbuf));
277    }
278  fixaddress(e.haddr);
279
280  e.hphone = e.xphone1 = phone;
281  fixphone(e.hphone);
282
283  e.xaddress = xaddrbuf;
284  strlcpy(e.xaddress, address, sizeof(xaddrbuf));
285  strlcat(e.xaddress, " |", sizeof(xaddrbuf));
286  if (*city)
287    {
288      strlcat(e.xaddress, city, sizeof(xaddrbuf));
289      if (*state)
290        {
291          strlcat(e.xaddress, " ", sizeof(xaddrbuf));
292          strlcat(e.xaddress, state, sizeof(xaddrbuf));
293          strlcat(e.xaddress, zip, sizeof(xaddrbuf));
294        }
295    }
296  else
297    strlcat(e.xaddress, "MIT INTERDEPARTMENTAL MAIL", sizeof(xaddrbuf));
298
299  return &e;
300}
Note: See TracBrowser for help on using the repository browser.