Subversion Repositories SvarDOS

Rev

Rev 995 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
994 mateusz.vi 2
 * This file is part of pkg (SvarDOS package manager)
1678 mateusz.vi 3
 * Copyright (C) 2012-2024 Mateusz Viste
219 mateuszvis 4
 *
5
 * It contains a few helper function...
6
 */
7
 
8
 
9
#include <ctype.h>    /* tolower() */
240 mateuszvis 10
#include <direct.h>   /* provides the mkdir() prototype */
219 mateuszvis 11
#include <string.h>   /* */
12
#include <stdio.h>    /* sprintf() */
13
#include <stdlib.h>   /* atoi() */
14
#include <sys/stat.h> /* mkdir() */
15
 
256 mateuszvis 16
#include "trim.h"
219 mateuszvis 17
#include "helpers.h"
18
 
19
 
20
/* change all / to \ in a string */
21
void slash2backslash(char *str) {
22
  int x;
23
  for (x = 0; str[x] != 0; x++) {
24
    if (str[x] == '/') str[x] = '\\';
25
  }
26
}
27
 
28
 
1678 mateusz.vi 29
/* trim CRC from a filename and returns a pointer to the CRC part.
30
 * this is used to parse filename lines from LSM files */
31
char *trimfnamecrc(char *fname) {
32
  while (*fname) {
33
    if (*fname == '?') {
34
      *fname = 0;
35
      return(fname + 1);
36
    }
37
    fname++;
38
  }
39
  return(0);
40
}
41
 
42
 
219 mateuszvis 43
void removeDoubleBackslashes(char *str) {
44
  char *curpos;
45
  int x;
46
  for (;;) {
251 mateuszvis 47
    curpos = strstr(str, "\\\\");
219 mateuszvis 48
    if (curpos == NULL) return; /* job done */
49
    for (x = 1; curpos[x] != 0; x++) {
50
      curpos[x - 1] = curpos[x];
51
    }
52
    curpos[x - 1] = 0;
53
  }
54
}
55
 
56
 
57
/* Find the first occurrence of find in s, ignore case. */
58
char *fdnpkg_strcasestr(const char *s, const char *find) {
59
  char c, sc;
60
  size_t len;
61
  if ((c = *find++) != 0) {
62
    c = tolower((unsigned char)c);
63
    len = strlen(find);
64
    do {
65
      do {
66
        if ((sc = *s++) == 0) return(NULL);
67
      } while ((char)tolower((unsigned char)sc) != c);
68
    } while (strncasecmp(s, find, len) != 0);
69
    s--;
70
  }
71
  return((char *)s);
72
}
73
 
74
 
75
/* Creates directories recursively */
76
void mkpath(char *dirs) {
77
  int x;
78
  char savechar;
79
  for (x = 0; dirs[x] != 0; x++) {
80
    if (((dirs[x] == '/') || (dirs[x] == '\\')) && (x > 0)) {
81
      if (dirs[x - 1] != ':') { /* avoid d:\ stuff */
82
        savechar = dirs[x];
83
        dirs[x] = 0;
84
        /* make the dir */
240 mateuszvis 85
        mkdir(dirs);
219 mateuszvis 86
        dirs[x] = savechar;
87
      }
88
    }
89
  }
90
}
91
 
92
 
93
/* returns a pointer to the start of the filename, out of a path\to\file string, and
94
   fills respath with the local folder where the file should be placed. */
231 mateuszvis 95
char *computelocalpath(char *longfilename, char *respath, const char *dosdir, const struct customdirs *dirlist) {
219 mateuszvis 96
  int x, lastsep = 0, firstsep = -1;
97
  char savedchar;
98
  char *shortfilename, *pathstart;
99
  pathstart = longfilename;
100
  for (x = 0; longfilename[x] != 0; x++) {
101
    if ((longfilename[x] == '/') || (longfilename[x] == '\\')) {
102
      lastsep = x;
103
      if (firstsep < 0) firstsep = x;
104
    }
105
  }
994 mateusz.vi 106
  /* if it's a file without any directory, then it goes to C:\ (COMMAND.COM, KERNEL.SYS...) */
107
  if (firstsep < 0) {
108
    sprintf(respath, "C:\\");
109
    return(longfilename);
110
  }
111
  /* */
219 mateuszvis 112
  shortfilename = &longfilename[lastsep + 1];
113
  /* look for possible custom path */
114
  if (firstsep > 0) {
115
    savedchar = longfilename[firstsep];
116
    longfilename[firstsep] = 0;
117
    for (; dirlist != NULL; dirlist = dirlist->next) {
118
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
119
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
120
        pathstart = &longfilename[firstsep + 1];
121
        dosdir = dirlist->location;
122
        break;
123
      }
124
    }
125
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
126
  }
127
  /* apply the default (DOSDIR) path */
128
  savedchar = longfilename[lastsep + 1];
129
  longfilename[lastsep + 1] = 0;
130
  sprintf(respath, "%s\\%s", dosdir, pathstart);
131
  slash2backslash(respath);
132
  removeDoubleBackslashes(respath);
133
  longfilename[lastsep + 1] = savedchar;
134
  return(shortfilename);
135
}
136
 
137
 
138
/* analyzes a filename string and returns the pointer to the file's extension
139
 * (which can be empty) */
140
char *getfext(char *fname) {
141
  char *res = NULL;
142
  for (; *fname != 0; fname++) {
143
    if (*fname == '.') res = fname + 1;
144
  }
145
  /* if no dot found, then point to the string's null terminator */
146
  if (res == NULL) return(fname);
147
  return(res);
148
}
248 mateuszvis 149
 
150
 
151
/* reads a line from a "token = value" file, returns 0 on success
152
 * val (if not NULL) is updated with a pointer to the "value" part
153
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
154
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
155
  int bytebuff, linelen = 0;
156
  if (val != NULL) *val = NULL;
157
  for (;;) {
158
    bytebuff = fgetc(fd);
159
    if (bytebuff == EOF) {
160
      if (linelen == 0) return(-1);
161
      break;
162
    }
163
    if (bytebuff < 0) return(-1);
164
    if ((*val == NULL) && (bytebuff == delim)) {
165
      line[linelen++] = 0;
166
      *val = line + linelen;
167
      continue;
168
    }
169
    if (bytebuff == '\r') continue; /* ignore CR */
170
    if (bytebuff == '\n') break;
171
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
172
  }
173
  /* terminate line and trim token and value (if any) */
174
  line[linelen] = 0;
175
  trim(line);
176
  if ((val != NULL) && (*val != NULL)) trim(*val);
177
  return(0);
178
}