metamodelica_string_const stringAppend(metamodelica_string_const s1, metamodelica_string_const s2) { unsigned len1 = 0, len2 = 0, nbytes = 0, header = 0, nwords = 0; void *res = NULL; struct mmc_string *p = NULL; MMC_CHECK_STRING(s1); MMC_CHECK_STRING(s2); /* fprintf(stderr, "stringAppend([%p] %s, [%p] %s)->\n", s1, anyString(s1), s2, anyString(s2)); fflush(NULL); */ len1 = MMC_STRLEN(s1); len2 = MMC_STRLEN(s2); nbytes = len1+len2; header = MMC_STRINGHDR(nbytes); nwords = MMC_HDRSLOTS(header) + 1; p = (struct mmc_string *) mmc_alloc_words_atomic(nwords); /* fprintf(stderr, "at address %p\n", MMC_TAGPTR(p)); fflush(NULL); */ p->header = header; memcpy(p->data, MMC_STRINGDATA(s1), len1); memcpy(p->data + len1, MMC_STRINGDATA(s2), len2 + 1); res = MMC_TAGPTR(p); MMC_CHECK_STRING(res); /* fprintf(stderr, "-> %s\n", anyString(res)); fflush(NULL); */ return res; }
modelica_metatype boxptr_stringUpdateStringChar(threadData_t *threadData,metamodelica_string str, metamodelica_string c, modelica_metatype iix) { int ix = MMC_UNTAGFIXNUM(iix); int length = 0; unsigned header = MMC_GETHDR(str); unsigned nwords = MMC_HDRSLOTS(header) + 1; struct mmc_string *p = NULL; void *res = NULL; MMC_CHECK_STRING(str); MMC_CHECK_STRING(c); /* fprintf(stderr, "stringUpdateStringChar(%s,%s,%ld)\n", MMC_STRINGDATA(str),MMC_STRINGDATA(c),ix); */ if (ix < 1 || MMC_STRLEN(c) != 1) MMC_THROW_INTERNAL(); length = MMC_STRLEN(str); if (ix > length) MMC_THROW_INTERNAL(); p = (struct mmc_string *) mmc_alloc_words_atomic(nwords); p->header = header; memcpy(p->data, MMC_STRINGDATA(str), length+1 /* include NULL */); p->data[ix-1] = MMC_STRINGDATA(c)[0]; res = MMC_TAGPTR(p); MMC_CHECK_STRING(res); return res; }
modelica_string stringAppend(modelica_string s1, modelica_string s2) { unsigned len1 = 0, len2 = 0, nbytes = 0, header = 0, nwords = 0; void *res = NULL; struct mmc_string *p = NULL; MMC_CHECK_STRING(s1); MMC_CHECK_STRING(s2); /* fprintf(stderr, "stringAppend([%p] %s, [%p] %s)->\n", s1, anyString(s1), s2, anyString(s2)); fflush(NULL); */ len1 = MMC_STRLEN(s1); len2 = MMC_STRLEN(s2); if (len1==0) { return s2; } else if (len2==0) { return s1; } nbytes = len1+len2; res = mmc_alloc_scon(nbytes); memcpy(MMC_STRINGDATA(res), MMC_STRINGDATA(s1), len1); memcpy(MMC_STRINGDATA(res) + len1, MMC_STRINGDATA(s2), len2 + 1); MMC_CHECK_STRING(res); return res; }
metamodelica_string stringAppendList(modelica_metatype lst) { /* fprintf(stderr, "stringAppendList(%s)\n", anyString(lst)); */ modelica_integer lstLen = 0, len = 0; unsigned nbytes = 0, header = 0, nwords = 0; modelica_metatype car = NULL, lstHead = NULL, lstTmp = NULL; char *tmp = NULL; struct mmc_string *res = NULL; void *p = NULL; lstLen = 0; nbytes = 0; lstHead = lst; lstTmp = lst; while (!listEmpty(lstTmp)) { MMC_CHECK_STRING(MMC_CAR(lstTmp)); nbytes += MMC_STRLEN(MMC_CAR(lstTmp)); /* fprintf(stderr, "stringAppendList: Has success reading input %d: %s\n", lstLen, MMC_STRINGDATA(MMC_CAR(lst))); */ lstTmp = MMC_CDR(lstTmp); lstLen++; } if (nbytes == 0) return mmc_emptystring; if (lstLen == 1) return MMC_CAR(lstHead); header = MMC_STRINGHDR(nbytes); nwords = MMC_HDRSLOTS(header) + 1; res = (struct mmc_string *) mmc_alloc_words_atomic(nwords); res->header = header; tmp = (char*) res->data; nbytes = 0; lstTmp = lstHead; while (!listEmpty(lstTmp)) { car = MMC_CAR(lstTmp); len = MMC_STRLEN(car); /* fprintf(stderr, "stringAppendList: %s %d %d\n", MMC_STRINGDATA(car), len, strlen(MMC_STRINGDATA(car))); */ /* Might be useful to check this when debugging. String literals are often done wrong :) */ MMC_DEBUG_ASSERT(len == strlen(MMC_STRINGDATA(car))); memcpy(tmp+nbytes,MMC_STRINGDATA(car),len); nbytes += len; lstTmp = MMC_CDR(lstTmp); } tmp[nbytes] = '\0'; /* fprintf(stderr, "stringAppendList(%s)=>%s\n", anyString(lstHead), anyString(MMC_TAGPTR(res))); */ p = MMC_TAGPTR(res); MMC_CHECK_STRING(p); return p; }
modelica_integer nobox_stringCharInt(threadData_t *threadData,metamodelica_string chr) { unsigned char c; if (MMC_STRLEN(chr) != 1) MMC_THROW_INTERNAL(); MMC_CHECK_STRING(chr); return (unsigned char) MMC_STRINGDATA(chr)[0]; }
modelica_metatype boxptr_stringGetStringChar(threadData_t *threadData,metamodelica_string str, modelica_metatype iix) { modelica_metatype res; int ix = MMC_UNTAGFIXNUM(iix); MMC_CHECK_STRING(str); if (ix < 1 || ix > (long) MMC_STRLEN(str)) MMC_THROW_INTERNAL(); return mmc_strings_len1[(size_t)MMC_STRINGDATA(str)[ix-1]]; }
metamodelica_string stringListStringChar(metamodelica_string s) { const char *str = MMC_STRINGDATA(s); char chr[2] = {'\0', '\0'}; modelica_metatype res = NULL; int i = 0; MMC_CHECK_STRING(s); res = mmc_mk_nil(); for (i=MMC_STRLEN(s)-1; i>=0; i--) { chr[0] = str[i]; res = mmc_mk_cons(mmc_mk_scon(chr), res); } return res; }
/* Convert a modelica_integer to a modelica_string, used in String(integer, format="xxx") */ modelica_string modelica_integer_to_modelica_string_format(modelica_integer i,modelica_string format) { void *res; size_t sz; void *c_fmt = modelica_string_format_to_c_string_format(format); switch (MMC_STRINGDATA(c_fmt)[MMC_STRLEN(c_fmt)-1]) { case 'f': case 'e': case 'E': case 'g': case 'G': /* double */ sz = snprintf(NULL, 0, MMC_STRINGDATA(c_fmt), (double) i); res = alloc_modelica_string(sz); sprintf(MMC_STRINGDATA(res), MMC_STRINGDATA(c_fmt), (double) i); break; case 'c': case 'd': case 'i': /* int */ sz = snprintf(NULL, 0, MMC_STRINGDATA(c_fmt), (long) i); res = alloc_modelica_string(sz); sprintf(MMC_STRINGDATA(res), MMC_STRINGDATA(c_fmt), (long) i); break; case 'o': case 'x': case 'X': case 'u': /* uint */ sz = snprintf(NULL, 0, MMC_STRINGDATA(c_fmt), (unsigned long) i); res = alloc_modelica_string(sz); sprintf(MMC_STRINGDATA(res), MMC_STRINGDATA(c_fmt), (unsigned long) i); break; default: /* integer values, etc */ omc_assert(NULL, dummyFILE_INFO, "Invalid conversion specifier for Real: %c", MMC_STRINGDATA(c_fmt)[MMC_STRLEN(c_fmt)-1]); } return res; }
modelica_metatype boxptr_substring(threadData_t *threadData, metamodelica_string_const str, modelica_metatype boxstart, modelica_metatype boxstop) { unsigned header = 0, nwords; long start = MMC_UNTAGFIXNUM(boxstart) - 1; long stop = MMC_UNTAGFIXNUM(boxstop) - 1; long totalLen = MMC_STRLEN(str), len = stop-start+1; struct mmc_string *res; char *tmp; modelica_metatype p; /* Bad indexes */ if (start < 0 || start >= totalLen || stop < start || stop >= totalLen) { MMC_THROW_INTERNAL(); } header = MMC_STRINGHDR(len); nwords = MMC_HDRSLOTS(header) + 1; res = (struct mmc_string *) mmc_alloc_words_atomic(nwords); res->header = header; tmp = (char*) res->data; memcpy(tmp, MMC_STRINGDATA(str) + start, len); tmp[len] = '\0'; p = MMC_TAGPTR(res); MMC_CHECK_STRING(p); return p; }
modelica_integer nobox_stringGet(threadData_t *threadData,metamodelica_string str, modelica_integer ix) { if (ix < 1 || ix > (long) MMC_STRLEN(str)) MMC_THROW_INTERNAL(); return ((unsigned char*)MMC_STRINGDATA(str))[ix-1]; }