123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "bpfind.h"
- #include "opendoor.h"
- #include "pageview.h"
- #define FILENAME_SIZE 75
- #define PATH_CHARS (FILENAME_SIZE - 13)
- #define LINE_SIZE 80
- #define ARRAY_GROW_SIZE 20
- int nTotalFiles = 0;
- int nFileArraySize = 0;
- char *paszFileArray = NULL;
- FILE *pfCurrentFile;
- int nTotalLines = 0;
- int nLineArraySize = 0;
- long *palLineOffset = NULL;
- void AddFilesMatching(char *pszFileSpec);
- char *GetFilename(int nIndex);
- int AddFilename(char *pszFilename);
- void GetDirOnly(char *pszOutDirName, const char *pszInPathName);
- int DirExists(const char *pszDirName);
- void BuildPath(char *pszOut, char *pszPath, char *pszFilename);
- void FreeFileList(void);
- void DisplayFileName(int nLine, void *pCallbackData);
- void DisplayFile(char *pszFilename);
- void DisplayFileLine(int nLine, void *pCallbackData);
- int AddOffsetToArray(long lOffset);
- void FreeLineArray(void);
- int main(int nArgCount, char *papszArgument[])
- {
- int nArg;
- int nChoice;
- od_init();
-
- if(nArgCount >= 2)
- {
- for(nArg = 1; nArg < nArgCount; ++nArg)
- {
- AddFilesMatching(papszArgument[nArg]);
- }
- }
-
- else
- {
- AddFilesMatching("*.*");
- }
-
- if(nTotalFiles == 0)
- {
- od_printf("No files were found.\n\r\n\r");
- od_printf("Press [Enter] to continue.\n\r");
- od_get_answer("\n\r");
- return(0);
- }
-
- else if(nTotalFiles == 1)
- {
- DisplayFile(GetFilename(0));
- }
-
-
- else
- {
-
- nChoice = 0;
- for(;;)
- {
-
- nChoice = PagedViewer(nChoice, nTotalFiles, DisplayFileName,
- NULL, TRUE, "Choose A File To Display", 19);
-
- if(nChoice == NO_LINE) break;
-
- DisplayFile(GetFilename(nChoice));
- }
- }
- FreeFileList();
- return(0);
- }
- void AddFilesMatching(char *pszFileSpec)
- {
- struct ffblk DirEntry;
- int bNoMoreFiles;
- char szDirName[PATH_CHARS + 1];
- char szFileName[FILENAME_SIZE];
-
- if(strlen(pszFileSpec) > PATH_CHARS)
- {
- return;
- }
-
- GetDirOnly(szDirName, pszFileSpec);
- bNoMoreFiles = findfirst(pszFileSpec, &DirEntry, FA_RDONLY);
- while(!bNoMoreFiles)
- {
- BuildPath(szFileName, szDirName, DirEntry.ff_name);
- AddFilename(szFileName);
- bNoMoreFiles = findnext(&DirEntry);
- }
- }
- void GetDirOnly(char *pszOutDirName, const char *pszInPathName)
- {
- char *pchBackslashChar;
-
- strcpy(pszOutDirName, pszInPathName);
-
- pchBackslashChar = strrchr(pszOutDirName, '\\');
- if(pchBackslashChar != NULL)
- {
-
- *pchBackslashChar = '\0';
- }
- else
- {
-
-
- pszOutDirName[0] = '\0';
- }
- }
- void BuildPath(char *pszOut, char *pszPath, char *pszFilename)
- {
-
- strcpy(pszOut, pszPath);
-
- if(strlen(pszOut) > 0 && pszOut[strlen(pszOut) - 1] != '\\')
- {
- strcat(pszOut, "\\");
- }
-
- strcat(pszOut, pszFilename);
- }
- char *GetFilename(int nIndex)
- {
- return(paszFileArray + (nIndex * FILENAME_SIZE));
- }
- int AddFilename(char *pszFilename)
- {
- int nNewArraySize;
- char *paszNewArray;
- char *pszNewString;
-
- if(nTotalFiles == nFileArraySize)
- {
- nNewArraySize = nFileArraySize + ARRAY_GROW_SIZE;
- if((paszNewArray =
- realloc(paszFileArray, nNewArraySize * FILENAME_SIZE)) == NULL)
- {
- return(FALSE);
- }
- nFileArraySize = nNewArraySize;
- paszFileArray = paszNewArray;
- }
-
-
- pszNewString = GetFilename(nTotalFiles++);
-
- strncpy(pszNewString, pszFilename, FILENAME_SIZE - 1);
- pszNewString[FILENAME_SIZE - 1] = '\0';
- return(TRUE);
- }
- void FreeFileList(void)
- {
- if(nFileArraySize > 0)
- {
- free(paszFileArray);
- nFileArraySize = 0;
- nTotalFiles = 0;
- paszFileArray = NULL;
- }
- }
- void DisplayFileName(int nLine, void *pCallbackData)
- {
- (void)pCallbackData;
- od_printf(GetFilename(nLine));
- }
- void DisplayFile(char *pszFilename)
- {
- char szLine[LINE_SIZE];
- long lnOffset;
-
- od_clr_scr();
-
- pfCurrentFile = fopen(pszFilename, "r");
- if(pfCurrentFile == NULL)
- {
- od_printf("Unable to open file.\n\r\n\r");
- od_printf("Press [Enter] to continue.\n\r");
- od_get_answer("\n\r");
- return;
- }
-
- for(;;)
- {
- lnOffset = fTell(pfCurrentFile);
- if(fgets(szLine, LINE_SIZE, pfCurrentFile) == NULL) break;
- AddOffsetToArray(lnOffset);
- }
-
- PagedViewer(0, nTotalLines, DisplayFileLine, NULL, FALSE, NULL, 21);
-
- FreeLineArray();
-
- fclose(pfCurrentFile);
- }
- void DisplayFileLine(int nLine, void *pCallbackData)
- {
- char szLine[LINE_SIZE];
- long lnTargetOffset = palLineOffset[nLine];
- int nLineLen;
- (void)pCallbackData;
-
- if(lnTargetOffset != ftell(pfCurrentFile))
- {
- fseek(pfCurrentFile, lnTargetOffset, SEEK_SET);
- }
-
- if(fgets(szLine, LINE_SIZE, pfCurrentFile) != NULL)
- {
-
- nLineLen = strlen(szLine);
- while(nLineLen > 0
- && (szLine[nLineLen - 1] == '\r' || szLine[nLineLen - 1] == '\n'))
- {
- szLine[--nLineLen] = '\0';
- }
-
- od_disp_str(szLine);
- }
- }
- int AddOffsetToArray(long lOffset)
- {
- long *palNewArray;
- int nNewArraySize;
-
- if(nTotalLines == nLineArraySize)
- {
- nNewArraySize = nLineArraySize + ARRAY_GROW_SIZE;
- if((palNewArray =
- realloc(palLineOffset, nNewArraySize * sizeof(long))) == NULL)
- {
- return(FALSE);
- }
- nLineArraySize = nNewArraySize;
- palLineOffset = palNewArray;
- }
- palLineOffset[nTotalLines++] = lOffset;
- return(TRUE);
- }
- void FreeLineArray(void)
- {
- if(nLineArraySize > 0)
- {
- nTotalLines = 0;
- nLineArraySize = 0;
- free(palLineOffset);
- palLineOffset = NULL;
- }
- }
-
|