Subversion Repositories SvarDOS

Rev

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