forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMIZip.c
100 lines (78 loc) · 1.74 KB
/
FMIZip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifdef _WIN32
#include <direct.h>
#include <errno.h>
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <process.h>
#include <strsafe.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "miniunzip.h"
#include "FMIZip.h"
const char* FMICreateTemporaryDirectory() {
#ifdef _WIN32
const char* tempfile = _tempnam(NULL, NULL);
if (!tempfile) {
return NULL;
}
BOOL res = CreateDirectoryA(tempfile, 0);
if (!PathFileExistsA(tempfile)) {
free((void*)tempfile);
return NULL;
}
return tempfile;
#else
char template[1024] = "/tmp/fmusim.XXXXXX";
const char* tempdir = mkdtemp(template);
const char* tempdir2 = strdup(tempdir);
return tempdir2;
#endif
}
int FMIPathAppend(char* path, const char* more) {
#ifdef _WIN32
return PathAppendA(path, "modelDescription.xml");
#else
sprintf(path, "%s/%s", path, more);
return 1;
#endif
}
int FMIExtractArchive(const char* filename, const char* unzipdir) {
char cd[2048] = "";
#ifdef _WIN32
if (!_getcwd(cd, 2048)) {
#else
if (!getcwd(cd, 2048)) {
#endif
return 1;
}
const char* argv[6];
argv[0] = "miniunz";
argv[1] = "-x";
argv[2] = "-o";
argv[3] = filename;
argv[4] = "-d";
argv[5] = unzipdir;
const int status = miniunz_main(6, argv);
#ifdef _WIN32
if (_chdir(cd)) {
#else
if (chdir(cd)) {
#endif
return 1;
}
return status;
}
int FMIRemoveDirectory(const char* path) {
char command[4096];
#ifdef _WIN32
snprintf(command, 4096, "rmdir /s /q \"%s\"", path);
#else
snprintf(command, 4096, "rm -rf \"%s\"", path);
#endif
return system(command);
}