|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Listing 32.3 rq.cA Simple Random Quotation Script Used as an SSI // RQ.C // This program reads a text file and extracts a random // quotation from it. If a citation line is found, it // treats it as a citation; otherwise, all text is treated // the same. HTML tags may be embedded in the text. // RQ is mostly platform-independent. Youll have to change // path element separators to the correct slash if you // compile for UNIX. There are no platform-specific system // calls, though, so a little bit of customization should // enable the code to run on any platform. #include <windows.h> // only required for Windows #include <stdio.h> #include <stdlib.h> #include <io.h> char buffer[16000]; // temp holding buffer void main(int argc, char * argv[]) { FILE *f; // file-info structure fpos_t fpos; // file-pos structure long flen; // length of the file char fname[80]; // the file name long lrand; // a long random number BOOL goodpos; // switch char *p; // generic pointer char *soq; // start-of-quote pointer char *eoq; // end-of-quote pointer // Seed the random number generator srand(GetTickCount()); // Set all I/O streams to unbuffered setvbuf(stdin,NULL,_IONBF,0); setvbuf(stdout,NULL,_IONBF,0); // Open the quote file // If a command-line argument is present, treat it as // the file name. But first check it for validity! if (argc > 1) { p = strstr(argv[1],..); if (p==NULL) p = strstr(argv[1],\\\\); if (p==NULL) p = strchr(argv[1],:); // If .., \\, or : found, reject the filename if (p) { printf(Invalid relative path specified: %s,argv[1]); return; } // Otherwise append it to our own path strcpy(fname,argv[0]); p = strrchr(fname,\\); if (p) *p = \0; strcat(fname,\\); strcat(fname,argv[1]); } else { // No command-line parm found, so use our // executable name, minus our extension, plus // .txt as the filename strcpy(fname,_pgmptr); p = strrchr(fname,.); if (p) strcpy(p,.txt); } // We have a filename, so try to open the file f = fopen(fname,r); // If open failed, die right here if (f==NULL) { printf(Could not open %s for read.,fname); return; } // Get total length of file in bytes. // We do this by seeking to the end and then // reading the offset of our current position. // There are other ways of getting this // information, but this way works almost // everywhere, whereas the other ways are // platform-dependent. fseek(f,0,SEEK_END); fgetpos(f,&fpos); flen = (long) fpos; // Seek to a random point in the file. Loop through // the following section until we find a block of text // we can use. goodpos = FALSE; // goes TRUE when were done while (!goodpos) { // Make a random offset into the file. Generate // the number based on the files length. if (flen > 65535) { lrand = MAKELONG(rand(),rand()); } else { lrand = MAKELONG(rand(),0); } // If our random number is less than the length // of the file, use it as an offset. Seek there // and read whatever we find. if (lrand < flen) { fpos = lrand; fsetpos(f,&fpos); if (fread(buffer, sizeof(char), sizeof(buffer),f) !=0 ) { soq=NULL; eoq=NULL; soq = strstr(buffer,\n\n); if (soq) eoq = strstr(soq+2,\n\n); if (eoq) { // skip the first CR soq++; // and the one for the blank line soq++; // mark end of string *eoq=\0'; // look for citation marker p = strstr(soq,\n--); // if found, exempt it & remember if (p) { *p=\0'; p++; } // print the quotation printf(soq); if (p) // and citation if any printf(<br><cite>%s</cite>,p); // exit the loop goodpos=TRUE; } } } } fclose(f); fflush(stdout); return; } XMASThe XMAS program prints out the number of days remaining until Christmas. It recognizes Christmas Day and Christmas Eve as special cases, and solves the general case problem through brute force. You can certainly find more elegant and efficient ways to calculate elapsed time, but this method doesnt rely on any platform-specific date/time routines. The code in Listing 32.4 is short enough and uncomplicated enough that it needs no further explanation. Listing 32.4 xmas.cA Simple SSI Script that Counts Down the Days Until Christmas // CHRISTMAS.C // This program calculates the number of days between // the time of invocation and the nearest upcoming 25 // December. It reports the result as a complete sentence. // The code is platform-independent. #include <windows.h> // only required for Windows #include <stdio.h> #include <time.h> void main() { // Some variables, all self-explanatory struct tm today; time_t now; int days; // Get the current date, first retrieving the // Universal Coordinated Time, then converting it // to local time, stored in the today tm structure. time(&now); today = *localtime(&now); mktime(&today); // month is zero-based (0=jan, 1=feb, etc); // day is one-based // year is one-based // so Christmas Eve is 11/24 // Is it Christmas Eve? if ((today.tm_mon == 11) && (today.tm_mday==24)) { printf(Today is Christmas Eve!); } else { // Is it Christmas Day? if ((today.tm_mon == 11) && (today.tm_mday==25)) { printf(Today is Christmas Day!); } else { // Calculate days by adding one and comparing // for 11/25 repeatedly days =0; while ( (today.tm_mon != 11) | (today.tm_mday != 25) ) { days++; today.tm_mday = today.tm_mday + 1; mktime(&today); } // Print the result using the customary // static verb formation printf(There are %i days until Christmas. ,days); } } // Flush the output and were done fflush(stdout); return; }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |