Last active
August 29, 2015 14:01
-
-
Save lidio601/eedd592d24383c3e4d6d to your computer and use it in GitHub Desktop.
I was trying to load fixed-structure plist file by changing his text content ad adding a dictionary as root element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* I was trying to load fixed-structure plist files like this: | |
* \code{.xml} | |
* <?xml version="1.0" encoding="UTF-8"?> | |
* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
* <plist version="1.0"> | |
* <array> | |
* <dict> | |
* <key>id</key> | |
* <string>ID-0</string> | |
* <key>field-1</key> | |
* <string>value 1</string> | |
* ... | |
* </dict> | |
* ... | |
* </array> | |
* </plist> | |
* \endcode | |
* But what I need was a structure like this: | |
* \code{.xml} | |
* <?xml version="1.0" encoding="UTF-8"?> | |
* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
* <plist version="1.0"> | |
* <dict> | |
* <key>rows</key> | |
* <array> | |
* <dict> | |
* <key>id</key> | |
* <string>ID-0</string> | |
* <key>field-1</key> | |
* <string>value 1</string> | |
* ... | |
* </dict> | |
* ... | |
* </array> | |
* </dict> | |
* </plist> | |
* \endcode | |
* Due to the big size of the plist file I wrote this buffered reader for ASCII-only text files | |
* to overcome this problem. | |
*/ | |
/* | |
Setup variables | |
*/ | |
NSString* filepath = @"input.plist"; | |
NSString* outfilepath = @"output.plist"; | |
/* | |
Open the input text file | |
*/ | |
FILE* fp = fopen([filepath UTF8String], "r"); | |
if(!fp) { | |
NSLog(@"Cannot load Plist file %@",filepath); | |
return; | |
} | |
/* | |
Open the output text file | |
*/ | |
FILE* fpout = fopen([outfilepath UTF8String], "w"); | |
if(!fpout) { | |
NSLog(@"Cannot write temporary plist file %@",tempfilename); | |
return; | |
} | |
/* | |
Service variables | |
*/ | |
const int buflen = 100; | |
char buf[buflen]; | |
char* startidx; | |
char buf2[buflen+100]; | |
int j; | |
while(!feof(fp)) { | |
int rlen = fread(buf, buflen, sizeof(buf[0]), fp); | |
// NSLog(@"len %d = %s",rlen,buf); | |
if(rlen>0) { | |
startidx = strstr(buf, "<array>"); | |
if( startidx!=NULL ) { | |
char *i = buf; | |
j = 0; | |
while( i!=startidx ) { | |
buf2[j++] = *i; | |
i++; | |
} | |
buf2[j] = '\0'; | |
strcat(buf2,"<dict><key>rows</key>"); | |
strcat(buf2,startidx); | |
fwrite(buf2, strlen(buf2), sizeof(buf2[0]), fpout); | |
} | |
else { | |
startidx = strstr(buf, "</array>"); | |
if( startidx!=NULL ) { | |
char *i = buf; | |
j = 0; | |
while( i!=startidx ) { | |
buf2[j++] = *i; | |
i++; | |
} | |
buf2[j] = '\0'; | |
strcat(buf2,"</array><dict><key>rows</key>"); | |
startidx+=strlen("</array>"); | |
strcat(buf2,startidx); | |
fwrite(buf2, strlen(buf2), sizeof(buf2[0]), fpout); | |
} else { | |
fwrite(buf, buflen, sizeof(buf[0]), fpout); | |
} | |
} | |
} | |
} | |
fflush(fpout); | |
fclose(fpout); | |
fclose(fp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment