summaryrefslogtreecommitdiff
path: root/fonts/utilities/ps2pk/filenames.c
blob: 8081233ef25720087da674d8cf889c1eb586f6e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* FILE:    filenames.c
 * PURPOSE: some handy functions for working with TeXfiles
 * AUTHOR:  Piet Tutelaers
 * (see README for license)
 */

#ifdef HAVE_CONFIG_H
#include "c-auto.h"
#endif

#ifdef KPATHSEA
#include <kpathsea/kpathsea.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "basics.h"	/* basic definitions and fatal() */
#include "filenames.h"

/* comparing names (system dependant) */
static int equal(const char *s, const char *t)
{
#  ifndef UNIX
   while (tolower(*s) == tolower(*t)) {
      if (*s == '\0')
         break;
      s++; t++;
   }
#  else
   while (*s == *t) {
      if (*s == '\0' || *t == '\0')
         break;
      s++; t++;
   }
#  endif
   if (*s == '\0' && *t == '\0')
      return 1;
   else
      return 0;
}

/*
 * Determine the extension
 */
char *extension(char *str) {
   char *p, *base;
   for (p = base = str; *p; ++p)
      /*      if (*p++ == DIRSEP) base = p; */
      if (IS_DIR_SEP(*p))
         base = p + 1;
   for (p = base ; *p; p++)
      if (*p == '.')
         break;
   return p;
}

/*
 * Construct filename by replacing the current extension (if available)
 * with supplied one.
 */
char *newname(char *name, const char *ext)
{  
   char *e, *nn; int len1, len2;

   e = extension(name);
   if (equal(ext, e))
      return name;
   len1 = strlen(name) - strlen(e);
   len2 = len1 + strlen(ext) + 1;
   nn = (char *) malloc(len2);
   if (nn == NULL)
      fatal("Out of memory\n");
   strncpy(nn, name, len1);
   strcpy(nn+len1, ext);
   return nn;
}

/*
 * Derived from BSD basename
 */
char *basename(char *str, const char *suffix){   
   char *p; int len = 0;
   const char *t;
   char *base;

   printf("basename of %s = ", str);
#ifdef KPATHSEA
   for (p = base = (NAME_BEGINS_WITH_DEVICE(str) ? str+2 : str);
	*p; p++) {
#else
   for (p = base = str; *p; p++) {
#endif
      /*      if (*p++ == DIRSEP) { base = p; len = 0; } */
      if (IS_DIR_SEP(*p)) {
         base = p+1;
         len = 0;
      } else len++;
   }
   printf("%s\n", base);
   if (suffix != NULL) {
      for (t = suffix; *t; ++t);
      do {
         len--; t--; p--;
         if (*t != *p) break;
         if (t == suffix) {
            char *bn;
            if (len == 0)
               return NULL;
            bn = malloc(len+1);
            if (bn == NULL)
               fatal("Out of memory\n");
            strncpy(bn, base, len);
            *(bn+len) = '\0'; /* RA */
            return bn;
         }
      } while (p > base);
   }
   return base;
}

/*
 * Return true if name can be the name of a PostScript resource
 * (no extension and no absolute pathname).
 */
int ps_resource(const char *name) {
   if (strchr(name, '.')) return 0 ;
#ifdef KPATHSEA
   if (kpse_absolute_p(name, true)) return 0;
#else
   if (strchr(name, DIRSEP)) return 0 ;
#endif
   return 1;
}