/************************************************************ Vnews Copyright By: Stephen Vermeulen 1987. 3635 Utah Dr. N.W., Calgary, Alberta, CANADA, T2N 4A6 This program is designed to be a simple news file reader. The user invokes it with the name of the directory containing the news files he wants to read. The program then allows him to read these files easily. Syntax: vnews [dir_name [first_file]] vnews dir_name will display the contents of all the files in the directory named dir_name. vnews will display the contents of all the files in the current directory. vnews dir_name first_file will allow you to read all the files within the directory "dir_name" and will start you at file "first_file" NOTE: some aspects of the code are clearly examples of how not to code in C! Ie: I have made extensive and unforgivable use of the GOTO statement... NOTE2: This compiles under Lattice 3.1 only, sorry... ************************************************************/ #include #include #include #include #define PGUP 1 #define PGDN 2 #define NEXT 3 #define PREV 4 struct IntuitionBase *IntuitionBase; /* to use intuition functions */ struct GfxBase *GfxBase; /* to use graphics functions */ /************************************************************ The following stuff sets up the window to be used to display the text along with its gadgets that advance you through the text of a single message, of from message to message. The following gadgets are provided: PgUp - display next page of the current message PgDn - displays the previous page of the current message Next - displays the next message Prev - displays the previous message These all live in the title bar, along with the name of the file that is being displayed. ************************************************************/ #define LEFT_OFFSET 410 struct IntuiText pgup_text = { 1, 0, JAM2, 0, 0, NULL, (UBYTE *) "Prev", NULL }; struct IntuiText pgdn_text = { 1, 0, JAM2, 0, 0, NULL, (UBYTE *) "back", NULL }; struct IntuiText next_text = { 1, 0, JAM2, 0, 0, NULL, (UBYTE *) "fore", NULL }; struct IntuiText prev_text = { 1, 0, JAM2, 0, 0, NULL, (UBYTE *) "Next", NULL }; struct Gadget pgup_gad = { NULL, LEFT_OFFSET, 0, 32, 10, GADGHCOMP, RELVERIFY | TOPBORDER, BOOLGADGET, NULL, NULL, &pgup_text, NULL, NULL, PREV, NULL }; struct Gadget pgdn_gad = { &pgup_gad, LEFT_OFFSET+48, 0, 32, 10, GADGHCOMP, RELVERIFY | TOPBORDER, BOOLGADGET, NULL, NULL, &pgdn_text, NULL, NULL, PGDN, NULL }; struct Gadget next_gad = { &pgdn_gad, LEFT_OFFSET+96, 0, 32, 10, GADGHCOMP, RELVERIFY | TOPBORDER, BOOLGADGET, NULL, NULL, &next_text, NULL, NULL, PGUP, NULL }; struct Gadget prev_gad = { &next_gad, LEFT_OFFSET+144, 0, 32, 10, GADGHCOMP, RELVERIFY | TOPBORDER, BOOLGADGET, NULL, NULL, &prev_text, NULL, NULL, NEXT, NULL }; struct NewWindow nw = { 0, 3, 640, 200 - 3, -1, -1, GADGETUP | CLOSEWINDOW, WINDOWDEPTH | WINDOWCLOSE | SMART_REFRESH | BORDERLESS | ACTIVATE, &prev_gad, NULL, NULL, NULL, /* default screen */ NULL, /* no special bm */ 0, 0, -1, -1, WBENCHSCREEN }; /************************************************************ allow space for about 500 files in the directory. ************************************************************/ char names[40000], *pointers[500]; char dir_name[100]; /************************************************************ This array is used to hold offsets from the start of the file to the nth page in it, so the PgDn function works, as the file is paged through by hitting PgUp the position of each page start is entered into the array, then when PgDn is hit you can easily backup. ************************************************************/ unsigned long int page_top[1000]; short current_page; /* index for the page_top array */ short current_file; /* index for the file name array */ struct Window *w; FILE *in; /************************************************************ The display_page() function displays a page of text from the file on the screen. It first clears the window, then it does a line by line display of the file, it reads from the line one line of text at a time, printing each line of text to the window and then advancing to the next line on the window, when it has printed 23 lines of text the process stops. ************************************************************/ char temp_buf[200]; /************************************************************ The clean_string function removes CR's from the text string and expands tabs out to 2 spaces each. ************************************************************/ void clean_string(s) register char *s; { register int n; register int i; n = strlen(s); if (n) { if (strpbrk(s, "\11\15\12")) { for (i = 0; i < n; ++i) { switch(s[i]) { case 10: /* line feed -- replace with space */ case 13: /* carriage return -- replace with space */ s[i] = ' '; break; case 9: /* tab -- expand to 2 spaces */ s[i] = ' '; memcpy(s + i + 2, s + i + 1, n - i); s[i+1] = ' '; ++n; break; } } } } } void display_page() { int dy; SetAPen(w->RPort, 0); RectFill(w->RPort, 0, w->BorderTop + 1, w->Width - 1, w->Height - w->BorderBottom); SetAPen(w->RPort, 1); dy = w->RPort->TxHeight; Move(w->RPort, 0, w->BorderTop + dy); while((w->RPort->cp_y + dy) < w->Height) /* enough room */ { if (fgets(temp_buf, 80, in)) { temp_buf[80] = 0; /* always force an end of string here as fgets does not put one in if 80 characters were read before any end of line type character was hit */ clean_string(temp_buf); /********************************************* This is not the best way of doing it, because lines that are longer than about 80 characters get truncated. One should really break long lines appropriately so that all the text does get displayed. This problem has been partially eliminated by reading in only 80 character chunks from the file by the fgets() function. This is not the true solution because it depends on the font that is being used. *********************************************/ Text(w->RPort, temp_buf, strlen(temp_buf)); w->RPort->cp_x = 0; w->RPort->cp_y += dy; } else /* we have reached EOF */ { clean_string(temp_buf); Text(w->RPort, temp_buf, strlen(temp_buf)); goto eof_stop; } } eof_stop: } /************************************************************ The next_page() function has three modes depending on the value of n: -1 - Move to the previous page and display one page of the file. 0 - Display the file from the very first line. +1 - Move the file to the next page and display it. ************************************************************/ void next_page(n) short n; { unsigned long int pos; if (!in) return; switch(n) { case -1: if (current_page) --current_page; fseek(in, page_top[current_page], 0); display_page(); break; case 0: current_page = 0; page_top[0] = 0; display_page(); break; case 1: pos = ftell(in); if (pos != EOF) { if (current_page < 1000) ++current_page; page_top[current_page] = pos; display_page(); } break; } } /************************************************************ The next file function has three modes depending on the value of n: -1 - Move to the previous file in the file list. 0 - Open the very first file in the file list. +1 - Move to the next file in the file list. ************************************************************/ void next_file(n) short n; { switch(n) { case -1: if (current_file) --current_file; if (in) fclose(in); in = fopen(pointers[current_file], "r"); break; case 0: current_file = 0; in = fopen(pointers[current_file], "r"); break; case 1: if (current_file < 500) ++current_file; if (in) fclose(in); in = fopen(pointers[current_file], "r"); break; } if (in) { SetWindowTitles(w, pointers[current_file], -1); } else { SetWindowTitles(w, "No more news!", -1); } } char start_name[90]; void main(argc, argv) int argc; char *argv[]; { int count, flag; int func, class; struct IntuiMessage *mess; printf("\nVnews - a news file reader was brought to you by:\n"); printf(" Stephen Vermeulen,\n"); printf(" 3635 Utah Dr. N.W.,\n"); printf(" Calgary, Alberta,\n"); printf(" CANADA, T2N 4A6\n"); printf("Copyright 1987 by Stephen Vermeulen\n"); printf("\nThis program must be distributed at or below the costs of the\n"); printf("media. If you like this send a donation to support more Vware.\n"); printf("\nLoading the directory now (this can take time!)...\n"); IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0); if (!IntuitionBase) goto no_intuition; GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0); if (!GfxBase) goto no_graphics; /************************ read in the directory *************************/ if (argc > 1) strmfn(dir_name, NULL, argv[1], "#?", NULL); else strmfn(dir_name, NULL, NULL, "#?", NULL); count = getfnl(dir_name, names, sizeof(names), 0); if (count > 0) { if (strbpl(pointers, 500, names) != count) { printf("Too many files in this directory.\n"); goto dir_error; } strsrt(pointers, count); /**************************************** The directory has now been read and sorted it is time to walk through the files. Currently this is displayed in the console window, one file at a time. ****************************************/ w = (struct Window *) OpenWindow(&nw); if (!w) goto no_window; /* can't open window error */ strmfn(start_name, NULL, argv[1], argv[2], NULL); if (argc == 3) /* start at this file */ { current_file = 0; while (strcmpi(start_name, pointers[current_file]) && (current_file < count)) ++current_file; --current_file; next_file(1); next_page(0); } else { next_file(0); /* open a new file (the first) for display */ next_page(0); /* display the next page in this file */ } flag = TRUE; while(flag) { Wait(1 << w->UserPort->mp_SigBit); while(mess = (struct IntuiMessage *) GetMsg(w->UserPort)) { func = ((struct Gadget *) mess->IAddress)->GadgetID; class = mess->Class; ReplyMsg(mess); if (class == CLOSEWINDOW) flag = FALSE; switch(func) { case PGUP: if (feof(in)) { next_file(1); next_page(0); } else next_page(1); break; case PGDN: if (current_page == 0) { next_file(-1); next_page(0); } else next_page(-1); break; case NEXT: next_file(1); next_page(0); break; case PREV: next_file(-1); next_page(0); break; } } /* end of while more messages */ } /* end of while(flag) */ CloseWindow(w); } else { printf("Could not find any files.\n"); goto no_files; } no_window: dir_error: no_files: CloseLibrary(GfxBase); no_graphics: CloseLibrary(IntuitionBase); no_intuition: }