Back to index

tetex-bin  3.0
filenames.c
Go to the documentation of this file.
00001 /* FILE:    filenames.c
00002  * PURPOSE: some handy functions for working with TeXfiles
00003  * AUTHOR:  Piet Tutelaers (internet: rcpt@urc.tue.nl)
00004  * VERSION: 1.3 (August 1992)
00005  * VERSION: 1.4 (January 1994)
00006  * VERSION: 1.5 (September 1995)
00007  */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <ctype.h>
00012 #include <string.h>
00013 
00014 #ifdef KPATHSEA
00015 #include <kpathsea/kpathsea.h>
00016 #include <c-auto.h>
00017 #endif
00018 
00019 #include "basics.h"  /* basic definitions and fatal() */
00020 
00021 /* for those systems that don't provide an ANSI strchr() */
00022 static char *charptr(char *name, char c)
00023 {
00024    while (*name != c && *name != '\0') name++; 
00025    if (*name == '\0') return NULL;
00026    else return name;
00027 }
00028 
00029 /* comparing names (system dependant) */
00030 int equal(char *s, char *t)
00031 {
00032 #  ifndef UNIX
00033    while (tolower(*s) == tolower(*t)) {
00034       if (*s == '\0') break;
00035       s++; t++;
00036    }
00037 #  else
00038    while (*s == *t) {
00039       if (*s == '\0' || *t == '\0')  break;
00040       s++; t++;
00041    }
00042 #  endif
00043    if (*s == '\0' && *t == '\0') return 1;
00044    else return 0;
00045 }
00046 
00047 /*
00048  * Determine the extension
00049  */
00050 char *extension(char *str) {
00051    char *p, *base;
00052    for (p = base = str; *p; ++p)
00053      /*      if (*p++ == DIRSEP) base = p; */
00054      if (IS_DIR_SEP(*p)) base = p + 1;
00055    for (p = base ; *p; p++)
00056       if (*p == '.') break;
00057    return p;
00058 }
00059 
00060 /*
00061  * Construct filename by replacing the current extension (if available)
00062  * with supplied one.
00063  */
00064 char *newname(char *name, char *ext)
00065 {  
00066    char *e, *nn; int len1, len2;
00067 
00068    e = extension(name);
00069    if (equal(ext, e)) return name;
00070    len1 = strlen(name) - strlen(e);
00071    len2 = len1 + strlen(ext) + 1;
00072    nn = (char *) malloc(len2);
00073    if (nn == NULL) fatal("Out of memory\n");
00074    strncpy(nn, name, len1);
00075    strcpy(nn+len1, ext);
00076    return nn;
00077 }
00078 
00079 /*
00080  * Derived from BSD basename
00081  */
00082 char *basename(char *str, char *suffix){   
00083    char *p, *t; int len = 0;
00084    char *base;
00085 
00086    printf("basename of %s = ", str);
00087 #ifdef KPATHSEA
00088    for (p = base = (NAME_BEGINS_WITH_DEVICE(str) ? str+2 : str);
00089        *p; p++) {
00090 #else
00091    for (p = base = str; *p; p++) {
00092 #endif
00093      /*      if (*p++ == DIRSEP) { base = p; len = 0; } */
00094      if (IS_DIR_SEP(*p)) { base = p+1; len = 0; }
00095       else len++;
00096    }
00097    printf("%s\n", base);
00098    if (suffix != NULL) {
00099       for (t = suffix; *t; ++t);
00100       do {
00101         len--; t--; p--;
00102         if (*t != *p) break;
00103         if (t == suffix) { char *bn;
00104             if (len == 0) return NULL;
00105             bn = malloc(len+1);
00106             if (bn == NULL) fatal("Out of memory\n");
00107             strncpy(bn, base, len);
00108             *(bn+len) = '\0'; /* RA */
00109             return bn;
00110          }
00111       } while (p > base);
00112    }
00113    return base;
00114 }
00115 
00116 /*
00117  * Return true if name can be the name of a PostScript resource
00118  * (no extension and no absolute pathname).
00119  */
00120 int ps_resource(char *name) {
00121    if (charptr(name, '.')) return 0 ;
00122 #ifdef KPATHSEA
00123    if (kpse_absolute_p(name, true)) return 0;
00124 #else
00125    if (charptr(name, DIRSEP)) return 0 ;
00126 #endif
00127    return 1;
00128 }