#include <stdio.h>
#include <sys/param.h>
#include <string.h>
#include <unistd.h>
// Each file should be slightly less than the maximum possible.
#define FILE_SIZE 4096
#define SLACK_SIZE 256
#define BUFLEN 32
char title[BUFLEN] = "Converted Text";
char prefix[BUFLEN] = "N";
int main(int argc, char **argv)
{
char c;
int hungry_mode = 1;
int gobble = 0;
int count = 0;
int page = 0;
char filename[MAXPATHLEN];
FILE *in = stdin;
FILE *f = NULL;
while ((c=getopt(argc,argv,"i:t:p:")) > -1) {
switch (c) {
case 't':
strncpy(title,optarg,BUFLEN);
break;
case 'p':
strncpy(prefix,optarg,BUFLEN);
break;
case 'i':
in = fopen(optarg,"r");
if (!in) {
perror("Failed to open file for writing.\n");
return -1;
}
break;
case '?':
default:
fprintf(stderr,"Usage: ipodconvert [-i source] [-t title] [-p prefix] \n");
return -1;
break;
}
}
c='#'; // bogus non-whitespace initial value.
do {
if (f) {
fclose(f);
}
sprintf(filename,"%s%03d",prefix,page);
f = fopen(filename,"w");
if (!f) {
perror("Failed to open file for writing.\n");
return -1;
}
fprintf(f,"<TITLE>%s %03d</TITLE>\n",title, page);
if (page > 0) {
fprintf(f,"Back To Page %03d (\"%s%03d\") \n",
prefix,page-1,page-1);
}
/*
* When we hit the size limit, we want to continue till
* the end of the current word.
*/
count = 0;
while (((count < (FILE_SIZE-SLACK_SIZE)) ||
(!isspace(c) && (count < FILE_SIZE))) &&
((c=fgetc(in))!=EOF)) {
if (c=='\r') {
//throw it on the floor.
} else if (c=='\n') {
// If this is our first EOL, output a space.
if (!gobble) {
gobble = 1;
fputc(' ',f);
count++;
} else {
// consume the EOL.
gobble++;
}
} else {
if (gobble>1) {
// Emit a double EOL to create a blank line.
fputc('\n',f);
fputc('\n',f);
count+=2;
}
fputc(c,f);
gobble = 0;
count++;
}
}
if (c != EOF) {
fprintf(f,"Go to Page %03d (\"%s%03d\") \n",
prefix,page+1,page+1);
}
page++;
} while (c != EOF);
return 0;
}
In any case, paste the source into a file called "ipodconvert.c" then, in Terminal, type
gcc -o ipodconvert ipodconvert.c
To use it, type ipodconvert -i text file name -t title of document -p file name for notes