static const char* ModelicaInternal_fullPathName(const char* name) { /* Get full path name of file or directory */ char* fullName; #if defined(_WIN32) char* tempName = _fullpath(buffer, name, sizeof(buffer)); if (tempName == NULL) { ModelicaFormatError("Not possible to construct full path name of \"%s\"\n%s", name, strerror(errno)); return ""; } fullName = ModelicaAllocateString(strlen(tempName)); strcpy(fullName, tempName); ModelicaConvertToUnixDirectorySeparator(fullName); #else /* No such system call in _POSIX_ available */ char* cwd = getcwd(buffer, sizeof(buffer)); if (cwd == NULL) { ModelicaFormatError("Not possible to get current working directory:\n%s", strerror(errno)); } fullName = ModelicaAllocateString(strlen(cwd) + strlen(name) + 1); strcpy(fullName, cwd); strcat(fullName, "/"); strcat(fullName, name); #endif return fullName; }
void ModelicaStrings_scanIdentifier(_In_z_ const char* string, int startIndex, _Out_ int* nextIndex, _Out_ const char** identifier) { int token_start = ModelicaStrings_skipWhiteSpace(string, startIndex); /* Index of first char of token, after ws. */ if (isalpha((unsigned char)string[token_start-1])) { /* Identifier has begun. */ int token_length = 1; while (string[token_start+token_length-1] != '\0' && (isalpha((unsigned char)string[token_start+token_length-1]) || isdigit((unsigned char)string[token_start+token_length-1]) || string[token_start+token_length-1] == '_')) { ++token_length; } { char* s = ModelicaAllocateString((size_t)token_length); strncpy(s, string+token_start-1, (size_t)token_length); s[token_length] = '\0'; *nextIndex = token_start + token_length; *identifier = s; return; } } /* Token missing or not identifier. */ *nextIndex = startIndex; *identifier = ModelicaAllocateString(0); return; }
extern const char* Socket_handlerequest(int sock) { char *str = SocketImpl_handlerequest(sock); char *res = strcpy(ModelicaAllocateString(strlen(str)),str); free(str); return res; }
static const char* ModelicaInternal_getcwd(int dummy) { const char* cwd; char* directory; #if defined(_WIN32) cwd = _getcwd(buffer, sizeof(buffer)); #elif defined(_POSIX_) cwd = getcwd(buffer, sizeof(buffer)); #else ModelicaNotExistError("ModelicaInternal_getcwd"); cwd = ""; #endif if (cwd == NULL) { ModelicaFormatError("Not possible to get current working directory:\n%s", strerror(errno)); cwd = ""; } directory = ModelicaAllocateString(strlen(cwd)); strcpy(directory, cwd); ModelicaConvertToUnixDirectorySeparator(directory); return directory; }
const char* UnitParserExt_unit2str(void *nums, void *denoms, void *tpnoms, void *tpdenoms, void *tpstrs) { long int i1,i2; string tpParam; Unit unit; unit.unitVec.clear(); unit.typeParamVec.clear(); /* Add baseunits*/ while(MMC_GETHDR(nums) == MMC_CONSHDR) { i1 = MMC_UNTAGFIXNUM(MMC_CAR(nums)); i2 = MMC_UNTAGFIXNUM(MMC_CAR(denoms)); unit.unitVec.push_back(Rational(i1,i2)); nums = MMC_CDR(nums); denoms = MMC_CDR(denoms); } /* Add type parameters*/ while(MMC_GETHDR(tpnoms) == MMC_CONSHDR) { i1 = MMC_UNTAGFIXNUM(MMC_CAR(tpnoms)); i2 = MMC_UNTAGFIXNUM(MMC_CAR(tpdenoms)); tpParam = string(MMC_STRINGDATA(MMC_CAR(tpstrs))); unit.typeParamVec.insert(std::pair<string,Rational>(tpParam,Rational(i1,i2))); tpnoms = MMC_CDR(tpnoms); tpdenoms = MMC_CDR(tpdenoms); } //string res = unitParser->unit2str(unit); string res = unitParser->prettyPrintUnit2str(unit); return strcpy(ModelicaAllocateString(res.size()), res.c_str()); }
void ModelicaMessage(const char* string) { PFUNC_ModelicaMessage ITI_ModelicaMessage = NULL; HINSTANCE hInstModelicaExternalC = LoadLibrary(TEXT("ModelicaExternalC.dll")); if (hInstModelicaExternalC) { ITI_ModelicaMessage = (PFUNC_ModelicaMessage) GetProcAddress(hInstModelicaExternalC, "ModelicaMessage"); } if (ITI_ModelicaMessage) { char* buf = ModelicaAllocateString(strlen(string) + 1); if (buf) { strcpy(buf, string); Trim(buf); ITI_ModelicaMessage(buf); } else { printf(string); } } else { printf(string); } if (hInstModelicaExternalC) { FreeLibrary(hInstModelicaExternalC); hInstModelicaExternalC = NULL; ITI_ModelicaMessage = NULL; } }
void ModelicaTrace(int tab, int kind, const char* caption, const char* text) { PFUNC_ModelicaTrace ITI_ModelicaTrace = NULL; HINSTANCE hInstGlobalHlp = LoadLibrary(TEXT("globalHlp.dll")); if (hInstGlobalHlp) { ITI_ModelicaTrace = (PFUNC_ModelicaTrace) GetProcAddress(hInstGlobalHlp, "OutputTraceMessageA"); } if (ITI_ModelicaTrace) { char* buf = ModelicaAllocateString(strlen(text) + 1); if (buf) { strcpy(buf, text); Trim(buf); ITI_ModelicaTrace(tab, kind, caption, buf); } else { printf("%s: %s\n", caption, text); } } else { printf("%s: %s\n", caption, text); } if (hInstGlobalHlp) { FreeLibrary(hInstGlobalHlp); hInstGlobalHlp = NULL; ITI_ModelicaTrace = NULL; } }
MODELICA_EXPORT const char* ModelicaStrings_substring(const char* string, int startIndex, int endIndex) { /* Return string1(startIndex:endIndex) if endIndex >= startIndex, or return string1(startIndex:startIndex), if endIndex = 0. An assert is triggered, if startIndex/endIndex are not valid. */ char* substring; int len1 = (int) strlen(string); int len2; /* Check arguments */ if ( startIndex < 1 ) { ModelicaFormatError("Wrong call of Utilities.Strings.substring:\n" " startIndex = %d (has to be > 0).\n" " string = \"%s\"\n", startIndex, string); } else if ( endIndex == -999 ) { endIndex = startIndex; } else if ( endIndex < startIndex ) { ModelicaFormatError("Wrong call of Utilities.Strings.substring:\n" " startIndex = %d\n" " endIndex = %d (>= startIndex required)\n" " string = \"%s\"\n", startIndex, endIndex, string); } else if ( endIndex > len1 ) { ModelicaFormatError("Wrong call of Utilities.Strings.substring:\n" " endIndex = %d (<= %d required (=length(string)).\n" " string = \"%s\"\n", endIndex, len1, string); }; /* Allocate memory and copy string */ len2 = endIndex - startIndex + 1; substring = ModelicaAllocateString(len2); strncpy(substring, &string[startIndex-1], len2); substring[len2] = '\0'; return substring; }
static const char* ModelicaInternal_getenv(const char* name, int convertToSlash, int* exist) { /* Get content of environment variable */ char* value = getenv(name); char* result; if (value == NULL) { result = ModelicaAllocateString(0); result[0] = '\0'; *exist = 0; } else { result = ModelicaAllocateString(strlen(value)); strcpy(result, value); if ( convertToSlash == 1 ) ModelicaConvertToUnixDirectorySeparator(result); *exist = 1; } return result; }
_Ret_z_ const char* ModelicaInternal_fullPathName(_In_z_ const char* name) { /* Get full path name of file or directory */ #if defined(_WIN32) || (_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || (_POSIX_VERSION >= 200112L)) char* fullName; char localbuf[BUFFER_LENGTH]; #if (_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || _POSIX_VERSION >= 200112L) /* realpath availability: 4.4BSD, POSIX.1-2001. Using the behaviour of NULL: POSIX.1-2008 */ char* tempName = realpath(name, localbuf); #else char* tempName = _fullpath(localbuf, name, sizeof(localbuf)); #endif if (tempName == NULL) { ModelicaFormatError("Not possible to construct full path name of \"%s\"\n%s", name, strerror(errno)); return ""; } fullName = ModelicaAllocateString(strlen(tempName)); strcpy(fullName, tempName); ModelicaConvertToUnixDirectorySeparator(fullName); #elif defined(_POSIX_) char* fullName; char localbuf[BUFFER_LENGTH]; /* No such system call in _POSIX_ available (except realpath above) */ char* cwd = getcwd(localbuf, sizeof(localbuf)); if (cwd == NULL) { ModelicaFormatError("Not possible to get current working directory:\n%s", strerror(errno)); } fullName = ModelicaAllocateString(strlen(cwd) + strlen(name) + 1); if (name[0] != '/') { /* Any name beginning with "/" is regarded as already being a full path. */ strcpy(fullName, cwd); strcat(fullName, "/"); } strcat(fullName, name); #else char* fullName = ""; ModelicaNotExistError("ModelicaInternal_fullPathName"); #endif return fullName; }
void ModelicaStrings_scanString(_In_z_ const char* string, int startIndex, _Out_ int* nextIndex, _Out_ const char** result) { int i, token_start, past_token, token_length; token_length = 0; token_start = ModelicaStrings_skipWhiteSpace(string, startIndex); i = token_start; if (string[token_start-1] != '"') { goto Modelica_ERROR; } /* Index of first char of token, after ws. */ ++i; while (1) { if (string[i-1] == '\0') { goto Modelica_ERROR; } if (string[i-2] == '\\' && string[i-1] == '"') ; /* escaped quote, consume */ else if (string[i-1] == '"') { break; /* end quote */ } ++i; } past_token = i + 1; /* Index of first char after token, ws or separator. */ token_length = past_token-token_start-2; if (token_length > 0) { char* s = ModelicaAllocateString((size_t)token_length); strncpy(s, string+token_start, (size_t)token_length); s[token_length] = '\0'; *result = s; *nextIndex = past_token; return; } Modelica_ERROR: *result = ModelicaAllocateString(0); *nextIndex = startIndex; return; }
static const char* ModelicaInternal_readLine(const char* fileName, int lineNumber, int* endOfFile) { /* Read line lineNumber from file fileName */ FILE* fp = ModelicaStreams_openFileForReading(fileName); char* line; int c; size_t lineLen; size_t iLines; long offset; /* Read upto line lineNumber-1 */ iLines = 0; c = 1; while ( iLines != (size_t) lineNumber-1 && c != EOF ) { c = fgetc(fp); while ( c != '\n' && c != EOF ) { c = fgetc(fp); } iLines++; } if ( iLines != (size_t) lineNumber-1 ) goto END_OF_FILE; /* Determine length of line lineNumber */ offset = ftell(fp); lineLen = 0; c = fgetc(fp); while ( c != '\n' && c != EOF ) { lineLen++; c = fgetc(fp); } if ( lineLen == 0 && c == EOF ) goto END_OF_FILE; /* Read line lineNumber */ line = ModelicaAllocateStringWithErrorReturn(lineLen); if ( line == NULL ) goto ERROR; if ( fseek(fp, offset, SEEK_SET != 0) ) goto ERROR; if ( fread(line, sizeof(char), lineLen, fp) != lineLen ) goto ERROR; fclose(fp); line[lineLen] = '\0'; *endOfFile = 0; return line; /* End-of-File or error */ END_OF_FILE: fclose(fp); *endOfFile = 1; line = ModelicaAllocateString(0); return line; ERROR : fclose(fp); ModelicaFormatError("Error when reading line %i from file\n\"%s\":\n%s", lineNumber, fileName, strerror(errno)); return ""; }
void ModelicaVFormatMessage(const char* string, va_list args) { PFUNC_ModelicaMessage ITI_ModelicaMessage = NULL; HINSTANCE hInstModelicaExternalC = LoadLibrary(TEXT("ModelicaExternalC.dll")); if (hInstModelicaExternalC) { ITI_ModelicaMessage = (PFUNC_ModelicaMessage) GetProcAddress(hInstModelicaExternalC, "ModelicaMessage"); } if (ITI_ModelicaMessage) { size_t count = 128; char* buf = NULL; int charsWritten; buf = (char*) malloc((count + 1)*sizeof(char)); while ((charsWritten = vsnprintf(buf, count, string, args)) == -1) { count *= 2; buf = (char*) realloc((void*) buf, (count + 1)*sizeof(char)); } buf[charsWritten] = '\0'; count = strlen(buf) + 1; char* buf2 = ModelicaAllocateString(count); if (buf2) { strcpy(buf2, buf); free(buf); Trim(buf2); ITI_ModelicaMessage(buf2); } else { free(buf); vprintf(string, args); } } else { vprintf(string, args); } if (hInstModelicaExternalC) { FreeLibrary(hInstModelicaExternalC); hInstModelicaExternalC = NULL; ITI_ModelicaMessage = NULL; } }
_Ret_z_ const char* ModelicaInternal_temporaryFileName(void) { /* Get full path name of a temporary file name which does not exist */ char* fullName; char* tempName = tmpnam(NULL); if (tempName == NULL) { ModelicaFormatError("Not possible to get temporary filename\n%s", strerror(errno)); return ""; } fullName = ModelicaAllocateString(strlen(tempName)); strcpy(fullName, tempName); ModelicaConvertToUnixDirectorySeparator(fullName); return fullName; }
const char* ED_getStringFromXML(void* _xml, const char* varName) { XMLFile* xml = (XMLFile*)_xml; if (xml != NULL) { XmlNodeRef root = xml->root; char* token = findValue(&root, varName, xml->fileName); if (token != NULL) { char* ret = ModelicaAllocateString(strlen(token)); strcpy(ret, token); return (const char*)ret; } else { ModelicaFormatError("Error in line %i when reading value from file \"%s\"\n", XmlNode_getLine(root), xml->fileName); } } return ""; }
extern const char* Settings_getCompileCommand() { const char *res = SettingsImpl__getCompileCommand(); return strcpy(ModelicaAllocateString(strlen(res)), res); }