| iMatix home page | << | < | > | >> |
SFL Version 2.11 |
#include "sflfort.h" int fortune_build ( const char *infile, /* Name of file to compress */ const char *outfile, /* Output file, created */ Bool compress) /* Compress yes/no */
Builds an indexed fortune file from a formatted text file. The text file contains paragraphs separated by lines containing '%%'. If okay, returns 0, else returns -1, see errno for the cause.
{ char line [LINE_MAX + 1]; /* Line from input file */ int block_nbr; long offset; inbuf = mem_alloc (BLOCK_SIZE); outbuf = mem_alloc (BLOCK_SIZE); sizes = mem_alloc (MAX_BLOCKS * sizeof (int)); input = file open (infile, 'r'); scratch = ftmp open (NULL); if (!inbuf || !outbuf || !sizes || !input || !scratch) { mem_free (inbuf); mem_free (outbuf); mem_free (sizes); return (-1); } nbr_blocks = 0; nbr_paras = 0; /* Number of paragraphs in block */ insize = 2; /* Leave room for nbr_paras */ while (file read (input, line)) { if (streq (line, "%%")) have_end_of_paragraph (compress); else { memcpy (inbuf + insize, line, strlen (line)); insize += strlen (line); inbuf [insize++] = '\n'; } } have_end_of_paragraph (compress); if (nbr_paras) have_end_of_block (compress); fclose (input); /* Finished with input file */ output = fopen (outfile, "wb"); fprintf (output, "IFF%s -- http://www.imatix.com/ --\n%c", compress? "CMP": "TXT", 26); output_dbyte = htons (nbr_blocks); fwrite (&output_dbyte, 2, 1, output); offset = ftell (output) + nbr_blocks * 4; for (block_nbr = 0; block_nbr < nbr_blocks; block_nbr++) { output_qbyte = htonl (offset); fwrite (&output_qbyte, 4, 1, output); offset += sizes [block_nbr]; } fseek (scratch, 0, SEEK_SET); /* Back to start of scratch file */ while ((outsize = fread (outbuf, 1, BLOCK_SIZE, scratch)) != 0) fwrite (outbuf, 1, outsize, output); file close (output); ftmp close (scratch); mem_free (sizes); mem_free (inbuf); mem_free (outbuf); return (0); /* No errors */ }
| << | < | > | >> | Copyright © 1996-2000 iMatix Corporation |