#include <stdio.h>
#include <ctype.h>
/*
*
[email protected]
*
* Just a quick hack to split up the mail messages into
* individual recipe files and create meaningful file names.
* 10/4/92
*/
#define SEPARATOR "=========="
main(argc,argv) int argc; char **argv;
{
FILE *fp;
int i;
if(argc < 2)
doit(stdin);
else {
++argv;
--argc;
for(i=0; i < argc; i++) {
if((fp = fopen(argv[i],"r")) == NULL) {
perror(argv[i]);
}
else {
doit(fp);
fclose(fp);
}
}
}
exit(0);
}
doit(fp) FILE *fp;
{
char buf[BUFSIZ], title[BUFSIZ];
register char *bp, *tp;
FILE *fpout;
int ok, i;
/*
* find first separator
*/
while(fgets(buf,BUFSIZ,fp))
if(!strncmp(buf,SEPARATOR,sizeof(SEPARATOR)-1))
break;
while(!feof(fp)) {
/*
* Get title
*/
if(fgets(buf,BUFSIZ,fp) == NULL)
return;
for(bp=buf,tp=title,ok=0; *bp; bp++)
if(isspace(*bp)) {
if(isspace(bp[1]) || !strncmp(&bp[1],"Servings",sizeof("Servings")-1)){
*tp = '\0';
ok = 1;
break;
}
else
*tp++ = '.';
}
else if(isupper(*bp))
*tp++ = tolower(*bp);
else switch(*bp) {
case '&':
*tp++ = '+';
break;
case '(':
case '[':
case ')':
case ']':
*tp++ = '_';
break;
case '/':
case '\\':
*tp++ = '-';
break;
case '!':
case '\'':
case '"':
case '`':
case ':':
break; /* ignore/elide */
default:
*tp++ = *bp;
}
if(!ok)
return;
ok = 1;
/*
* Now trim backwards any folderol off end
*/
for(tp = &title[strlen(title)-1]; tp >= title; tp--)
if(isalnum(*tp) || (*tp == '_'))
break;
else
*tp = '\0';
if(tp <= title) /* oops */
ok = 0;
if(ok && (access(title,0) != -1)) {
char ttmp[BUFSIZ];
for(i=2,ok=0; i < 100; i++) {
sprintf(ttmp,"%s.%d",title,i);
if(access(ttmp,0) == -1) {
strcpy(title,ttmp);
ok = 1;
break;
}
}
if(!ok)
fprintf(stderr,"Couldn't create %s\n",title);
}
if(ok && ((fpout = fopen(title,"w")) == NULL)) {
perror(title);
return;
}
/*
* Now copy the recipe out to the newly created file (or skip)
*/
do {
if(!strncmp(buf,SEPARATOR,sizeof(SEPARATOR)-1))
break;
if(ok)
fputs(buf,fpout);
} while(fgets(buf,BUFSIZ,fp));
if(ok)
fclose(fpout);
}
}