24#define _POSIX_C_SOURCE 199506
48#define FILEPATHSEP '/'
60extern double str2num(
const char* s,
int i,
int n) {
62 char str[256], *p = str;
64 if (i < 0 || (
int)strlen(s) < i || (
int)
sizeof(str) - 1 < n) {
67 for (s += i; *s && --n >= 0; s++) {
68 *p++ = *s ==
'd' || *s ==
'D' ?
'E' : *s;
71 return sscanf(str,
"%lf", &value) == 1 ? value : 0.0;
84 trace(NULL, 3,
"execcmd: cmd=%s\n", cmd);
96extern int expath(
const char* path,
char* paths[],
int nmax) {
101 const char* file = path;
102 char dir[1024] =
"", s1[1024], s2[1024], *p, *q, *r;
104 trace(NULL, 3,
"expath : path=%s nmax=%d\n", path, nmax);
106 if ((p = strrchr(path,
'/')) || (p = strrchr(path,
'\\'))) {
108 strncpy(dir, path, p - path + 1);
109 dir[p - path + 1] =
'\0';
111 if (!(dp = opendir(*dir ? dir :
"."))) {
114 while ((d = readdir(dp))) {
115 if (*(d->d_name) ==
'.') {
118 sprintf(s1,
"^%s$", d->d_name);
119 sprintf(s2,
"^%s$", file);
120 for (p = s1; *p; p++) {
121 *p = (char)tolower((
int)*p);
123 for (p = s2; *p; p++) {
124 *p = (char)tolower((
int)*p);
127 for (p = s1, q = strtok_r(s2,
"*", &r); q; q = strtok_r(NULL,
"*", &r)) {
128 if ((p = strstr(p, q))) {
135 sprintf(paths[n++],
"%s%s", dir, d->d_name);
140 for (i = 0; i < n - 1; i++) {
141 for (j = i + 1; j < n; j++) {
142 if (strcmp(paths[i], paths[j]) > 0) {
143 strcpy(tmp, paths[i]);
144 strcpy(paths[i], paths[j]);
145 strcpy(paths[j], tmp);
149 for (i = 0; i < n; i++) {
150 trace(NULL, 3,
"expath : file=%s\n", paths[i]);
164 sprintf(pdir,
"%.1023s", dir);
167 if (!(fp = fopen(pdir,
"r"))) {
175 if (!mkdir(dir, 0777) || errno == EEXIST) {
178 trace(NULL, 2,
"directory generation error: dir=%s\n", dir);
190 tracet(NULL, 3,
"createdir: path=%s\n", path);
201static int repstr(
char* str,
const char* pat,
const char* rep) {
202 int len = (int)strlen(pat);
203 char buff[1024], *p, *q, *r;
205 for (p = str, r = buff; *p; p = q + len) {
206 if (!(q = strstr(p, pat))) {
209 strncpy(r, p, q - p);
211 r += sprintf(r,
"%s", rep);
248extern int reppath(
const char* path,
char* rpath,
gtime_t time,
const char* rov,
const char* base) {
249 double ep[6], ep0[6] = {2000, 1, 1, 0, 0, 0};
250 int week, dow, doy, stat = 0;
255 if (!strstr(rpath,
"%")) {
259 stat |=
repstr(rpath,
"%r", rov);
262 stat |=
repstr(rpath,
"%b", base);
264 if (time.
time != 0) {
267 dow = (int)floor(
time2gpst(time, &week) / 86400.0);
269 sprintf(rep,
"%02d", ((
int)ep[3] / 3) * 3);
270 stat |=
repstr(rpath,
"%ha", rep);
271 sprintf(rep,
"%02d", ((
int)ep[3] / 6) * 6);
272 stat |=
repstr(rpath,
"%hb", rep);
273 sprintf(rep,
"%02d", ((
int)ep[3] / 12) * 12);
274 stat |=
repstr(rpath,
"%hc", rep);
275 sprintf(rep,
"%04.0f", ep[0]);
276 stat |=
repstr(rpath,
"%Y", rep);
277 sprintf(rep,
"%02.0f", fmod(ep[0], 100.0));
278 stat |=
repstr(rpath,
"%y", rep);
279 sprintf(rep,
"%02.0f", ep[1]);
280 stat |=
repstr(rpath,
"%m", rep);
281 sprintf(rep,
"%02.0f", ep[2]);
282 stat |=
repstr(rpath,
"%d", rep);
283 sprintf(rep,
"%02.0f", ep[3]);
284 stat |=
repstr(rpath,
"%h", rep);
285 sprintf(rep,
"%02.0f", ep[4]);
286 stat |=
repstr(rpath,
"%M", rep);
287 sprintf(rep,
"%02.0f", floor(ep[5]));
288 stat |=
repstr(rpath,
"%S", rep);
289 sprintf(rep,
"%03d", doy);
290 stat |=
repstr(rpath,
"%n", rep);
291 sprintf(rep,
"%04d", week);
292 stat |=
repstr(rpath,
"%W", rep);
293 sprintf(rep,
"%d", dow);
294 stat |=
repstr(rpath,
"%D", rep);
295 sprintf(rep,
"%c",
'a' + (
int)ep[3]);
296 stat |=
repstr(rpath,
"%H", rep);
297 sprintf(rep,
"%02d", ((
int)ep[4] / 15) * 15);
298 stat |=
repstr(rpath,
"%t", rep);
299 }
else if (strstr(rpath,
"%ha") || strstr(rpath,
"%hb") || strstr(rpath,
"%hc") || strstr(rpath,
"%Y") ||
300 strstr(rpath,
"%y") || strstr(rpath,
"%m") || strstr(rpath,
"%d") || strstr(rpath,
"%h") ||
301 strstr(rpath,
"%M") || strstr(rpath,
"%S") || strstr(rpath,
"%n") || strstr(rpath,
"%W") ||
302 strstr(rpath,
"%D") || strstr(rpath,
"%H") || strstr(rpath,
"%t")) {
324 double tow, tint = 86400.0;
327 trace(NULL, 3,
"reppaths: path =%s nmax=%d rov=%s base=%s\n", path, nmax, rov, base);
333 if (strstr(path,
"%S") || strstr(path,
"%M") || strstr(path,
"%t")) {
335 }
else if (strstr(path,
"%h") || strstr(path,
"%H")) {
340 time =
gpst2time(week, floor(tow / tint) * tint);
342 while (
timediff(time, te) <= 0.0 && n < nmax) {
343 reppath(path, rpath[n], time, rov, base);
344 if (n == 0 || strcmp(rpath[n], rpath[n - 1])) {
349 for (i = 0; i < n; i++) {
350 trace(NULL, 3,
"reppaths: rpath=%s\n", rpath[i]);
369 char *p, cmd[64 + 2048] =
"", tmpfile[1024] =
"", buff[1024], *fname, *dir =
"";
371 trace(NULL, 3,
"rtk_uncompress: file=%s\n", file);
373 strcpy(tmpfile, file);
374 if (!(p = strrchr(tmpfile,
'.'))) {
379 if (!strcmp(p,
".z") || !strcmp(p,
".Z") || !strcmp(p,
".gz") || !strcmp(p,
".GZ") || !strcmp(p,
".zip") ||
380 !strcmp(p,
".ZIP")) {
381 strcpy(uncfile, tmpfile);
382 uncfile[p - tmpfile] =
'\0';
383 sprintf(cmd,
"gzip -f -d -c \"%s\" > \"%s\"", tmpfile, uncfile);
389 strcpy(tmpfile, uncfile);
393 if ((p = strrchr(tmpfile,
'.')) && !strcmp(p,
".tar")) {
394 strcpy(uncfile, tmpfile);
395 uncfile[p - tmpfile] =
'\0';
396 strcpy(buff, tmpfile);
398 if ((p = strrchr(buff,
'/'))) {
403 sprintf(cmd,
"tar -C \"%s\" -xf \"%s\"", dir, tmpfile);
416 else if ((p = strrchr(tmpfile,
'.')) &&
417 ((strlen(p) > 3 && (*(p + 3) ==
'd' || *(p + 3) ==
'D')) || !strcmp(p,
".crx") || !strcmp(p,
".CRX"))) {
418 strcpy(uncfile, tmpfile);
419 uncfile[p - tmpfile + 3] = *(p + 3) ==
'D' ?
'O' :
'o';
420 sprintf(cmd,
"crx2rnx < \"%s\" > \"%s\"", tmpfile, uncfile);
434 trace(NULL, 3,
"rtk_uncompress: stat=%d\n", stat);
int expath(const char *path, char *paths[], int nmax)
Expand file path with wild-card (*) in file.
int rtk_uncompress(const char *file, char *uncfile)
Uncompress file (gzip/zip/tar/Hatanaka).
int execcmd(const char *cmd)
Execute command line by operating system shell.
void createdir(const char *path)
Create directory if not exists (recursively).
int reppath(const char *path, char *rpath, gtime_t time, const char *rov, const char *base)
Replace keywords in file path with date, time, rover and base station id.
static int mkdir_r(const char *dir)
double str2num(const char *s, int i, int n)
Convert substring in string to number.
int reppaths(const char *path, char *rpath[], int nmax, gtime_t ts, gtime_t te, const char *rov, const char *base)
Replace keywords in file path and generate multiple paths.
static int repstr(char *str, const char *pat, const char *rep)
MRTKLIB System Module — OS/filesystem utility functions.
gtime_t epoch2time(const double *ep)
Convert calendar day/time to gtime_t struct.
gtime_t gpst2time(int week, double sec)
Convert GPS week/tow to gtime_t.
void time2epoch(gtime_t t, double *ep)
Convert gtime_t struct to calendar day/time.
double timediff(gtime_t t1, gtime_t t2)
Compute time difference (t1 - t2).
double time2gpst(gtime_t t, int *week)
Convert gtime_t to GPS week/tow.
gtime_t timeadd(gtime_t t, double sec)
Add seconds to gtime_t.
void tracet(mrtk_ctx_t *ctx, int level, const char *format,...)
Output trace message with elapsed time.
void trace(mrtk_ctx_t *ctx, int level, const char *format,...)
Output trace message.
MRTKLIB Trace Module — debug trace and logging functions.
GNSS time representation.