Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | 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
/* detect local paths (eg. C:\REPO). Returns 1 if the url looks like a local path, zero otherwise. */
126
int detect_localpath(char *url) {
127
  if (url[0] != 0) {
128
    if (url[1] != 0) {
129
      if ((url[1] == ':') && ((url[2] == '\\') || (url[2] == '/'))) return(1);
130
    }
131
  }
132
  return(0);
133
}
134
 
135
 
136
/* analyzes a filename string and returns the pointer to the file's extension
137
 * (which can be empty) */
138
char *getfext(char *fname) {
139
  char *res = NULL;
140
  for (; *fname != 0; fname++) {
141
    if (*fname == '.') res = fname + 1;
142
  }
143
  /* if no dot found, then point to the string's null terminator */
144
  if (res == NULL) return(fname);
145
  return(res);
146
}
248 mateuszvis 147
 
148
 
149
/* reads a line from a "token = value" file, returns 0 on success
150
 * val (if not NULL) is updated with a pointer to the "value" part
151
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
152
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
153
  int bytebuff, linelen = 0;
154
  if (val != NULL) *val = NULL;
155
  for (;;) {
156
    bytebuff = fgetc(fd);
157
    if (bytebuff == EOF) {
158
      if (linelen == 0) return(-1);
159
      break;
160
    }
161
    if (bytebuff < 0) return(-1);
162
    if ((*val == NULL) && (bytebuff == delim)) {
163
      line[linelen++] = 0;
164
      *val = line + linelen;
165
      continue;
166
    }
167
    if (bytebuff == '\r') continue; /* ignore CR */
168
    if (bytebuff == '\n') break;
169
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
170
  }
171
  /* terminate line and trim token and value (if any) */
172
  line[linelen] = 0;
173
  trim(line);
174
  if ((val != NULL) && (*val != NULL)) trim(*val);
175
  return(0);
176
}