[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | filname.c -- file name and extension extractors
|
---|
| 4 | Version 3 -- 1987-07-09 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define TESTER 0 /* define non-zero for test routine */
|
---|
| 9 |
|
---|
[b28a12e] | 10 | #include "ram.h"
|
---|
[f40a309] | 11 |
|
---|
| 12 | #define MAX_NAME 8 /* maximum length of a file name */
|
---|
| 13 | #define MAX_EXT 3 /* maximum length of an extension */
|
---|
| 14 |
|
---|
| 15 | /*
|
---|
| 16 | =============================================================================
|
---|
| 17 | FilName(s, p) -- Return a pointer to a string containing the file name
|
---|
| 18 | found in the string at 's'. Copy the file name into string 'p'.
|
---|
| 19 | =============================================================================
|
---|
| 20 | */
|
---|
| 21 |
|
---|
[7258c6a] | 22 | int8_t *FilName(int8_t *s, int8_t *p)
|
---|
[f40a309] | 23 | {
|
---|
[7258c6a] | 24 | register int8_t *tp;
|
---|
| 25 | register int16_t i;
|
---|
[f40a309] | 26 |
|
---|
| 27 | tp = p;
|
---|
| 28 |
|
---|
| 29 | for (i = 0; i < MAX_NAME; i++) { /* scan the name */
|
---|
| 30 |
|
---|
| 31 | if (*s) { /* ... until we hit a '\0' */
|
---|
| 32 |
|
---|
| 33 | if (*s EQ '.') /* ... or a '.' */
|
---|
| 34 | break;
|
---|
| 35 |
|
---|
| 36 | *p++ = *s++; /* ... copying as we go */
|
---|
| 37 |
|
---|
| 38 | } else { /* stop at '\0' */
|
---|
| 39 |
|
---|
| 40 | *p = '\0'; /* terminate the string */
|
---|
| 41 | return(tp); /* return a pointer to it */
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | *p = '\0'; /* copied MAX_NAME bytes - that's all folks */
|
---|
| 46 | return(tp);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /*
|
---|
| 50 | =============================================================================
|
---|
| 51 | FilExt(s, p) -- Returns a pointer to a copy of the extension of
|
---|
| 52 | the file name in 's'. The file name is copied into string 'p' and
|
---|
| 53 | a pointer to 'p' is returned.
|
---|
| 54 | =============================================================================
|
---|
| 55 | */
|
---|
| 56 |
|
---|
[7258c6a] | 57 | int8_t *FilExt(int8_t *s, int8_t *p)
|
---|
[f40a309] | 58 | {
|
---|
[7258c6a] | 59 | register int8_t c, *tp;
|
---|
| 60 | register int16_t i;
|
---|
[f40a309] | 61 |
|
---|
| 62 | tp = p;
|
---|
| 63 |
|
---|
[8973acd] | 64 | while ((c = *s)) { /* scan the string */
|
---|
[f40a309] | 65 |
|
---|
| 66 | if (c EQ '.') { /* ... until we hit the dot */
|
---|
| 67 |
|
---|
| 68 | ++s; /* point past the dot */
|
---|
| 69 |
|
---|
| 70 | for (i = 0; i < MAX_EXT; i++) { /* scan the extension ... */
|
---|
| 71 |
|
---|
| 72 | if (*s) { /* ... until we hit a '\0' */
|
---|
| 73 |
|
---|
| 74 | *p++ = *s++; /* ... copying as we go */
|
---|
| 75 |
|
---|
| 76 | } else { /* stop at '\0' */
|
---|
| 77 |
|
---|
| 78 | *p = '\0'; /* terminate the string */
|
---|
| 79 | return(tp); /* return a pointer to it */
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | *p = '\0'; /* copied MAX_EXT bytes - that's all folks */
|
---|
| 84 | return(tp); /* return a pointer to the result */
|
---|
| 85 |
|
---|
| 86 | } else {
|
---|
| 87 |
|
---|
| 88 | ++s; /* advance the pointer */
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | *p = '\0'; /* terminate the string */
|
---|
| 93 | return(tp); /* return a pointer to the result */
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #if TESTER
|
---|
| 97 |
|
---|
| 98 | char *fn[] = { /* test cases */
|
---|
| 99 |
|
---|
| 100 | "FILE.NAM",
|
---|
| 101 | "FILE",
|
---|
| 102 | "FILE.",
|
---|
| 103 | ".NAM",
|
---|
| 104 | ".",
|
---|
| 105 | "",
|
---|
| 106 | "fartoolonganame.longextension",
|
---|
| 107 | "fartoolonganame",
|
---|
| 108 | "fartoolonganame.",
|
---|
| 109 | ".longextension"
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | #define NCASES ((sizeof fn) / (sizeof (char *)))
|
---|
| 113 |
|
---|
| 114 | char temp1[MAX_NAME+1], temp2[MAX_EXT+1];
|
---|
| 115 |
|
---|
| 116 | fnt(s) /* test both functions and print the result */
|
---|
| 117 | char *s;
|
---|
| 118 | {
|
---|
| 119 | printf("[%s] gave [%s] [%s]\r\n",
|
---|
| 120 | s, FilName(s, temp1), FilExt(s, temp2));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | main()
|
---|
| 124 | {
|
---|
| 125 | int i;
|
---|
| 126 |
|
---|
| 127 | /* hand the functions each of the test cases */
|
---|
| 128 |
|
---|
| 129 | for (i = 0; i < NCASES; i++)
|
---|
| 130 | fnt(fn[i]);
|
---|
| 131 |
|
---|
[5f714c9] | 132 | exit();
|
---|
[f40a309] | 133 | }
|
---|
| 134 |
|
---|
| 135 | #endif
|
---|
[6262b5c] | 136 |
|
---|