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"); }
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; }