Annotation of capa/capa51/pProj/allcapaid.c, revision 1.4

1.3       albertel    1: /* Generate all CAPA Ids for a class
                      2:    Copyright (C) 1992-2000 Michigan State University
                      3: 
                      4:    The CAPA system is free software; you can redistribute it and/or
                      5:    modify it under the terms of the GNU Library General Public License as
                      6:    published by the Free Software Foundation; either version 2 of the
                      7:    License, or (at your option) any later version.
                      8: 
                      9:    The CAPA system is distributed in the hope that it will be useful,
                     10:    but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                     12:    Library General Public License for more details.
                     13: 
                     14:    You should have received a copy of the GNU Library General Public
                     15:    License along with the CAPA system; see the file COPYING.  If not,
                     16:    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.4     ! albertel   17:    Boston, MA 02111-1307, USA.
        !            18: 
        !            19:    As a special exception, you have permission to link this program
        !            20:    with the TtH/TtM library and distribute executables, as long as you
        !            21:    follow the requirements of the GNU GPL in regard to all of the
        !            22:    software in the executable aside from TtH/TtM.
        !            23: */
1.1       albertel   24: 
                     25: /* ========================================================================== */
                     26: /*            allcapaid.c    created by Isaac Tsai                            */
                     27: /**************************************************************************** */
                     28: #ifdef NeXT
                     29: #include <stdlib.h>
                     30: #include <sys/types.h>
                     31: #include <sys/stat.h>  
                     32: #else
                     33: #include <malloc.h>
                     34: double atof();
                     35: #endif
                     36: 
                     37: #include <stdio.h>
                     38: #include <ctype.h>
                     39: #include <sys/types.h>
                     40: #include <sys/stat.h>  
                     41:  
                     42: #include <signal.h>
                     43: #include <time.h>
                     44: #include <math.h>
                     45: #include <string.h>
                     46: #ifdef NeXT
                     47: #include <bsd/curses.h>
                     48: #else
                     49: #include <curses.h>
                     50: #endif
                     51: #define   YES    1
                     52: 
                     53: #include "capaCommon.h"
                     54: #include "ranlib.h"
                     55: 
                     56: char  *progname;
                     57: 
                     58: /* filter out the number to be [1:999] */
                     59: int  scan_num(char *num_str,int *first, int *second) {
                     60:   char  tmp_str[SMALL_LINE_BUFFER], *ch;
                     61:   int   ii=0, a_num, b_num, result=0;
                     62:   
                     63:   ch = num_str;
                     64:   tmp_str[ii] = 0;
                     65:   while( isspace(*ch) ) ch++;
                     66:   while(isdigit(*ch)) { tmp_str[ii++] = *ch; ch++; }
                     67:   tmp_str[ii] = 0;
                     68:   sscanf(tmp_str,"%d",&a_num);
                     69:   if( a_num < 0 || a_num > 999 ) a_num = 1;
                     70:   *first = a_num;
                     71:   result = 1;
                     72:   while( isspace(*ch) ) ch++;
                     73:   if( *ch == ':' ) {
                     74:     ch++;
                     75:     while( isspace(*ch) ) ch++;
                     76:     ii=0; tmp_str[ii] = 0;
                     77:     while( isdigit(*ch) ) { tmp_str[ii++] = *ch; ch++; }
                     78:     tmp_str[ii] = 0;
                     79:     sscanf(tmp_str,"%d",&b_num);
                     80:     if( b_num < 0 || b_num > 999 ) b_num = 1;
                     81:     if( a_num > b_num )           b_num = a_num;
                     82:     *second = b_num;
                     83:     result = 2;
                     84:   }
                     85:   return (result);
                     86: }
                     87: 
                     88: void print_capaidplus (FILE *out,char* stunum,int set,int plus) 
                     89: {
                     90:   char fmt[SMALL_LINE_BUFFER];
                     91:   sprintf(fmt,"%%%dc",4+plus);
                     92:   fprintf(out,"%s ", capa_id_plus(stunum, set, plus));
                     93: }
                     94: 
                     95: void print_capaidplus_header1(FILE *dfp,int StartSet,int EndSet,int plus)
                     96: {
                     97:   int pluscnt,setIdx;
                     98:   for(setIdx = StartSet; setIdx <= EndSet; setIdx++)   
                     99:     {
                    100:       fprintf(dfp,"%3d",setIdx);
                    101:       for(pluscnt=0; pluscnt < (plus+2); pluscnt++)
                    102: 	fprintf(dfp," ");
                    103:     }
                    104: }
                    105: 
                    106: void print_capaidplus_header2(FILE *dfp,int StartSet,int EndSet,int plus)
                    107: {
                    108:   int setIdx,pluscnt;
                    109:   for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    110:     for(pluscnt=0; pluscnt < (plus+4); pluscnt++)
                    111:       fprintf(dfp,"-");
                    112:     fprintf(dfp,"|");
                    113:   }
                    114: }
                    115: 
                    116: void usage()
                    117: {
                    118:   printf("USAGE: %s [-s start-set] [-e end-set] [-stu student-number] [-c class-directory] [-d output-directory] [-h] [-i] [-p number] [-sec [n|n:m]]\n", progname);
                    119:   printf("       start-set : default 1\n");
                    120:   printf("       end-set   : default 10\n");
                    121:   printf("       student-number  : no default\n");
                    122:   printf("       class-directory : no default\n");
                    123:   printf("       output-directory: class-directory/capaID\n");
                    124:   printf("       -Sec 3          : for section 3\n");
                    125:   printf("       -Sec 3:7        : from section 3 to section 7\n");
                    126:   printf("       -i              : don't create files, print to the screen\n");
                    127:   printf("       -p 2            : output 6 charatcer capaidplus instead of capaid\n");
1.2       albertel  128:   printf("       -checkopen      : check if set is open, fail if it isn't.\n");
                    129:   printf("       -checkdue       : check if set is due, fail if it isn't.\n");
                    130:   printf("       -checkans       : check if set's answers are available, fail if they aren't.\n");
1.1       albertel  131:   printf("       -h              : prints this message\n");
                    132:   printf("       CAPA version %s, %s\n",CAPA_VER,COMPILE_DATE);
                    133: }
                    134: #define  F_CLASS       1
                    135: #define  F_SECTIONS    2
                    136: #define  F_STUDENT     3
                    137: int main (int argc, char  **argv) 
                    138: {
1.2       albertel  139:   T_student  *students_p,*s_p,student_data;
1.1       albertel  140:   int         i, setIdx, numStudents, count, tmp_num, StartSec=1,EndSec=999;
                    141:   int         StartSet = 1, EndSet = 10, classnameOK=0,outputDir=0,plus=0;
                    142:   int         SecCntArry[MAX_SECTION_COUNT], sectionIdx;   
                    143:   char        filename[MAX_BUFFER_SIZE], path[MAX_BUFFER_SIZE], 
                    144:     outputPath[MAX_BUFFER_SIZE], fullpath[MAX_BUFFER_SIZE];
1.2       albertel  145:   char        StuNum[MAX_STUDENT_NUMBER+1], fmt[16];
                    146:   int         ForWhat=F_CLASS, interactive=0,CheckOpen=0,CheckDue=0,CheckAns=0;
1.1       albertel  147:   FILE       *dfp;
                    148:   
                    149:   if( argc == 0 ) {
                    150:     usage();
                    151:     exit(0);
                    152:   }
                    153:   
                    154:   for( progname = *argv++; --argc; argv++) {
                    155:     if ( argv[0][0] == '-' && (strlen(argv[0]) > 1 ) ) {
                    156:       switch(argv[0][1]) {
                    157:       case 'S':
                    158:       case 's':
                    159: 	if( strncasecmp(argv[0],"-stu",4) == 0 ) {
                    160: 	  for(i=0;i<MAX_STUDENT_NUMBER;i++) {
                    161: 	    StuNum[i] = argv[1][i];
                    162: 	  }
                    163: 	  StuNum[i]=0;
                    164: 	  ForWhat = F_STUDENT;
                    165: 	} else if( strncasecmp(argv[0],"-sec",4) == 0 ) {
                    166: 	  tmp_num = scan_num(argv[1],&StartSec,&EndSec);
                    167: 	  if (tmp_num == 1 ) { EndSec = StartSec;  }
                    168: 	  ForWhat = F_SECTIONS;
                    169: 	} else if( strncasecmp(argv[0],"-s",2) == 0 ) {
                    170: 	  StartSet = atol(argv[1]);
                    171: 	  if( StartSet <= 0 ) StartSet = 1;
                    172: 	}
                    173: 	break;
                    174:       case 'p':
                    175:         plus = atol(argv[1]);
                    176: 	break;
                    177:       case 'e':
                    178: 	EndSet = atol(argv[1]);
                    179: 	if( EndSet > 999 ) EndSet = 99;
                    180: 	break;
                    181:       case 'c':
1.2       albertel  182: 	if (strcmp(argv[0],"-checkopen")==0) { CheckOpen=1; break; }
                    183: 	if (strcmp(argv[0],"-checkdue" )==0) { CheckDue=1;  break; }
                    184: 	if (strcmp(argv[0],"-checkans" )==0) { CheckAns=1;  break; }
1.1       albertel  185: 	strcpy(path,argv[1]);
                    186: 	if( capa_access(path, F_OK) == -1 ) {
                    187: 	  printf("No such class [%s] please specify it again:\n",path);
                    188: 	  classnameOK = 0;
                    189: 	} else {
                    190: 	  classnameOK = 1;
                    191: 	}
                    192: 	break;
                    193:       case 'i':
                    194: 	interactive = 1; 
                    195: 	break;
                    196:       case 'd':
                    197: 	strcpy(outputPath,argv[1]);
                    198: 	outputDir=1;
                    199: 	break;
                    200:       case 'u': case 'h': default:
                    201: 	usage(); 
                    202: 	exit(0);
                    203: 	break;
                    204:       }
                    205:     }
                    206:   }
                    207:   if( StartSet > EndSet ) StartSet = EndSet;
                    208:   while ( !classnameOK ) {
                    209:     puts("Enter the ABSOLUTE path of class from root (/)");
                    210:     scanf("%s", path);
                    211:     if( capa_access(path, F_OK) == -1 ) {
                    212:       printf("No such class, please specify it again:\n");
                    213:     } else {
                    214:       classnameOK = 1;
                    215:     }
                    216:   }
                    217:   
                    218:   if (!interactive) {
                    219:     if (outputDir) {
                    220:       sprintf(fullpath,"%s",outputPath);
                    221:     } else {
                    222:       sprintf(fullpath,"%s/capaID",path);
                    223:     }
                    224:     if( capa_access(fullpath, F_OK) == -1 ) {
                    225:       if ( mkdir(fullpath, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) {
                    226: 	printf("Unable to write to %s\n",fullpath);
                    227: 	printf("Please check this directory and run %s again.\n",progname);
                    228: 	return(-1);
                    229:       }
                    230:     }
                    231:   }
                    232:   
                    233:   chdir(path);
                    234:   switch(ForWhat) {
                    235:   case F_STUDENT:
                    236:     printf("\n");
                    237:     for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    238:       printf("%4d ", setIdx );
                    239:     }
                    240:     printf("\n");
1.2       albertel  241:     if (CheckOpen || CheckDue || CheckAns) {
                    242:       sprintf(fmt,"%%%ds ",plus+4);
                    243:       if ( capa_get_student(StuNum,&student_data) == 0 ) {
                    244: 	fprintf(stderr,"Unable to find student, %s.\n",StuNum);
                    245: 	exit(1);
                    246:       }
                    247:     }
1.1       albertel  248:     for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
1.2       albertel  249:       if ( CheckOpen && ( capa_check_date(CHECK_OPEN_DATE,StuNum,
                    250: 					  student_data.s_sec,setIdx) < 0)) {
                    251: 	printf(fmt,"Open");continue;
                    252:       }
                    253:       if ( CheckDue && ( capa_check_date(CHECK_DUE_DATE,StuNum,
                    254: 					  student_data.s_sec,setIdx) < 0)) {
                    255: 	printf(fmt,"Due");continue;
                    256:       }
                    257:       if ( CheckAns && ( capa_check_date(CHECK_ANS_DATE,StuNum,
                    258: 					  student_data.s_sec,setIdx) < 0)) {
                    259: 	printf(fmt,"Ans");continue;
                    260:       }
1.1       albertel  261:       if (plus) 
                    262: 	print_capaidplus(stdout,StuNum,setIdx,plus);
                    263:       else 
                    264: 	printf("%4d ", capa_PIN(StuNum, setIdx,0) );
                    265:     }
                    266:     printf("\n"); fflush(stdout);
                    267:     break;
                    268:   case F_CLASS:
                    269:     StartSec=1;EndSec=999;
                    270:   case F_SECTIONS:
1.2       albertel  271:     if (CheckOpen || CheckDue || CheckAns) {
                    272:       printf("Can only check dates in single student mode\n");
                    273:     }
1.1       albertel  274:     if ((count = capa_get_section_count( SecCntArry )) != 0 ) {
                    275:       if (count == -1 ) {
                    276: 	printf("classl file not found in %s\n",path);
                    277: 	exit (-1);
                    278:       }
                    279:       for(sectionIdx = StartSec; 
                    280: 	  (sectionIdx <= SecCntArry[0]) && (sectionIdx <= EndSec);
                    281: 	  sectionIdx++) {
                    282: 	if( SecCntArry[sectionIdx] > 0 ) {
                    283: 	  if ( interactive ) {
                    284: 	    dfp=stdout;
                    285: 	  } else {
                    286: 	    sprintf(filename,"%s/section%d.capaid",fullpath,sectionIdx);
                    287: 	    if((dfp=fopen(filename,"w"))==NULL) {
                    288: 	      printf("Unable to create %s\n",filename);
                    289: 	      exit(-2);
                    290: 	    }
                    291: 	  }
                    292: 	  
                    293: 	  numStudents = capa_get_section(&students_p, sectionIdx);
                    294: 	  printf("Section %d, %d students\n",
                    295: 		 sectionIdx, numStudents);
                    296: 	  
                    297: 	  fprintf(dfp,"Section %d   Student    NAME      NUMBER   ",sectionIdx);
                    298: 	  if (plus) {
                    299: 	    print_capaidplus_header1(dfp,StartSet,EndSet,plus);
                    300: 	  } else {
                    301: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++)   
                    302: 	      fprintf(dfp,"%3d  ",setIdx);
                    303: 	  }
                    304: 	  
                    305: 	  fprintf(dfp,"\n");
                    306: 	  fprintf(dfp,"------------------------------|----------|");
                    307: 	  if (plus) {
                    308: 	    print_capaidplus_header2(dfp,StartSet,EndSet,plus);
                    309: 	  } else {
                    310: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++) 
                    311: 	      fprintf(dfp,"----|");
                    312: 	  }
                    313: 	  fprintf(dfp,"\n");
                    314: 	  for(s_p = students_p; s_p ; s_p = s_p->s_next ) {
                    315: 	    fprintf(dfp,"%s %s  ",s_p->s_nm, s_p->s_sn);
                    316: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    317: 	      if (plus) 
                    318: 		print_capaidplus(dfp,s_p->s_sn,setIdx,plus);
                    319: 	      else 
                    320: 		fprintf(dfp,"%4d ", capa_PIN(s_p->s_sn, setIdx,0) );
                    321: 	    }
                    322: 	    fprintf(dfp,"\n");
                    323: 	  }
                    324: 	  fflush(dfp);
                    325: 	  if (!interactive) fclose(dfp); 
                    326: 	}
                    327:       }
                    328:     }
                    329:   }
                    330:   return (0);
                    331: }
                    332: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>