VcStatus readVcFile( FILE *const vcf, VcFile *const filep ){ //Initialize VcFile filep->ncards = 0; filep->cardp = NULL; //other variable declarations VcStatus returnStatus = initializeStatus(); //Initalize VcStatus VcStatus crrtStatus; //loop to read in vcards while(!feof(vcf)){ Vcard * crrtCard; filep->cardp = realloc(filep->cardp, sizeof(Vcard)*(filep->ncards+1)); crrtStatus = readVcard(vcf, &crrtCard); if(crrtStatus.code != OK){ freeVcFile(filep); return crrtStatus; } else if(crrtCard == NULL){ break; } returnStatus.lineto = linecount; filep->cardp[filep->ncards] = crrtCard; filep->ncards++; }//end of loop filep->cardp = realloc(filep->cardp, sizeof(Vcard)*(filep->ncards)); returnStatus.code = OK; return returnStatus; } //end of readVcFile
PyObject *Vcf_readFile( PyObject *self, PyObject *args ){ FILE *file; char *filename; VcStatus status; if(!PyArg_ParseTuple( args, "s", &filename ) ){ return NULL; } else{ cardNum = 0; file = fopen(filename,"r"); if(file == NULL){ char *error = strerror(errno); return Py_BuildValue("s", error); } status = readVcFile(file,&vcfile); if(status.code != OK){ freeVcFile(&vcfile); return Py_BuildValue("iii",status.code,status.linefrom,status.lineto); } fclose(file); return Py_BuildValue("s","OK"); } }
PyObject *Vcf_freeFile( PyObject *self, PyObject *args ){ freeVcFile(&gfile); //gfile = NULL; return PyLong_FromLong(0); }//end of wrapper function for freeVcFile
PyObject *Vcf_writeFile( PyObject *self, PyObject *args ){ char *filename; PyObject *cards; if(!(PyArg_ParseTuple( args, "sO", &filename, &cards)) ){ return NULL; } VcFile saveFile; FILE *writeFile; /*Grab the number of cards and allocate that many Vcard pointers*/ saveFile.ncards = PyList_Size(cards); saveFile.cardp = malloc(sizeof(Vcard*) * saveFile.ncards); assert(saveFile.cardp != NULL); /*Start loading up cards*/ int i,j, numProp; PyObject *cardListObj, *cardObj; for(i = 0; i < saveFile.ncards;i++){ /*Grab the number of props in the card to allocate the Vcard and the VLA*/ cardListObj = PyList_GetItem(cards,i); numProp = PyList_Size(cardListObj); saveFile.cardp[i] = malloc(sizeof(Vcard) + (sizeof(VcProp) * numProp)); assert(saveFile.cardp[i] != NULL); saveFile.cardp[i]->nprops = numProp; /*Parse the tuples in the list*/ for(j = 0; j < numProp; j++){ cardObj = PyList_GetItem(cardListObj,j); saveFile.cardp[i]->prop[j].partype = NULL; saveFile.cardp[i]->prop[j].parval= NULL; saveFile.cardp[i]->prop[j].value = NULL; saveFile.cardp[i]->prop[j].hook = NULL; VcPname tempName; char *tempVal; char *tempType; char *tempValue; PyArg_ParseTuple(cardObj,"izzz", &tempName, &tempType, &tempVal, &tempValue); saveFile.cardp[i]->prop[j].name = tempName; if(tempType != NULL){ saveFile.cardp[i]->prop[j].partype = malloc(sizeof(char) * strlen(tempType) + 1); assert(saveFile.cardp[i]->prop[j].partype); strcpy(saveFile.cardp[i]->prop[j].partype,tempType); } if(tempVal != NULL){ saveFile.cardp[i]->prop[j].parval = malloc(sizeof(char) * strlen(tempVal) + 1); assert(saveFile.cardp[i]->prop[j].parval); strcpy(saveFile.cardp[i]->prop[j].parval,tempVal); } if(tempValue != NULL){ saveFile.cardp[i]->prop[j].value = malloc(sizeof(char) * strlen(tempValue) + 1); assert(saveFile.cardp[i]->prop[j].value); strcpy(saveFile.cardp[i]->prop[j].value,tempValue); } } } /*Try to open file for writing*/ writeFile = fopen(filename,"w+"); if(filename == NULL){ return Py_BuildValue("s", strerror(errno)); } /*Try to write to the file*/ VcStatus status = writeVcFile(writeFile, &saveFile); fclose(writeFile); if(status.code != OK){ freeVcFile(&saveFile); return Py_BuildValue("iii", status.code,status.linefrom,status.lineto); } freeVcFile(&saveFile); return Py_BuildValue("s","OK"); }
PyObject *Vcf_freeFile( PyObject *self, PyObject *args ){ freeVcFile(&vcfile); }
int main(int argc, char**argv){ if (argc == 1){ fprintf(stderr,"You must enter atleast 1 command.\n"); return EXIT_FAILURE; } /*Read in file from STDIN*/ int write = 0; VcStatus writeStatus, readStatus; VcFile input; readStatus = readVcFile(stdin,&input); if (readStatus.code != OK){ fprintf(stderr,"Read Vcfile failed with error code %d lines %d to %d.\n",readStatus.code,readStatus.linefrom,readStatus.lineto); return EXIT_FAILURE; } /*Run the sepcified command*/ if (argc > 1){ if (strcmpML("-info",argv[1]) == 0){ if (argc > 2) return EXIT_FAILURE; vcfInfo(stdout,&input); } else if (strcmpML("-sort",argv[1]) == 0){ if (argc > 2) return EXIT_FAILURE; vcfSort(&input); writeStatus = writeVcFile(stdout,&input); write = 1; } else if (strcmpML("-canon",argv[1]) == 0){ if (argc > 2) return EXIT_FAILURE; vcfCanon(&input); writeStatus = writeVcFile(stdout,&input); write = 1; } else if (strcmpML("-select",argv[1]) == 0){ if (argc < 3){ fprintf(stderr,"ERROR: You must enter which cards to select.\n"); } else{ char * which = strdup(argv[2]); assert(which); int check = scanWhich(which); if (check == -1){ fprintf(stderr,"ERROR: Invalid format for selection."); } else{ vcfSelect(&input,which); writeStatus = writeVcFile(stdout,&input); write = 1; } free(which); } } else{ fprintf(stderr,"ERROR: Invalid options.\n"); freeVcFile(&input); return EXIT_FAILURE; } } /*Print the error*/ if ((write == 1) && writeStatus.code == IOERR){ fprintf(stderr,"IOERR on linefrom: %d. to lineto: %d.\n",writeStatus.linefrom,writeStatus.lineto); } /*Free and return*/ freeVcFile(&input); return EXIT_SUCCESS; }