So I wrote a simple program that pould take a random line for a text file, and write it, but anything within square brackets would be treated as a filename to recurse into. Basically, a simple text substitution system that used a random line from the target file.
Then adding 256 text storage things, some rewriting to output to strings rather than directly, and then other stuff...
Well, I want to share it with the forum, but it isn't good enough for a place of it's own.
So here it is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
FILE *o;
struct str
{
char *str;
int length,cur;
};
struct str* randPhrase(char*,char*[]);
void outChr(struct str *s,char c)
{
if(s->cur>=s->length-1)
{
char *t=s->str;
s->str=malloc(s->length+1024);
if(!s->str)
{
s->str=t;
return;
}
s->length+=1024;
s->str[0]=0;
if(t)
{
strcpy(s->str,t);
free(t);
}
}
s->str[s->cur]=c;
s->cur++;
s->str[s->cur]=0;
}
void outStr(struct str *s,char *c)
{
if(!c)
return;
int l=strlen(c);
if(s->cur+l>=s->length && 0)
{
l/=1024;
l+=2;
char *t=s->str;
s->str=malloc(s->length+l*1024);
if(!s->str)
return;
s->length+=l*1024;
s->str[0]=0;
if(t)
{
strcpy(s->str,t);
free(t);
}
}
strcat(s->str,c);
s->cur=strlen(s->str);
}
struct str* parseText(char *text, char *var[])
{
int i=0;
struct str *out=malloc(sizeof(struct str));
out->length=1024;
out->cur=0;
out->str=malloc(1024);
out->str[0]=0;
int len=strlen(text);
while(i<len)
{
switch(text[i])
{
case '[':
{
int p=i;
i++;
if(text[i]==':')
{
i++;
unsigned char n=text[i];
unsigned char n2=0;
if(n<='9' && n>='0')
n2+=n-'0';
else if(n<='a' && n>='f')
n2+=n-'a'+10;
else if(n<='A' && n>='F')
n2+=n-'a'+10;
i++;
n=text[i];
if(n<='9' && n>='0')
n2+=(n-'0')*16;
else if(n<='a' && n>='f')
n2+=(n-'a'+10)*16;
else if(n<='A' && n>='F')
n2+=(n-'a'+10)*16;
if(var && var[n2])
{
struct str *t=parseText(var[n2],var);
if(t)
{
if(t->str)
{
outStr(out,t->str);
free(t->str);
}
free(t);
}
i++;
break;
}
p+=3;
}
else if(text[i]=='|')
{
i++;
unsigned char n=text[i];
unsigned char n2=0;
if(n<='9' && n>='0')
n2+=n-'0';
else if(n<='a' && n>='f')
n2+=n-'a'+10;
else if(n<='A' && n>='F')
n2+=n-'a'+10;
i++;
n=text[i];
if(n<='9' && n>='0')
n2+=(n-'0')*16;
else if(n<='a' && n>='f')
n2+=(n-'a'+10)*16;
else if(n<='A' && n>='F')
n2+=(n-'a'+10)*16;
if(var && var[n2])
{
struct str *t=randPhrase(var[n2],var);
if(t)
{
if(t->str)
{
outStr(out,t->str);
free(t->str);
}
free(t);
}
i++;
break;
}
p+=3;
}
i=p;
int k=0,j=0;
i++;
while(text[i]&&text[i]!=']')
{
k++;
i++;
}
i=p;
char* file=malloc(k+1);
file[k]=0;
i++;
while(k)
{
k--;
file[j]=text[i];
i++;
j++;
}
//i++;
struct str *t=randPhrase(file,var);
if(t)
{
if(t->str)
{
outStr(out,t->str);
free(t->str);
}
free(t);
}
free(file);
break;
}
case '<':
{
i++;
unsigned char n=text[i];
unsigned char n2=0;
if(n<='9' && n>='0')
n2+=n-'0';
else if(n<='a' && n>='f')
n2+=n-'a'+10;
else if(n<='A' && n>='F')
n2+=n-'a'+10;
i++;
n=text[i];
if(n<='9' && n>='0')
n2+=(n-'0')*16;
else if(n<='a' && n>='f')
n2+=(n-'a'+10)*16;
else if(n<='A' && n>='F')
n2+=(n-'a'+10)*16;
i++;
i++;
int p=i;
int k=0,j=0;
while(text[i]&&text[i]!='>')
{
k++;
i++;
}
i=p;
char *temp=malloc(k+1);
temp[k]=0;
while(k)
{
k--;
temp[j]=text[i];
i++;
j++;
}
temp[j]=0;
struct str* t=parseText(temp,var);
if(t)
{
if(t->str)
{
if(var[n2])
free(var[n2]);
var[n2]=t->str;
}//The fact that t->str is not free()d, is not a memory leak in this context.
free(t);
}
free(temp);
//i++;
break;
}
case '\\':
{
i++;
if(i>=len)
return out;
switch(text[i])
{
case 'n':
case 'N':
outChr(out,'\n');
break;
default:
outChr(out,text[i]);
break;
}
break;
}
default:
outChr(out,text[i]);
}
i++;
}
return out;
}
struct str* parsePhrase(FILE *f, char *var[])
{
char *s=0;
int p=ftell(f);
char c=fgetc(f);
int l=0;
while(!feof(f)&&c!='\n')
{
l++;
c=fgetc(f);
}
s=malloc(l+1);
fseek(f,p,SEEK_SET);
p=0;
while(p<l)
{
s[p]=fgetc(f);
p++;
}
s[p]=0;
fclose(f);
return parseText(s,var);
}
int countPhrases(FILE *f)
{
int n=0;
while(!feof(f))
if(fgetc(f)=='\n')
n++;
return n+1;
}
struct str* printPhrase(FILE *f,int num, char *var[])
{
fseek(f,0,SEEK_SET);
while(num)
if(fgetc(f)=='\n')
num--;
return parsePhrase(f,var);
}
struct str* randPhrase(char *file, char *var[])
{
FILE *f=fopen(file,"r");
if(!f)
return 0;
int num=countPhrases(f);
struct str *t=printPhrase(f,rand()%num,var);
if((int)t==-1)
return 0;
return t;
}
int main()
{
srand(time(NULL));
o=stdout;
char **var=malloc(sizeof(char*)*256);
struct str *t;
int x;
do
{
for(x=0;x<10;x++)
{
t=randPhrase("test.txt",var);
fprintf(o,"%s\n",t->str);
free(t->str);
free(t);
}
}
while(getch()!='q');
return 0;
}
(Edit: added some libraries and changed the poor quality srand() mechanism that uses a file to save the last rand() into one that uses time(). The only error with -Wall is about getch(), but I don't know where getch() is implemented...)
This is the place for anyone else to share small programs that aren't really notably good at anything.