#include #include #include #include #include int validation(char *arg1,char* arg2) { xmlDocPtr doc; xmlSchemaPtr schema = NULL; xmlSchemaParserCtxtPtr ctxt; char *XMLFileName = arg1; // write your xml file here char *XSDFileName = arg2; // write your xsd file here xmlLineNumbersDefault(1); //set line numbers, 0> no substitution, 1>substitution ctxt = xmlSchemaNewParserCtxt(XSDFileName); //create an xml schemas parse context schema = xmlSchemaParse(ctxt); //parse a schema definition resource and build an internal XML schema xmlSchemaFreeParserCtxt(ctxt); //free the resources associated to the schema parser context doc = xmlReadFile(XMLFileName, NULL, 0); //parse an XML file if (doc == NULL) { fprintf(stderr, "Could not parse %s\n", XMLFileName); } else { xmlSchemaValidCtxtPtr ctxt; //structure xmlSchemaValidCtxt, not public by API int ret; ctxt = xmlSchemaNewValidCtxt(schema); //create an xml schemas validation context ret = xmlSchemaValidateDoc(ctxt, doc); //validate a document tree in memory if (ret == 0) //validated { printf("%s validates\n", XMLFileName); } else if (ret > 0) //positive error code number { printf("%s fails to validate\n", XMLFileName); } else //internal or API error { printf("%s validation generated an internal error\n", XMLFileName); } xmlSchemaFreeValidCtxt(ctxt); //free the resources associated to the schema validation context xmlFreeDoc(doc); } // free the resource if(schema != NULL) xmlSchemaFree(schema); //deallocate a schema structure xmlSchemaCleanupTypes(); //cleanup the default xml schemas types library xmlCleanupParser(); //cleans memory allocated by the library itself xmlMemoryDump(); //memory dump return(0); } typedef struct{ //Creating Struct char name[20]; char surname[30]; char gender[15]; char occupancy[45]; char level_of_education[35]; char email[50]; char bank_account_number[20]; char IBAN[30]; char account_type[20]; char currency_unit[10]; int total_balance_available; char available_for_loan[5]; }Person; void assignStruct(Person *person,char* str){//Assigning the struct int array[13]={0}; int size=strlen(str); int idx=1; for (size_t i = 1; i <= size; i++) { if(str[i]==',' && str[i-1]==',') { array[idx]=1; idx++; } else if(str[i]==',' && str[i-1]!=','){ array[idx]=0; idx++; } } // Assigning the struct variable one by one char* token=strtok(str,","); printf("%d",array[1]); if(array[1]==0){ strcpy(person->name,token); token=strtok(NULL,","); } else{ strcpy(person->name," "); } if(array[2]==0){ strcat(token,""); strcpy(person->surname,token); token=strtok(NULL,","); } else{ strcpy(person->surname," "); } if(array[3]==0){ strcat(token,""); strcpy(person->gender,token); token=strtok(NULL,","); } else{ strcpy(person->gender," "); } if(array[4]==0){ strcat(token,""); strcpy(person->occupancy,token); token=strtok(NULL,","); } else{ strcpy(person->occupancy," "); } if(array[5]==0){ strcat(token,""); strcpy(person->level_of_education,token); token=strtok(NULL,","); } else{ strcpy(person->level_of_education," "); } if(array[6]==0){ strcat(token,""); strcpy(person->email,token); token=strtok(NULL,","); } else{ strcpy(person->email," "); } if(array[7]==0){ strcat(token,""); strcpy(person->bank_account_number,token); token=strtok(NULL,","); } else{ strcpy(person->bank_account_number," "); } if(array[8]==0){ strcat(token,""); strcpy(person->IBAN,token); token=strtok(NULL,","); } else{ strcpy(person->IBAN," "); } if(array[9]==0){ strcat(token,""); strcpy(person->account_type,token); token=strtok(NULL,","); } else{ strcpy(person->account_type," "); } if(array[10]==0){ strcat(token,""); strcpy(person->currency_unit,token); token=strtok(NULL,","); } else{ strcpy(person->currency_unit," "); } if(array[11]==0){ strcat(token,""); person->total_balance_available=atoi(token); token=strtok(NULL,","); } else{ person->total_balance_available=0; } if(array[12]==0){ strcat(token,""); token[strcspn(token,"\n")]=0; strcpy(person->available_for_loan,token); } else{ strcpy(person->available_for_loan," "); } } int swap_Endians(int value) // Big endian <-> little endian { // This var holds the leftmost 8 // bits of the output. int leftmost_byte; // This holds the left middle // 8 bits of the output int left_middle_byte; // This holds the right middle // 8 bits of the output int right_middle_byte; // This holds the rightmost // 8 bits of the output int rightmost_byte; // To store the result // after conversion int result; // Get the rightmost 8 bits of the number // by anding it 0x000000FF. since the last // 8 bits are all ones, the result will be the // rightmost 8 bits of the number. this will // be converted into the leftmost 8 bits for the // output (swapping) leftmost_byte = (value & 0x000000FF) >> 0; // Similarly, get the right middle and left // middle 8 bits which will become // the left_middle bits in the output left_middle_byte = (value & 0x0000FF00) >> 8; right_middle_byte = (value & 0x00FF0000) >> 16; // Get the leftmost 8 bits which will be the // rightmost 8 bits of the output rightmost_byte = (value & 0xFF000000) >> 24; // Left shift the 8 bits by 24 // so that it is shifted to the // leftmost end leftmost_byte <<= 24; // Similarly, left shift by 16 // so that it is in the left_middle // position. i.e, it starts at the // 9th bit from the left and ends at the // 16th bit from the left left_middle_byte <<= 16; right_middle_byte <<= 8; // The rightmost bit stays as it is // as it is in the correct position rightmost_byte <<= 0; // Result is the concatenation of all these values. result = (leftmost_byte | left_middle_byte | right_middle_byte | rightmost_byte); return result; } int convertCSVtoBinary(char* arg1,char* arg2) { FILE* csvFile; FILE* binFile; binFile=fopen(arg2,"wb"); //opening the binary file csvFile=fopen(arg1,"r"); //opening the csv file if(!csvFile){ printf("couldn't open the file\n"); return 1; } else{ int max_char=1000; //Max char in one row int count=50; //number of person Person person[count]; char row[max_char]; int i=0; fgets(row,max_char,csvFile); while(!feof(csvFile)){ fgets(row,max_char,csvFile); // reading the csv file line by line assignStruct(&person[i],row); //assigning the struct with row i++; } fseek(binFile,0,SEEK_SET); fwrite(person,sizeof(Person),count,binFile); //writing binary to records.dat //Closing the file that we used fclose(binFile); fclose(csvFile); } return 0; } int convertBinarytoXML(char* arg1,char* arg2){ int max_char=1000; char row[max_char]; xmlDocPtr doc = NULL; xmlNodePtr root_node = NULL,mainNode=NULL,xmlrow=NULL,customer_info=NULL,bank_account_info=NULL, node = NULL, node1 = NULL; /* node pointers */ char buff[256]; FILE* read_bin; read_bin=fopen(arg1,"rb"); if(!read_bin){ printf("couldn't open the file\n"); return 0; } else{ doc = xmlNewDoc(BAD_CAST "1.0"); root_node = xmlNewNode(NULL, BAD_CAST "records"); char row[1000]; fseek(read_bin,0,SEEK_SET); for (int i = 1; i < 51; i++) { Person person; fread(&person,sizeof(Person),1,read_bin); // reading binary file xmlDocSetRootElement(doc, root_node); //setting root node xmlrow = xmlNewNode(NULL, BAD_CAST "row"); //setting row node char id[6]; snprintf(id,sizeof(id),"%d",i); const xmlChar* strId = (const xmlChar*) id; // casting id to const xmlChar xmlNewProp(xmlrow, BAD_CAST "id",BAD_CAST strId); customer_info = xmlNewNode(NULL, BAD_CAST "customer_info"); bank_account_info = xmlNewNode(NULL, BAD_CAST "bank_account_info"); // Adding child to main root xmlAddChild(root_node,xmlrow); xmlAddChild(xmlrow,customer_info); xmlAddChild(xmlrow,bank_account_info); xmlNewChild(customer_info, NULL, BAD_CAST "name", person.name); xmlNewChild(customer_info, NULL, BAD_CAST "surname", person.surname); xmlNewChild(customer_info, NULL, BAD_CAST "gender", person.gender); xmlNewChild(customer_info, NULL, BAD_CAST "occupancy", person.occupancy); xmlNewChild(customer_info, NULL, BAD_CAST "level_of_education", person.level_of_education); xmlNewChild(customer_info, NULL, BAD_CAST "email", person.email); xmlNewChild(bank_account_info, NULL, BAD_CAST "bank_account_number", person.bank_account_number); xmlNewChild(bank_account_info, NULL, BAD_CAST "IBAN", person.IBAN); xmlNewChild(bank_account_info, NULL, BAD_CAST "account_type", person.account_type); //Little endian -> Big endian -> cast big endian to constant string int endian=swap_Endians(person.total_balance_available); char endian_str[100]; snprintf(endian_str,sizeof(endian_str),"%d",endian); const xmlChar* xmlEndian_str = (const xmlChar*) endian_str; //casting integer to constant string char balance_available[100]; snprintf(balance_available,sizeof(balance_available),"%d",person.total_balance_available); const xmlChar* xmlBalance_available = (const xmlChar*) balance_available; xmlNodePtr total_balance_available_node=xmlNewChild(bank_account_info, NULL, BAD_CAST "total_balance_available", xmlBalance_available); //adding the properties that big endian and currency unit to total_balance_available_node xmlNewProp(total_balance_available_node, BAD_CAST "bigEnd_Version",BAD_CAST xmlEndian_str); xmlNewProp(total_balance_available_node, BAD_CAST "currency_unit",BAD_CAST person.currency_unit); xmlNewChild(bank_account_info, NULL, BAD_CAST "available_for_loan", person.available_for_loan); } } xmlSaveFormatFileEnc(arg2,doc,"UTF-8",1); xmlFreeDoc(doc); xmlCleanupParser(); fclose(read_bin); } int main(int argc, char *argv[]) { if(argc!=4){ printf("Wrong format: Usage is MyConverter "); return(0); } char* arg1=argv[1]; char* arg2=argv[2]; char* type=argv[3]; if(strcmp(type,"1")==0){ convertCSVtoBinary(arg1,arg2); } else if(strcmp(type,"2")==0){ convertBinarytoXML(arg1,arg2); } else if(strcmp(type,"3")==0){ validation(arg1,arg2); } return(0); }