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

1.1       albertel    1: 
                      2: /* ========================================================================== */
                      3: /* ========================================================================== */
                      4: /*            allcapaid.c    created by Isaac Tsai                            */
                      5: /*                                1998, 1999 copyrighted by Isaac Tsai        */
                      6: /**************************************************************************** */
                      7: #ifdef NeXT
                      8: #include <stdlib.h>
                      9: #include <sys/types.h>
                     10: #include <sys/stat.h>  
                     11: #else
                     12: #include <malloc.h>
                     13: double atof();
                     14: #endif
                     15: 
                     16: #include <stdio.h>
                     17: #include <ctype.h>
                     18: #include <sys/types.h>
                     19: #include <sys/stat.h>  
                     20:  
                     21: #include <signal.h>
                     22: #include <time.h>
                     23: #include <math.h>
                     24: #include <string.h>
                     25: #ifdef NeXT
                     26: #include <bsd/curses.h>
                     27: #else
                     28: #include <curses.h>
                     29: #endif
                     30: #define   YES    1
                     31: 
                     32: #include "capaCommon.h"
                     33: #include "ranlib.h"
                     34: 
                     35: char  *progname;
                     36: 
                     37: /* filter out the number to be [1:999] */
                     38: int  scan_num(char *num_str,int *first, int *second) {
                     39:   char  tmp_str[SMALL_LINE_BUFFER], *ch;
                     40:   int   ii=0, a_num, b_num, result=0;
                     41:   
                     42:   ch = num_str;
                     43:   tmp_str[ii] = 0;
                     44:   while( isspace(*ch) ) ch++;
                     45:   while(isdigit(*ch)) { tmp_str[ii++] = *ch; ch++; }
                     46:   tmp_str[ii] = 0;
                     47:   sscanf(tmp_str,"%d",&a_num);
                     48:   if( a_num < 0 || a_num > 999 ) a_num = 1;
                     49:   *first = a_num;
                     50:   result = 1;
                     51:   while( isspace(*ch) ) ch++;
                     52:   if( *ch == ':' ) {
                     53:     ch++;
                     54:     while( isspace(*ch) ) ch++;
                     55:     ii=0; tmp_str[ii] = 0;
                     56:     while( isdigit(*ch) ) { tmp_str[ii++] = *ch; ch++; }
                     57:     tmp_str[ii] = 0;
                     58:     sscanf(tmp_str,"%d",&b_num);
                     59:     if( b_num < 0 || b_num > 999 ) b_num = 1;
                     60:     if( a_num > b_num )           b_num = a_num;
                     61:     *second = b_num;
                     62:     result = 2;
                     63:   }
                     64:   return (result);
                     65: }
                     66: 
                     67: void print_capaidplus (FILE *out,char* stunum,int set,int plus) 
                     68: {
                     69:   char fmt[SMALL_LINE_BUFFER];
                     70:   sprintf(fmt,"%%%dc",4+plus);
                     71:   fprintf(out,"%s ", capa_id_plus(stunum, set, plus));
                     72: }
                     73: 
                     74: void print_capaidplus_header1(FILE *dfp,int StartSet,int EndSet,int plus)
                     75: {
                     76:   int pluscnt,setIdx;
                     77:   for(setIdx = StartSet; setIdx <= EndSet; setIdx++)   
                     78:     {
                     79:       fprintf(dfp,"%3d",setIdx);
                     80:       for(pluscnt=0; pluscnt < (plus+2); pluscnt++)
                     81: 	fprintf(dfp," ");
                     82:     }
                     83: }
                     84: 
                     85: void print_capaidplus_header2(FILE *dfp,int StartSet,int EndSet,int plus)
                     86: {
                     87:   int setIdx,pluscnt;
                     88:   for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                     89:     for(pluscnt=0; pluscnt < (plus+4); pluscnt++)
                     90:       fprintf(dfp,"-");
                     91:     fprintf(dfp,"|");
                     92:   }
                     93: }
                     94: 
                     95: void usage()
                     96: {
                     97:   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);
                     98:   printf("       start-set : default 1\n");
                     99:   printf("       end-set   : default 10\n");
                    100:   printf("       student-number  : no default\n");
                    101:   printf("       class-directory : no default\n");
                    102:   printf("       output-directory: class-directory/capaID\n");
                    103:   printf("       -Sec 3          : for section 3\n");
                    104:   printf("       -Sec 3:7        : from section 3 to section 7\n");
                    105:   printf("       -i              : don't create files, print to the screen\n");
                    106:   printf("       -p 2            : output 6 charatcer capaidplus instead of capaid\n");
                    107:   printf("       -h              : prints this message\n");
                    108:   printf("       CAPA version %s, %s\n",CAPA_VER,COMPILE_DATE);
                    109: }
                    110: #define  F_CLASS       1
                    111: #define  F_SECTIONS    2
                    112: #define  F_STUDENT     3
                    113: int main (int argc, char  **argv) 
                    114: {
                    115:   T_student  *students_p,*s_p;
                    116:   int         i, setIdx, numStudents, count, tmp_num, StartSec=1,EndSec=999;
                    117:   int         StartSet = 1, EndSet = 10, classnameOK=0,outputDir=0,plus=0;
                    118:   int         SecCntArry[MAX_SECTION_COUNT], sectionIdx;   
                    119:   char        filename[MAX_BUFFER_SIZE], path[MAX_BUFFER_SIZE], 
                    120:     outputPath[MAX_BUFFER_SIZE], fullpath[MAX_BUFFER_SIZE];
                    121:   char        StuNum[MAX_STUDENT_NUMBER+1];
                    122:   int         ForWhat=F_CLASS, interactive=0;
                    123:   FILE       *dfp;
                    124:   
                    125:   if( argc == 0 ) {
                    126:     usage();
                    127:     exit(0);
                    128:   }
                    129:   
                    130:   for( progname = *argv++; --argc; argv++) {
                    131:     if ( argv[0][0] == '-' && (strlen(argv[0]) > 1 ) ) {
                    132:       switch(argv[0][1]) {
                    133:       case 'S':
                    134:       case 's':
                    135: 	if( strncasecmp(argv[0],"-stu",4) == 0 ) {
                    136: 	  for(i=0;i<MAX_STUDENT_NUMBER;i++) {
                    137: 	    StuNum[i] = argv[1][i];
                    138: 	  }
                    139: 	  StuNum[i]=0;
                    140: 	  ForWhat = F_STUDENT;
                    141: 	} else if( strncasecmp(argv[0],"-sec",4) == 0 ) {
                    142: 	  tmp_num = scan_num(argv[1],&StartSec,&EndSec);
                    143: 	  if (tmp_num == 1 ) { EndSec = StartSec;  }
                    144: 	  ForWhat = F_SECTIONS;
                    145: 	} else if( strncasecmp(argv[0],"-s",2) == 0 ) {
                    146: 	  StartSet = atol(argv[1]);
                    147: 	  if( StartSet <= 0 ) StartSet = 1;
                    148: 	}
                    149: 	break;
                    150:       case 'p':
                    151:         plus = atol(argv[1]);
                    152: 	break;
                    153:       case 'e':
                    154: 	EndSet = atol(argv[1]);
                    155: 	if( EndSet > 999 ) EndSet = 99;
                    156: 	break;
                    157:       case 'c':
                    158: 	strcpy(path,argv[1]);
                    159: 	if( capa_access(path, F_OK) == -1 ) {
                    160: 	  printf("No such class [%s] please specify it again:\n",path);
                    161: 	  classnameOK = 0;
                    162: 	} else {
                    163: 	  classnameOK = 1;
                    164: 	}
                    165: 	break;
                    166:       case 'i':
                    167: 	interactive = 1; 
                    168: 	break;
                    169:       case 'd':
                    170: 	strcpy(outputPath,argv[1]);
                    171: 	outputDir=1;
                    172: 	break;
                    173:       case 'u': case 'h': default:
                    174: 	usage(); 
                    175: 	exit(0);
                    176: 	break;
                    177:       }
                    178:     }
                    179:   }
                    180:   if( StartSet > EndSet ) StartSet = EndSet;
                    181:   while ( !classnameOK ) {
                    182:     puts("Enter the ABSOLUTE path of class from root (/)");
                    183:     scanf("%s", path);
                    184:     if( capa_access(path, F_OK) == -1 ) {
                    185:       printf("No such class, please specify it again:\n");
                    186:     } else {
                    187:       classnameOK = 1;
                    188:     }
                    189:   }
                    190:   
                    191:   if (!interactive) {
                    192:     if (outputDir) {
                    193:       sprintf(fullpath,"%s",outputPath);
                    194:     } else {
                    195:       sprintf(fullpath,"%s/capaID",path);
                    196:     }
                    197:     if( capa_access(fullpath, F_OK) == -1 ) {
                    198:       if ( mkdir(fullpath, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) {
                    199: 	printf("Unable to write to %s\n",fullpath);
                    200: 	printf("Please check this directory and run %s again.\n",progname);
                    201: 	return(-1);
                    202:       }
                    203:     }
                    204:   }
                    205:   
                    206:   chdir(path);
                    207:   switch(ForWhat) {
                    208:   case F_STUDENT:
                    209:     printf("\n");
                    210:     for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    211:       printf("%4d ", setIdx );
                    212:     }
                    213:     printf("\n");
                    214:     for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    215:       if (plus) 
                    216: 	print_capaidplus(stdout,StuNum,setIdx,plus);
                    217:       else 
                    218: 	printf("%4d ", capa_PIN(StuNum, setIdx,0) );
                    219:     }
                    220:     printf("\n"); fflush(stdout);
                    221:     break;
                    222:   case F_CLASS:
                    223:     StartSec=1;EndSec=999;
                    224:   case F_SECTIONS:
                    225:     if ((count = capa_get_section_count( SecCntArry )) != 0 ) {
                    226:       if (count == -1 ) {
                    227: 	printf("classl file not found in %s\n",path);
                    228: 	exit (-1);
                    229:       }
                    230:       for(sectionIdx = StartSec; 
                    231: 	  (sectionIdx <= SecCntArry[0]) && (sectionIdx <= EndSec);
                    232: 	  sectionIdx++) {
                    233: 	if( SecCntArry[sectionIdx] > 0 ) {
                    234: 	  if ( interactive ) {
                    235: 	    dfp=stdout;
                    236: 	  } else {
                    237: 	    sprintf(filename,"%s/section%d.capaid",fullpath,sectionIdx);
                    238: 	    if((dfp=fopen(filename,"w"))==NULL) {
                    239: 	      printf("Unable to create %s\n",filename);
                    240: 	      exit(-2);
                    241: 	    }
                    242: 	  }
                    243: 	  
                    244: 	  numStudents = capa_get_section(&students_p, sectionIdx);
                    245: 	  printf("Section %d, %d students\n",
                    246: 		 sectionIdx, numStudents);
                    247: 	  
                    248: 	  fprintf(dfp,"Section %d   Student    NAME      NUMBER   ",sectionIdx);
                    249: 	  if (plus) {
                    250: 	    print_capaidplus_header1(dfp,StartSet,EndSet,plus);
                    251: 	  } else {
                    252: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++)   
                    253: 	      fprintf(dfp,"%3d  ",setIdx);
                    254: 	  }
                    255: 	  
                    256: 	  fprintf(dfp,"\n");
                    257: 	  fprintf(dfp,"------------------------------|----------|");
                    258: 	  if (plus) {
                    259: 	    print_capaidplus_header2(dfp,StartSet,EndSet,plus);
                    260: 	  } else {
                    261: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++) 
                    262: 	      fprintf(dfp,"----|");
                    263: 	  }
                    264: 	  fprintf(dfp,"\n");
                    265: 	  for(s_p = students_p; s_p ; s_p = s_p->s_next ) {
                    266: 	    fprintf(dfp,"%s %s  ",s_p->s_nm, s_p->s_sn);
                    267: 	    for(setIdx = StartSet; setIdx <= EndSet; setIdx++) {
                    268: 	      if (plus) 
                    269: 		print_capaidplus(dfp,s_p->s_sn,setIdx,plus);
                    270: 	      else 
                    271: 		fprintf(dfp,"%4d ", capa_PIN(s_p->s_sn, setIdx,0) );
                    272: 	    }
                    273: 	    fprintf(dfp,"\n");
                    274: 	  }
                    275: 	  fflush(dfp);
                    276: 	  if (!interactive) fclose(dfp); 
                    277: 	}
                    278:       }
                    279:     }
                    280:   }
                    281:   return (0);
                    282: }
                    283: 

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