/
System_omc.c
689 lines (599 loc) · 14.9 KB
/
System_omc.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#if defined(_MSC_VER)
#include <Windows.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#include <ctype.h> /* for toupper */
#include "openmodelica.h"
#include "meta_modelica.h"
#include "rml_compatibility.h"
#define ADD_METARECORD_DEFINTIONS static
#include "OpenModelicaBootstrappingHeader.h"
#include "systemimpl.c"
extern void System_writeFile(const char* filename, const char* data)
{
if (SystemImpl__writeFile(filename, data))
MMC_THROW();
}
extern void System_appendFile(const char *filename, const char *data)
{
if (SystemImpl__appendFile(filename, data))
MMC_THROW();
}
extern int System_removeFile(const char* filename)
{
return SystemImpl__removeFile(filename);
}
extern char* System_readFile(const char* filename)
{
return SystemImpl__readFile(filename);
}
extern const char* System_stringReplace(const char* str, const char* source, const char* target)
{
char* res = _replace(str,source,target);
if (res == NULL)
MMC_THROW();
return res;
}
extern int System_stringFind(const char* str, const char* searchStr)
{
const char *found = strstr(str, searchStr);
if (found == NULL)
return -1;
else
return found-str;
}
extern const char* System_stringFindString(const char* str, const char* searchStr)
{
const char *found = strstr(str, searchStr);
if (found == NULL)
MMC_THROW();
return strdup(found);
}
extern void System_realtimeTick(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_tick(ix);
}
extern double System_realtimeTock(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_tock(ix);
}
extern void System_realtimeClear(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_clear(ix);
}
extern int System_realtimeNtick(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_ncall(ix);
}
extern const char* System_getRTLibs()
{
return LDFLAGS_RT;
}
extern const char* System_getCCompiler()
{
return strdup(cc);
}
extern const char* System_getCXXCompiler()
{
return strdup(cxx);
}
extern const char* System_getLinker()
{
return strdup(linker);
}
extern const char* System_getLDFlags()
{
return strdup(ldflags);
}
extern const char* System_getCFlags()
{
return strdup(cflags);
}
extern const char* System_getExeExt()
{
return CONFIG_EXE_EXT;
}
extern const char* System_getDllExt()
{
return CONFIG_DLL_EXT;
}
extern const char* System_os()
{
return CONFIG_OS;
}
extern const char* System_trim(const char* str, const char* chars_to_remove)
{
return SystemImpl__trim(str,chars_to_remove);
}
extern const char* System_trimWhitespace(const char* str)
{
return SystemImpl__trim(str," \f\n\r\t\v");
}
extern const char* System_trimChar(const char* str, const char* char_to_remove)
{
if (!char_to_remove[0] || char_to_remove[1])
MMC_THROW();
return MMC_STRINGDATA(SystemImpl__trimChar(str,*char_to_remove));
}
extern const char* System_basename(const char* str)
{
return strdup(SystemImpl__basename(str));
}
extern const char* System_dirname(const char* str)
{
char *cpy = strdup(str);
char *res = NULL;
#if defined(_MSC_VER)
char drive[_MAX_DRIVE], dir[_MAX_DIR], filename[_MAX_FNAME], extension[_MAX_EXT];
_splitpath(str, drive, dir, filename, extension);
sprintf(cpy, "%s/%s/",drive,dir);
res = strdup(cpy);
#else
res = strdup(dirname(cpy));
#endif
free(cpy);
return res;
}
extern const char* System_configureCommandLine()
{
return CONFIGURE_COMMANDLINE;
}
extern const char* System_platform()
{
return CONFIG_PLATFORM;
}
extern const char* System_pathDelimiter()
{
return CONFIG_PATH_DELIMITER;
}
extern const char* System_groupDelimiter()
{
return CONFIG_GROUP_DELIMITER;
}
extern int System_strncmp(const char *str1, const char *str2, int len)
{
int res= strncmp(str1,str2,len);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}
extern int System_strcmp(const char *str1, const char *str2)
{
int res = strcmp(str1,str2);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}
extern int System_getHasExpandableConnectors()
{
return hasExpandableConnectors;
}
extern void System_setHasExpandableConnectors(int b)
{
hasExpandableConnectors = b;
}
extern int System_getPartialInstantiation()
{
return isPartialInstantiation;
}
extern void System_setPartialInstantiation(int b)
{
isPartialInstantiation = b;
}
extern int System_getHasInnerOuterDefinitions()
{
return hasInnerOuterDefinitions;
}
extern void System_setHasInnerOuterDefinitions(int b)
{
hasInnerOuterDefinitions = b;
}
extern int System_getHasStreamConnectors()
{
return hasStreamConnectors;
}
extern void System_setHasStreamConnectors(int b)
{
hasStreamConnectors = b;
}
extern int System_getUsesCardinality()
{
return usesCardinality;
}
extern void System_setUsesCardinality(int b)
{
usesCardinality = b;
}
extern void* System_strtok(const char *str0, const char *delimit)
{
char *s;
void *res = mmc_mk_nil();
char *str = strdup(str0);
s=strtok(str,delimit);
if (s == NULL)
{
free(str);
return res;
}
res = mmc_mk_cons(mmc_mk_scon(s),res);
while ((s=strtok(NULL,delimit)))
{
res = mmc_mk_cons(mmc_mk_scon(s),res);
}
free(str);
return listReverse(res);
}
extern char* System_substring(const char *str, int start, int stop)
{
static char* substring = NULL; // This function is not re-entrant... Note that the result will be overwritten at each call to substring...
int startIndex = start;
int stopIndex = stop;
int len1 = strlen(str);
int len2 = 0;
void *res = NULL;
/* Check arguments */
if ( startIndex < 1 )
{
MMC_THROW();
}
if ( stopIndex == -999 )
{
stopIndex = startIndex;
} else if ( stopIndex < startIndex ) {
MMC_THROW();
} else if ( stopIndex > len1 ) {
MMC_THROW();
}
/* Allocate memory and copy string */
if (substring) free(substring);
len2 = stopIndex - startIndex + 1;
substring = (char*)malloc(len2+1);
strncpy(substring, &str[startIndex-1], len2);
substring[len2] = '\0';
return substring;
}
extern char* System_toupper(const char *str)
{
int i;
char* strToUpper = strdup(str);
for (i = 0; i < strlen(strToUpper); i++)
{
strToUpper[i] = toupper(strToUpper[i]);
}
return strToUpper;
}
extern char* System_tolower(const char *str)
{
int i;
char* strToLower = strdup(str);
for (i = 0; i < strlen(strToLower); i++)
{
strToLower[i] = tolower(strToLower[i]);
}
return strToLower;
}
const char* System_getClassnamesForSimulation()
{
if(class_names_for_simulation)
return strdup(class_names_for_simulation);
else
return "{}";
}
void System_setClassnamesForSimulation(const char *class_names)
{
if(class_names_for_simulation)
free(class_names_for_simulation);
class_names_for_simulation = strdup(class_names);
}
extern double System_getVariableValue(double _timeStamp, void* _timeValues, void* _varValues)
{
double res;
if (SystemImpl__getVariableValue(_timeStamp,_timeValues,_varValues,&res))
MMC_THROW();
return res;
}
extern void* System_getFileModificationTime(const char *fileName)
{
struct stat attrib; // create a file attribute structure
double elapsedTime; // the time elapsed as double
int result; // the result of the function call
if (stat( fileName, &attrib ) != 0) {
return mmc_mk_none();
} else {
return mmc_mk_some(mmc_mk_rcon(difftime(attrib.st_mtime, 0))); // the file modification time
}
}
#if defined(__MINGW32__) || defined(_MSC_VER)
void* System_moFiles(const char *directory)
{
void *res;
WIN32_FIND_DATA FileData;
BOOL more = TRUE;
char pattern[1024];
HANDLE sh;
sprintf(pattern, "%s\\*.mo", directory);
res = mmc_mk_nil();
sh = FindFirstFile(pattern, &FileData);
if (sh != INVALID_HANDLE_VALUE) {
while(more) {
if (strcmp(FileData.cFileName,"package.mo") != 0)
{
res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
}
more = FindNextFile(sh, &FileData);
}
if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
}
return res;
}
#else
void* System_moFiles(const char *directory)
{
int i,count;
void *res;
struct dirent **files;
select_from_dir = directory;
count = scandir(directory, &files, file_select_mo, NULL);
res = mmc_mk_nil();
for (i=0; i<count; i++)
{
res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
free(files[i]);
}
return res;
}
#endif
extern int System_lookupFunction(int _inLibHandle, const char* _inFunc)
{
int res = SystemImpl__lookupFunction(_inLibHandle, _inFunc);
if (res == -1) MMC_THROW();
return res;
}
extern void System_freeFunction(int _inFuncHandle, int printDebug)
{
if (SystemImpl__freeFunction(_inFuncHandle, printDebug)) MMC_THROW();
}
extern void System_freeLibrary(int _inLibHandle, int printDebug)
{
if (SystemImpl__freeLibrary(_inLibHandle, printDebug)) MMC_THROW();
}
extern int System_userIsRoot()
{
return CONFIG_USER_IS_ROOT;
}
extern const char* System_readEnv(const char *envname)
{
char *envvalue = getenv(envname);
if (envvalue == NULL) MMC_THROW();
return strdup(envvalue);
}
extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday, int* mon, int* year)
{
time_t t;
struct tm* localTime;
time( &t );
localTime = localtime(&t);
*sec = localTime->tm_sec;
*min = localTime->tm_min;
*hour = localTime->tm_hour;
*mday = localTime->tm_mday;
*mon = localTime->tm_mon + 1;
*year = localTime->tm_year + 1900;
}
extern const char* System_getUUIDStr()
{
return strdup(SystemImpl__getUUIDStr());
}
extern int System_loadLibrary(const char *name, int printDebug)
{
int res = SystemImpl__loadLibrary(name, printDebug);
if (res == -1) MMC_THROW();
return res;
}
#if defined(__MINGW32__) || defined(_MSC_VER)
void* System_subDirectories(const char *directory)
{
void *res;
WIN32_FIND_DATA FileData;
BOOL more = TRUE;
char pattern[1024];
HANDLE sh;
sprintf(pattern, "%s\\*.*", directory);
res = mmc_mk_nil();
sh = FindFirstFile(pattern, &FileData);
if (sh != INVALID_HANDLE_VALUE) {
while(more) {
if (strcmp(FileData.cFileName,"..") != 0 &&
strcmp(FileData.cFileName,".") != 0 &&
(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
}
more = FindNextFile(sh, &FileData);
}
if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
}
return res;
}
#else
void* System_subDirectories(const char *directory)
{
int i,count;
void *res;
struct dirent **files;
select_from_dir = directory;
count = scandir(directory, &files, file_select_directories, NULL);
res = mmc_mk_nil();
for (i=0; i<count; i++)
{
res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
free(files[i]);
}
return res;
}
#endif
extern const char* System_getCorbaLibs()
{
return CONFIG_CORBALIBS;
}
extern void* System_getRuntimeLibs()
{
return CONFIG_SYSTEMLIBS;
}
extern void* System_regex(const char* str, const char* re, int maxn, int extended, int sensitive, int *nmatch)
{
*nmatch = 0;
void *res = SystemImpl__regex(str,re,maxn,extended,sensitive,nmatch);
if (res==NULL) MMC_THROW();
return res;
}
extern int System_regexModelica(const char* str, const char* re, int maxn, int extended, int sensitive, char **matches)
{
int nmatch = 0, i=0;
void *res = SystemImpl__regex(str,re,maxn,extended,sensitive,&nmatch);
if (res==NULL) MMC_THROW();
for (i=0; i<maxn; i++) {
char *tmp = MMC_STRINGDATA(MMC_CAR(res));
matches[i] = alloc_modelica_string(strlen(tmp));
strcpy(matches[i],tmp);
res = MMC_CDR(res);
}
return nmatch;
}
extern char* System_escapedString(char* str, int nl)
{
char *res = omc__escapedString(str,nl);
if (res == NULL) return str;
return res;
}
extern char* System_unescapedString(char* str)
{
char *res = SystemImpl__unescapedString(str);
if (res == NULL) return str;
return res;
}
extern char* System_unquoteIdentifier(char *str)
{
char *res = SystemImpl__unquoteIdentifier(str);
if (res == NULL) return str;
return res;
}
extern char* System_getCurrentTimeStr()
{
time_t t;
struct tm* localTime;
char * dateStr;
time( &t );
localTime = localtime(&t);
dateStr = asctime(localTime);
if (dateStr)
return dateStr;
MMC_THROW();
}
void System_resetTimer()
{
/* reset the timer */
timerIntervalTime = 0;
timerCummulatedTime = 0;
timerTime = 0;
timerStackIdx = 0;
}
void System_startTimer()
{
/* start the timer if not already started */
if (!timerStackIdx)
{
rt_tick(RT_CLOCK_SPECIAL_STOPWATCH);
}
pushTimerStack();
}
void System_stopTimer()
{
popTimerStack();
}
double System_getTimerIntervalTime()
{
return timerIntervalTime;
}
double System_getTimerCummulatedTime()
{
return timerCummulatedTime;
}
extern void System_uriToClassAndPath(const char *uri, const char **scheme, char **name, char **path)
{
int res = SystemImpl__uriToClassAndPath(uri, scheme, name, path);
// TODO: Fix memory leak by using the external interface
if (res) MMC_THROW();
}
extern const char* System_modelicaPlatform()
{
return CONFIG_MODELICA_SPEC_PLATFORM;
}
extern const char* System_openModelicaPlatform()
{
return CONFIG_OPENMODELICA_SPEC_PLATFORM;
}
extern void System_getGCStatus(int *used, int *allocated)
{
*used = -1;
*allocated = -1;
}
extern void System_getLoadModelPath(const char *className, void *prios, void *mps, const char **dir, char **name, int *isDir)
{
*name = NULL;
if (SystemImpl__getLoadModelPath(className,prios,mps,dir,name,isDir)) MMC_THROW();
/* TODO: Do not strdup in parent... */
}
extern const char* System_getMakeCommand()
{
return DEFAULT_MAKE;
}
extern const char* System_snprintff(const char *fmt, int len, double d)
{
static char buf[1024] /* It's ok to keep me static because snprintf is not reentrant either */;
assert(1024>len);
snprintf(buf,len,fmt,d);
buf[1023] = 0;
return buf;
}
#ifdef __cplusplus
}
#endif