Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
219 mateuszvis 1
/*
225 mateuszvis 2
 * This file is part of pkginst
3
 * Copyright (C) 2012-2021 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
  }
99
  shortfilename = &longfilename[lastsep + 1];
100
  /* look for possible custom path */
101
  if (firstsep > 0) {
102
    savedchar = longfilename[firstsep];
103
    longfilename[firstsep] = 0;
104
    for (; dirlist != NULL; dirlist = dirlist->next) {
105
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
106
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
107
        pathstart = &longfilename[firstsep + 1];
108
        dosdir = dirlist->location;
109
        break;
110
      }
111
    }
112
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
113
  }
114
  /* apply the default (DOSDIR) path */
115
  savedchar = longfilename[lastsep + 1];
116
  longfilename[lastsep + 1] = 0;
117
  sprintf(respath, "%s\\%s", dosdir, pathstart);
118
  slash2backslash(respath);
119
  removeDoubleBackslashes(respath);
120
  longfilename[lastsep + 1] = savedchar;
121
  return(shortfilename);
122
}
123
 
124
 
125
/* analyzes a filename string and returns the pointer to the file's extension
126
 * (which can be empty) */
127
char *getfext(char *fname) {
128
  char *res = NULL;
129
  for (; *fname != 0; fname++) {
130
    if (*fname == '.') res = fname + 1;
131
  }
132
  /* if no dot found, then point to the string's null terminator */
133
  if (res == NULL) return(fname);
134
  return(res);
135
}
248 mateuszvis 136
 
137
 
138
/* reads a line from a "token = value" file, returns 0 on success
139
 * val (if not NULL) is updated with a pointer to the "value" part
140
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
141
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
142
  int bytebuff, linelen = 0;
143
  if (val != NULL) *val = NULL;
144
  for (;;) {
145
    bytebuff = fgetc(fd);
146
    if (bytebuff == EOF) {
147
      if (linelen == 0) return(-1);
148
      break;
149
    }
150
    if (bytebuff < 0) return(-1);
151
    if ((*val == NULL) && (bytebuff == delim)) {
152
      line[linelen++] = 0;
153
      *val = line + linelen;
154
      continue;
155
    }
156
    if (bytebuff == '\r') continue; /* ignore CR */
157
    if (bytebuff == '\n') break;
158
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
159
  }
160
  /* terminate line and trim token and value (if any) */
161
  line[linelen] = 0;
162
  trim(line);
163
  if ((val != NULL) && (*val != NULL)) trim(*val);
164
  return(0);
165
}