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 |
|
---|
10 | #include <stddefs.h>
|
---|
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 |
|
---|
22 | int8_t *FilName(int8_t *s, int8_t *p)
|
---|
23 | {
|
---|
24 | register int8_t *tp;
|
---|
25 | register int16_t i;
|
---|
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 | */
|
---|
52 |
|
---|
53 | /*
|
---|
54 | =============================================================================
|
---|
55 | FilExt(s, p) -- Returns a pointer to a copy of the extension of
|
---|
56 | the file name in 's'. The file name is copied into string 'p' and
|
---|
57 | a pointer to 'p' is returned.
|
---|
58 | =============================================================================
|
---|
59 | */
|
---|
60 |
|
---|
61 | int8_t *FilExt(int8_t *s, int8_t *p)
|
---|
62 | {
|
---|
63 | register int8_t c, *tp;
|
---|
64 | register int16_t i;
|
---|
65 |
|
---|
66 | tp = p;
|
---|
67 |
|
---|
68 | while (c = *s) { /* scan the string */
|
---|
69 |
|
---|
70 | if (c EQ '.') { /* ... until we hit the dot */
|
---|
71 |
|
---|
72 | ++s; /* point past the dot */
|
---|
73 |
|
---|
74 | for (i = 0; i < MAX_EXT; i++) { /* scan the extension ... */
|
---|
75 |
|
---|
76 | if (*s) { /* ... until we hit a '\0' */
|
---|
77 |
|
---|
78 | *p++ = *s++; /* ... copying as we go */
|
---|
79 |
|
---|
80 | } else { /* stop at '\0' */
|
---|
81 |
|
---|
82 | *p = '\0'; /* terminate the string */
|
---|
83 | return(tp); /* return a pointer to it */
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | *p = '\0'; /* copied MAX_EXT bytes - that's all folks */
|
---|
88 | return(tp); /* return a pointer to the result */
|
---|
89 |
|
---|
90 | } else {
|
---|
91 |
|
---|
92 | ++s; /* advance the pointer */
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | *p = '\0'; /* terminate the string */
|
---|
97 | return(tp); /* return a pointer to the result */
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* |
---|
101 |
|
---|
102 | */
|
---|
103 |
|
---|
104 | #if TESTER
|
---|
105 |
|
---|
106 | char *fn[] = { /* test cases */
|
---|
107 |
|
---|
108 | "FILE.NAM",
|
---|
109 | "FILE",
|
---|
110 | "FILE.",
|
---|
111 | ".NAM",
|
---|
112 | ".",
|
---|
113 | "",
|
---|
114 | "fartoolonganame.longextension",
|
---|
115 | "fartoolonganame",
|
---|
116 | "fartoolonganame.",
|
---|
117 | ".longextension"
|
---|
118 | };
|
---|
119 |
|
---|
120 | #define NCASES ((sizeof fn) / (sizeof (char *)))
|
---|
121 |
|
---|
122 | char temp1[MAX_NAME+1], temp2[MAX_EXT+1];
|
---|
123 |
|
---|
124 | fnt(s) /* test both functions and print the result */
|
---|
125 | char *s;
|
---|
126 | {
|
---|
127 | printf("[%s] gave [%s] [%s]\r\n",
|
---|
128 | s, FilName(s, temp1), FilExt(s, temp2));
|
---|
129 | }
|
---|
130 |
|
---|
131 | main()
|
---|
132 | {
|
---|
133 | int i;
|
---|
134 |
|
---|
135 | /* hand the functions each of the test cases */
|
---|
136 |
|
---|
137 | for (i = 0; i < NCASES; i++)
|
---|
138 | fnt(fn[i]);
|
---|
139 |
|
---|
140 | exit();
|
---|
141 | }
|
---|
142 |
|
---|
143 | #endif
|
---|