home *** CD-ROM | disk | FTP | other *** search
- /**********
- * INDEXKEY.C
- *
- * by Ralph Davis
- * modified by Tom Rettig, Leonard Zerman
- *
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * SYNTAX: INDEXKEY <expC1> [<expC2> ... <expCn> /p]
- *
- * PARAMETERS: <expC> = filename to read index expression of
- * accepts wildcards
- *
- * /p = pause after one screenful of output
- *
- * RETURNS: Index key expression used to create <expC>
- *
- * SPECIAL LINK NOTE:
- * This program makes calls to _TR_FUNC.ASM, an assembly
- * language routine that must be named in your link command.
- *********/
-
- #include "trlib.h"
- #include "stdio.h"
-
- static char new_dir[65]; /* Holds name of directory to change to
- (if change is necessary) */
-
- TRTYPE main(argc, argv)
- int argc;
- char *argv[];
- {
- int get_drive(), curr_drive; /* For current drive */
- int set_drive(), new_drive; /* To change to new disk drive */
- int set_dir(); /* To change directories */
- char *get_dir(); /* Returns current directory */
- char *curr_dir; /* Holds current directory */
- static char filename[80]; /* Holds name of desired file */
- char *curr_file; /* Holds name of current matching file */
- char *char_pos; /* Holds position of backslash in
- requested filespec */
- int status;
- char *find_first(); /* DOS function 0x4E--find first
- matching file */
- char *find_next(); /* DOS function 0x4F--find next
- matching file */
- static char indexkey[101]; /* Holds index key expression */
- int counter = 0; /* Counts number of index keys
- displayed--regulates screen pause */
- int cl_args; /* Indicates whether command line
- arguments were entered */
- int scroll = FALSE; /* Pause screen or not */
- int i;
-
- if (argv[argc-1][0] == '/' &&
- toupper(argv[argc-1][1]) == 'P') /* /P (scroll) option specified */
- {
- scroll = TRUE;
- --argc; /* Back up to next to last argument */
- }
-
- if (argc == 1) /* No filename entered */
- {
- printf("Please enter filename (return for *.NTX): ");
- gets( filename );
-
- /* If user presses <RETURN>, default to file name "*.NTX" */
-
- if (_tr_strlen(filename) == 0)
- _tr_strcpy( filename, "*.NTX" );
- ++argc; /* Bump argc artificially */
- cl_args = FALSE; /* No command line arguments */
- }
- else
- {
- cl_args = TRUE; /* Command line arguments were entered */
- }
-
- curr_drive = get_drive(); /* Get current drive */
-
- while (--argc)
- {
- i = 0;
- if (cl_args) /* If command line arguments entered,
- copy current one into filename buffer */
- _tr_strcpy( filename, argv[argc] );
-
-
- if (filename[1] == ':') /* Was drive specified? */
- {
- /* Convert drive letter to integer for DOS call */
- new_drive = (toupper(filename[0]) - 'A');
-
- status = set_drive(new_drive); /* Change to indicated drive */
-
- if (status == (-1))
- {
- printf("\007Invalid drive specification: %c\n\n",
- new_drive + 'A');
- continue;
- }
- }
- else
- {
- new_dir[i++] = (curr_drive + 'A'); /* Put drive letter and ':'
- in full path name of
- file */
- new_dir[i++] = ':';
- }
-
- /* Was path specified? */
-
- char_pos = _tr_strrchr(filename, '\\'); /* Find last occurrence of
- backslash in filename */
- curr_dir = get_dir(); /* Get current directory */
-
- if (char_pos != 0L)
- {
- /* Add pathname to new directory string by using
- _tr_strncpy to copy everything up to the final
- backslash */
-
- _tr_strncpy(&new_dir[i], filename, char_pos-filename);
-
- /* Now put the filename into the filename buffer */
-
- _tr_strcpy(filename, char_pos+1);
-
- status = set_dir(new_dir); /* Change to new directory */
-
- if (status == (-1)) /* Invalid path */
- {
- printf("\007Path not found: %s\n\n",_tr_toup(new_dir));
- set_dir(curr_dir); /* Return to old directory */
- set_drive(curr_drive); /* Return to old drive */
- continue;
- }
- }
- else
- {
- _tr_strcpy(&new_dir[i], curr_dir); /* Place current directory
- into new_dir buffer */
- }
-
- /* Get first matching file */
-
- curr_file = find_first(filename);
-
- if (curr_file == 0L)
- {
- printf("\007No matching files found: %s\\%s\n\n",
- _tr_toup(new_dir),_tr_toup(filename));
- }
- else
- {
- while (curr_file != 0L)
- {
-
- /* Put file's index key into indexkey buffer */
-
- status = get_ikey(curr_file, indexkey);
- if (status != (-1))
- {
- /* Print filespec and index key */
-
- printf("%s\\%s:\n\t%s\n\n",_tr_toup(new_dir),curr_file,
- _tr_toup(indexkey));
- ++counter;
-
- if (counter >= 7)
- {
- if (scroll) /* Stop screen scrolling if /P
- option specified */
- {
- printf("\nPress any key to continue...");
- getch(); /* Press any key */
- printf("\n\n"); /* Two linefeeds */
- }
- counter = 0; /* Reset line counter */
- }
- }
- curr_file = find_next(); /* Get next matching file */
-
- } /* ENDWHILE: curr_file != 0L
- i.e., while matching files are found */
- } /* ENDIF: curr_file == 0L */
-
- set_dir(curr_dir); /* Return to old directory */
- set_drive(curr_drive); /* and old drive */
-
- } /* ENDWHILE: argc-- */
- } /* End of main procedure */
-
-
-
- int get_ikey(s,t) /* Extract index key from file */
- char *s;
- char *t;
- {
- int status, fd;
- int i;
- char thischar; /* Buffer to read single character from file into */
- long offset; /* File seek offset */
- long file_pos; /* Current file position */
- long filesize; /* Size of file */
-
- fd = _tr_open(s, READONLY); /* Open file for read only */
-
- i = 0;
- if (fd == (-1))
- {
- printf("\007Error opening file %s\\%s\n\n",_tr_toup(new_dir),
- _tr_toup(s));
-
- t[i] = '\0'; /* Indexkey <== null string */
- return(fd);
-
- }
-
- offset = 10L; /* For dBASE II index file */
-
- while (1)
- {
- filesize = _tr_lseek(fd, 0L, 2); /* Get file size
- by seeking 0 bytes from EOF */
-
- file_pos = _tr_lseek(fd, offset, 0); /* Go to offset bytes from
- beginning of file */
-
- if ((file_pos < (0L)) || (filesize < 25L))
- {
- printf("\007Invalid index file format: %s\\%s\n\n",
- _tr_toup(new_dir), _tr_toup(s));
- status = (-1);
- break;
- }
-
- status = _tr_read(fd, &thischar, 1); /* Read one character
- at current file offset */
-
- if (status == (-1))
- {
- printf("\007Error reading file: %s\\%s\n\n",
- _tr_toup(new_dir), _tr_toup(s));
- break;
- }
-
- if (isalpha(thischar)) /* If not alpha character, not index key */
- {
- _tr_lseek(fd, -1L, 1); /* Back up one byte
- from current position */
-
- do
- {
- status = _tr_read(fd, &t[i], 1); /* Read in one byte at a time */
- if (status == (-1))
- {
- printf("\007Error reading file: %s\\%s\n\n",
- _tr_toup(new_dir), _tr_toup(s));
- break;
- }
- } while (t[i++] >= ' '); /* Read in characters till we get
- to one less than ASCII 0x20 */
- break;
- }
- else
- {
- if (offset == 10L)
- offset = 22L; /* For Clipper */
-
- else if (offset == 22L)
- offset = 24L; /* For dBASE III */
-
- else
- break;
- }
- }
- t[i] = '\0'; /* Null terminate index key */
- _tr_close(fd);
- return(status);
- }
-
-