Subversion Repositories SvarDOS

Rev

Rev 614 | Rev 995 | Go to most recent revision | 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)
3
 * Copyright (C) 2012-2022 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
 
29
void removeDoubleBackslashes(char *str) {
30
  char *curpos;
31
  int x;
32
  for (;;) {
251 mateuszvis 33
    curpos = strstr(str, "\\\\");
219 mateuszvis 34
    if (curpos == NULL) return; /* job done */
35
    for (x = 1; curpos[x] != 0; x++) {
36
      curpos[x - 1] = curpos[x];
37
    }
38
    curpos[x - 1] = 0;
39
  }
40
}
41
 
42
 
43
/* converts a string to all lowercase */
44
void strtolower(char *mystring) {
45
  int x;
46
  for (x = 0; mystring[x] != 0; x++) mystring[x] = tolower(mystring[x]);
47
}
48
 
49
 
50
/* Find the first occurrence of find in s, ignore case. */
51
char *fdnpkg_strcasestr(const char *s, const char *find) {
52
  char c, sc;
53
  size_t len;
54
  if ((c = *find++) != 0) {
55
    c = tolower((unsigned char)c);
56
    len = strlen(find);
57
    do {
58
      do {
59
        if ((sc = *s++) == 0) return(NULL);
60
      } while ((char)tolower((unsigned char)sc) != c);
61
    } while (strncasecmp(s, find, len) != 0);
62
    s--;
63
  }
64
  return((char *)s);
65
}
66
 
67
 
68
/* Creates directories recursively */
69
void mkpath(char *dirs) {
70
  int x;
71
  char savechar;
72
  for (x = 0; dirs[x] != 0; x++) {
73
    if (((dirs[x] == '/') || (dirs[x] == '\\')) && (x > 0)) {
74
      if (dirs[x - 1] != ':') { /* avoid d:\ stuff */
75
        savechar = dirs[x];
76
        dirs[x] = 0;
77
        /* make the dir */
240 mateuszvis 78
        mkdir(dirs);
219 mateuszvis 79
        dirs[x] = savechar;
80
      }
81
    }
82
  }
83
}
84
 
85
 
86
/* returns a pointer to the start of the filename, out of a path\to\file string, and
87
   fills respath with the local folder where the file should be placed. */
231 mateuszvis 88
char *computelocalpath(char *longfilename, char *respath, const char *dosdir, const struct customdirs *dirlist) {
219 mateuszvis 89
  int x, lastsep = 0, firstsep = -1;
90
  char savedchar;
91
  char *shortfilename, *pathstart;
92
  pathstart = longfilename;
93
  for (x = 0; longfilename[x] != 0; x++) {
94
    if ((longfilename[x] == '/') || (longfilename[x] == '\\')) {
95
      lastsep = x;
96
      if (firstsep < 0) firstsep = x;
97
    }
98
  }
994 mateusz.vi 99
  /* if it's a file without any directory, then it goes to C:\ (COMMAND.COM, KERNEL.SYS...) */
100
  if (firstsep < 0) {
101
    sprintf(respath, "C:\\");
102
    return(longfilename);
103
  }
104
  /* */
219 mateuszvis 105
  shortfilename = &longfilename[lastsep + 1];
106
  /* look for possible custom path */
107
  if (firstsep > 0) {
108
    savedchar = longfilename[firstsep];
109
    longfilename[firstsep] = 0;
110
    for (; dirlist != NULL; dirlist = dirlist->next) {
111
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
112
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
113
        pathstart = &longfilename[firstsep + 1];
114
        dosdir = dirlist->location;
115
        break;
116
      }
117
    }
118
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
119
  }
120
  /* apply the default (DOSDIR) path */
121
  savedchar = longfilename[lastsep + 1];
122
  longfilename[lastsep + 1] = 0;
123
  sprintf(respath, "%s\\%s", dosdir, pathstart);
124
  slash2backslash(respath);
125
  removeDoubleBackslashes(respath);
126
  longfilename[lastsep + 1] = savedchar;
127
  return(shortfilename);
128
}
129
 
130
 
131
/* analyzes a filename string and returns the pointer to the file's extension
132
 * (which can be empty) */
133
char *getfext(char *fname) {
134
  char *res = NULL;
135
  for (; *fname != 0; fname++) {
136
    if (*fname == '.') res = fname + 1;
137
  }
138
  /* if no dot found, then point to the string's null terminator */
139
  if (res == NULL) return(fname);
140
  return(res);
141
}
248 mateuszvis 142
 
143
 
144
/* reads a line from a "token = value" file, returns 0 on success
145
 * val (if not NULL) is updated with a pointer to the "value" part
146
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
147
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
148
  int bytebuff, linelen = 0;
149
  if (val != NULL) *val = NULL;
150
  for (;;) {
151
    bytebuff = fgetc(fd);
152
    if (bytebuff == EOF) {
153
      if (linelen == 0) return(-1);
154
      break;
155
    }
156
    if (bytebuff < 0) return(-1);
157
    if ((*val == NULL) && (bytebuff == delim)) {
158
      line[linelen++] = 0;
159
      *val = line + linelen;
160
      continue;
161
    }
162
    if (bytebuff == '\r') continue; /* ignore CR */
163
    if (bytebuff == '\n') break;
164
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
165
  }
166
  /* terminate line and trim token and value (if any) */
167
  line[linelen] = 0;
168
  trim(line);
169
  if ((val != NULL) && (*val != NULL)) trim(*val);
170
  return(0);
171
}