This utility will split open an MPD so that its subfiles that can be saved separately. http://pastebin.com/eQC7fSPx
Code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#define NOFILE 0
#define INFILE 1
const char* searchText[2] = {"0 NOFILE", "0 FILE"};
#define INFILE_CT 6
typedef struct subfile{
char* filename;
char* data;
} subfile_t;
typedef struct lineref{
char* begin;
int type;
} lineref_t;
int lineref_comp(const void * elem1, const void * elem2)
{
return ((lineref_t*)elem1)->begin - ((lineref_t*)elem2)->begin;
}
lineref_t* lineref_insert(lineref_t* lrArray, lineref_t newLR, int *count, int *max)
{
if(1+(*count) >= (*max))
{
(*max) = (int)(1+1.4*(*max)); // increase size more or less geometrically by ~sqrt(2)
lrArray = (lineref_t*)realloc(lrArray,(*max)*sizeof(lineref_t));
}
lrArray[(*count)++] = newLR;
return lrArray;
}
int main(int argc, char* argv[])
{
if(argc <= 1) exit(1);
FILE *fp;
char* filename = argv[1];
char* data = NULL;
int state;
lineref_t* lrArray = NULL;
lineref_t curLR;
subfile_t* subFiles = NULL;
subfile_t curSubFile;
int count, max, i, j;
count = max = 0;
printf("reading %s\n",filename);
if((fp = fopen(filename,"r")) == NULL){
printf("Cannot open file.\n");
exit(1);
}
if (fseek(fp, 0L, SEEK_END) == 0) {
long bufsize = ftell(fp);
if (bufsize == -1) { printf("Error\n"); exit(1); }
data = (char*)malloc(sizeof(char) * (bufsize + 1));
if (fseek(fp, 0L, SEEK_SET) != 0) { printf("Error\n"); exit(1);}
size_t newLen = fread(data, sizeof(char), bufsize, fp);
if (newLen == 0) {
fputs("Error reading file", stderr);
} else {
data[++newLen] = '\0'; /* Just to be safe. */
}
}
fclose(fp);
for(state = NOFILE; state <= INFILE; state++){
char * curPos = data;
while((curPos = strstr(curPos, searchText[state])) != NULL){
char prev = (curPos == data)? '\0' : *(curPos-1);
if((curPos == data) || prev == '\r' || prev == '\n'){
//At beginning of line!
curLR.begin = curPos;
curLR.type = state;
lrArray = lineref_insert(lrArray, curLR, &count, &max);
}
curPos++;
}
}
qsort(lrArray, count, sizeof(lineref_t), lineref_comp);
subFiles = (subfile_t*)realloc(subFiles,sizeof(subfile_t)*count);
for(j = i = 0; i < count; i++){
if(lrArray[i].type){
lrArray[i].begin+=INFILE_CT;
while(lrArray[i].begin[0] <= ' ') lrArray[i].begin++;
curSubFile.filename = lrArray[i].begin;
while(lrArray[i].begin[0] != '\0' && lrArray[i].begin[0] != '\n' && lrArray[i].begin[0] != '\r') lrArray[i].begin++;
while(lrArray[i].begin[0] == '\n' || lrArray[i].begin[0] == '\r') {
lrArray[i].begin[0] = '\0';
lrArray[i].begin++;
}
curSubFile.data = lrArray[i].begin;
subFiles[j] = curSubFile;
j++;
}
if(i+1<count){
lrArray[i+1].begin[0] = '\0';
}
}
subFiles = (subfile_t*)realloc(subFiles,sizeof(subfile_t)*j);
for(i = 0; i < j; i++){
printf("File: %s\nData:\n%s\n",subFiles[i].filename,subFiles[i].data);
}
free(data);
return 0;
}