| 1 |
/* FILE: filenames.c |
| 2 |
* PURPOSE: some handy functions for working with TeXfiles |
| 3 |
* AUTHOR: Piet Tutelaers |
| 4 |
* (see README for license) |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifdef HAVE_CONFIG_H |
| 8 |
#include "c-auto.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifdef KPATHSEA |
| 12 |
#include <kpathsea/kpathsea.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <ctype.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "basics.h" /* basic definitions and fatal() */ |
| 21 |
#include "filenames.h" |
| 22 |
|
| 23 |
/* comparing names (system dependant) */ |
| 24 |
static int equal(const char *s, const char *t) |
| 25 |
{ |
| 26 |
# ifndef UNIX |
| 27 |
while (tolower(*s) == tolower(*t)) { |
| 28 |
if (*s == '\0') |
| 29 |
break; |
| 30 |
s++; t++; |
| 31 |
} |
| 32 |
# else |
| 33 |
while (*s == *t) { |
| 34 |
if (*s == '\0' || *t == '\0') |
| 35 |
break; |
| 36 |
s++; t++; |
| 37 |
} |
| 38 |
# endif |
| 39 |
if (*s == '\0' && *t == '\0') |
| 40 |
return 1; |
| 41 |
else |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Determine the extension |
| 47 |
*/ |
| 48 |
char *extension(char *str) { |
| 49 |
char *p, *base; |
| 50 |
for (p = base = str; *p; ++p) |
| 51 |
/* if (*p++ == DIRSEP) base = p; */ |
| 52 |
if (IS_DIR_SEP(*p)) |
| 53 |
base = p + 1; |
| 54 |
for (p = base ; *p; p++) |
| 55 |
if (*p == '.') |
| 56 |
break; |
| 57 |
return p; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* Construct filename by replacing the current extension (if available) |
| 62 |
* with supplied one. |
| 63 |
*/ |
| 64 |
char *newname(char *name, const char *ext) |
| 65 |
{ |
| 66 |
char *e, *nn; int len1, len2; |
| 67 |
|
| 68 |
e = extension(name); |
| 69 |
if (equal(ext, e)) |
| 70 |
return name; |
| 71 |
len1 = strlen(name) - strlen(e); |
| 72 |
len2 = len1 + strlen(ext) + 1; |
| 73 |
nn = (char *) malloc(len2); |
| 74 |
if (nn == NULL) |
| 75 |
fatal("Out of memory\n"); |
| 76 |
strncpy(nn, name, len1); |
| 77 |
strcpy(nn+len1, ext); |
| 78 |
return nn; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Derived from BSD basename |
| 83 |
*/ |
| 84 |
char *basename(char *str, const char *suffix){ |
| 85 |
char *p; int len = 0; |
| 86 |
const char *t; |
| 87 |
char *base; |
| 88 |
|
| 89 |
printf("basename of %s = ", str); |
| 90 |
#ifdef KPATHSEA |
| 91 |
for (p = base = (NAME_BEGINS_WITH_DEVICE(str) ? str+2 : str); |
| 92 |
*p; p++) { |
| 93 |
#else |
| 94 |
for (p = base = str; *p; p++) { |
| 95 |
#endif |
| 96 |
/* if (*p++ == DIRSEP) { base = p; len = 0; } */ |
| 97 |
if (IS_DIR_SEP(*p)) { |
| 98 |
base = p+1; |
| 99 |
len = 0; |
| 100 |
} else len++; |
| 101 |
} |
| 102 |
printf("%s\n", base); |
| 103 |
if (suffix != NULL) { |
| 104 |
for (t = suffix; *t; ++t); |
| 105 |
do { |
| 106 |
len--; t--; p--; |
| 107 |
if (*t != *p) break; |
| 108 |
if (t == suffix) { |
| 109 |
char *bn; |
| 110 |
if (len == 0) |
| 111 |
return NULL; |
| 112 |
bn = malloc(len+1); |
| 113 |
if (bn == NULL) |
| 114 |
fatal("Out of memory\n"); |
| 115 |
strncpy(bn, base, len); |
| 116 |
*(bn+len) = '\0'; /* RA */ |
| 117 |
return bn; |
| 118 |
} |
| 119 |
} while (p > base); |
| 120 |
} |
| 121 |
return base; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* Return true if name can be the name of a PostScript resource |
| 126 |
* (no extension and no absolute pathname). |
| 127 |
*/ |
| 128 |
int ps_resource(const char *name) { |
| 129 |
if (strchr(name, '.')) return 0 ; |
| 130 |
#ifdef KPATHSEA |
| 131 |
if (kpse_absolute_p(name, true)) return 0; |
| 132 |
#else |
| 133 |
if (strchr(name, DIRSEP)) return 0 ; |
| 134 |
#endif |
| 135 |
return 1; |
| 136 |
} |