Example #1
0
static void
order_unstr(STRFILE *tbl)
{
	unsigned int i;
	char *sp;
	long pos;
	char buf[BUFSIZ];

	for (i = 0; i < tbl->str_numstr; i++) {
		fread((char *)&pos, 1, sizeof(pos), Dataf);
		fseek(Inf, ntohl(pos), SEEK_SET);
		if (i != 0)
			printf("%c\n", Delimch);
		for (;;) {
			sp = fgets(buf, sizeof(buf), Inf);
			if (sp == NULL || STR_ENDSTRING(sp, *tbl))
				break;
			else
				fputs(sp, stdout);
		}
	}
}
Example #2
0
bool fortune_get(FortuneFile* f, unsigned int num, edelib::String& ret) {
	if(num >= fortune_num_items(f))
		return false;

	// read position from .dat file
	off_t seek_pts[2];

	fseek(f->dat_file, (long)(sizeof(StrFile) + num * sizeof(seek_pts[0])), 0);
	fread(seek_pts, sizeof(seek_pts), 1, f->dat_file);
	seek_pts[0] = ntohl(seek_pts[0]);
	seek_pts[1] = ntohl(seek_pts[1]);

	// now jump to that position in string file
	fseek(f->str_file, (long)seek_pts[0], 0);

	char buff[1024];
	char* p;
	char ch;

	ret.clear();

	while(fgets((char*)buff, sizeof(buff), f->str_file) != NULL && !STR_ENDSTRING(buff, f->data)) {
		if(f->data.str_flags & STR_ROTATED) {
			for(p = buff; (ch = *p); p++) {
				if(isupper(ch))
					*p = 'A' + (ch - 'A' + 13) % 26;
				else if(islower(ch))
					*p = 'a' + (ch - 'a' + 13) % 26;
			}
		}

		ret += buff;
	}

	return true;
}
Example #3
0
/*
 * main:
 *      Drive the sucker.  There are two main modes -- either we store
 *      the seek pointers, if the table is to be sorted or randomized,
 *      or we write the pointer directly to the file, if we are to stay
 *      in file order.  If the former, we allocate and re-allocate in
 *      CHUNKSIZE blocks; if the latter, we just write each pointer,
 *      and then seek back to the beginning to write in the table.
 */
int main(int ac, char **av)
{
    register unsigned char *sp;
    register FILE *inf, *outf;
    register int32_t last_off, length, pos, *p;
    register int first, cnt;
    register char *nsp;
    register STR *fp;
    static char string[257];

    getargs(ac, av);		/* evalute arguments */
    if ((inf = fopen(Infile, "r")) == NULL)
    {
	perror(Infile);
	exit(1);
    }

    if ((outf = fopen(Outfile, "w")) == NULL)
    {
	perror(Outfile);
	exit(1);
    }
    if (!STORING_PTRS)
	(void) fseek(outf, sizeof Tbl, 0);

    /*
     * Write the strings onto the file
     */

    Tbl.str_longlen = 0;
    Tbl.str_shortlen = (unsigned int) 0xffffffff;
    Tbl.str_delim = Delimch;
    Tbl.str_version = VERSION;
    first = Oflag;
    add_offset(outf, ftell(inf));
    last_off = 0;
    do
    {
	sp = (unsigned char*)fgets(string, 256, inf);
	if (sp == NULL || STR_ENDSTRING(sp, Tbl))
	{
	    pos = ftell(inf);
	    length = pos - last_off - (sp ? strlen((const char*)sp) : 0);
	    if (!length)
		/* Here's where we go back and fix things, if the
		 * 'fortune' just read was the null string.
		 * We had to make the assignment of last_off slightly
		 * redundant to achieve this.
		 */
	    {
		if (pos - last_off == 2)
		    fix_last_offset(outf, pos);
		last_off = pos;
		continue;
	    }
	    last_off = pos;
	    add_offset(outf, pos);
	    if (Tbl.str_longlen < length)
		Tbl.str_longlen = length;
	    if (Tbl.str_shortlen > length)
		Tbl.str_shortlen = length;
	    first = Oflag;
	}
	else if (first)
	{
	    for (nsp = (char*)sp; !isalnum(*nsp); nsp++)
		continue;
	    ALLOC(Firstch, Num_pts);
	    fp = &Firstch[Num_pts - 1];
	    if (Iflag && isupper(*nsp))
		fp->first = tolower(*nsp);
	    else
		fp->first = *nsp;
	    fp->pos = Seekpts[Num_pts - 1];
	    first = FALSE;
	}
    }
    while (sp != NULL);

    /*
     * write the tables in
     */

    fclose(inf);

    if (Oflag)
	do_order();
    else if (Rflag)
	randomize();

    if (Xflag)
	Tbl.str_flags |= STR_ROTATED;

    if (!Sflag)
    {
	printf("\"%s\" created\n", Outfile);
	if (Num_pts == 1)
	    puts("There was no string");
	else
	{
	    if (Num_pts == 2)
		puts("There was 1 string");
	    else
		printf("There were %ld strings\n", Num_pts - 1);
	    printf("Longest string: %u byte%s\n", Tbl.str_longlen, 
		Tbl.str_longlen == 1 ? "" : "s");
	    printf("Shortest string: %u byte%s\n", Tbl.str_shortlen,
		Tbl.str_shortlen == 1 ? "" : "s");
	}
    }

    fseek(outf, (off_t) 0, 0);
    Tbl.str_version = htonl(Tbl.str_version);
    Tbl.str_numstr = htonl(Num_pts - 1);
    /* Look, Ma!  After using the variable three times, let's store
     * something in it!
     */
    Tbl.str_longlen = htonl(Tbl.str_longlen);
    Tbl.str_shortlen = htonl(Tbl.str_shortlen);
    Tbl.str_flags = htonl(Tbl.str_flags);
    fwrite(&Tbl.str_version,  sizeof Tbl.str_version,  1, outf);
    fwrite(&Tbl.str_numstr,   sizeof Tbl.str_numstr,   1, outf);
    fwrite(&Tbl.str_longlen,  sizeof Tbl.str_longlen,  1, outf);
    fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf);
    fwrite(&Tbl.str_flags,    sizeof Tbl.str_flags,    1, outf);
    fwrite( Tbl.stuff,        sizeof Tbl.stuff,        1, outf);
    if (STORING_PTRS)
    {
	for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
	{
	    *p = htonl(*p);
	    fwrite(p, sizeof *p, 1, outf);
	}
    }
    fclose(outf);
    exit(0);
}