advertisement

Article:
  iPod Notes: Above and Beyond
Subject:   Here's a quick tool for your readers -
Date:   2006-12-13 09:56:39
From:   ObviousTroll
I used to have an eBook library website, back in the day, and your first article inspired me to play with the Note feature. This program is the result; it's too short for a sourceforge project, so I hope you don't mind if paste it here for your readers. It will take any text file and chunk it up into iPod notes, with previous page and next page links.


-----------------

#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;
}


1 to 1 of 1
  1. Here's a quick tool for your readers -
    2006-12-13 10:00:35  ObviousTroll [View]

1 to 1 of 1