int qspSplitStr(QSP_CHAR *str, QSP_CHAR *delim, QSP_CHAR ***res) { int allocChars, count = 0, bufSize = 8, delimLen = qspStrLen(delim); QSP_CHAR *newStr, **ret, *curPos = str, *found = qspStrStr(str, delim); ret = (QSP_CHAR **)malloc(bufSize * sizeof(QSP_CHAR *)); while (found) { allocChars = (int)(found - curPos); newStr = (QSP_CHAR *)malloc((allocChars + 1) * sizeof(QSP_CHAR)); qspStrNCopy(newStr, curPos, allocChars); newStr[allocChars] = 0; if (++count > bufSize) { bufSize += 16; ret = (QSP_CHAR **)realloc(ret, bufSize * sizeof(QSP_CHAR *)); } ret[count - 1] = newStr; curPos = found + delimLen; found = qspStrStr(curPos, delim); } newStr = (QSP_CHAR *)malloc((qspStrLen(curPos) + 1) * sizeof(QSP_CHAR)); qspStrCopy(newStr, curPos); if (++count > bufSize) ret = (QSP_CHAR **)realloc(ret, count * sizeof(QSP_CHAR *)); ret[count - 1] = newStr; *res = ret; return count; }
QSPString qspJoinPrepLines(QSPLineOfCode *s, int count, QSPString delim) { int i, itemLen, txtLen = 0, txtRealLen = 0, bufSize = 256, delimLen = qspStrLen(delim); QSP_CHAR *txt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); for (i = 0; i < count; ++i) { itemLen = qspStrLen(s[i].Str); if ((txtLen += itemLen) > bufSize) { bufSize = txtLen + 128; txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR)); } memcpy(txt + txtRealLen, s[i].Str.Str, itemLen * sizeof(QSP_CHAR)); if (i == count - 1) break; txtRealLen = txtLen; if ((txtLen += delimLen) > bufSize) { bufSize = txtLen + 128; txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR)); } memcpy(txt + txtRealLen, delim.Str, delimLen * sizeof(QSP_CHAR)); txtRealLen = txtLen; } return qspStringFromLen(txt, txtLen); }
QSP_CHAR *qspReplaceText(QSP_CHAR *txt, QSP_CHAR *searchTxt, QSP_CHAR *repTxt) { int txtLen, oldTxtLen, bufSize, searchLen, repLen, len; QSP_CHAR *newTxt, *pos = qspStrStr(txt, searchTxt); if (!pos) return qspGetNewText(txt, -1); bufSize = 256; txtLen = oldTxtLen = 0; searchLen = qspStrLen(searchTxt); repLen = qspStrLen(repTxt); newTxt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); do { len = (int)(pos - txt); if ((txtLen += len + repLen) >= bufSize) { bufSize = txtLen + 128; newTxt = (QSP_CHAR *)realloc(newTxt, bufSize * sizeof(QSP_CHAR)); } qspStrNCopy(newTxt + oldTxtLen, txt, len); qspStrCopy(newTxt + oldTxtLen + len, repTxt); oldTxtLen = txtLen; txt = pos + searchLen; pos = qspStrStr(txt, searchTxt); } while (pos); return qspGetAddText(newTxt, txt, txtLen, -1); }
static int qspSearchPlayList(QSP_CHAR *file) { QSP_CHAR *uName, *buf; int i, bufSize, itemLen, len; if (!qspPLFilesCount) return -1; len = qspStrLen(file); qspUpperStr(uName = qspGetNewText(file, len)); bufSize = 32; buf = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); for (i = 0; i < qspPLFilesCount; ++i) { itemLen = qspStrLen(qspPLFiles[i]); if (itemLen >= bufSize) { bufSize = itemLen + 8; buf = (QSP_CHAR *)realloc(buf, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(buf, qspPLFiles[i]); qspUpperStr(buf); if (!qspStrsNComp(buf, uName, len) && qspIsInListEOL(QSP_PLVOLUMEDELIM, buf[len])) { free(uName); free(buf); return i; } } free(uName); free(buf); return -1; }
QSP_CHAR *qspJoinStrs(QSP_CHAR **s, int count, QSP_CHAR *delim) { int i, txtLen = 0, txtRealLen = 0, bufSize = 256, lastIndex = count - 1, delimLen = qspStrLen(delim); QSP_CHAR *txt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); *txt = 0; for (i = 0; i < count; ++i) { if ((txtLen += qspStrLen(s[i])) >= bufSize) { bufSize = txtLen + 128; txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(txt + txtRealLen, s[i]); if (i == lastIndex) break; txtRealLen = txtLen; if ((txtLen += delimLen) >= bufSize) { bufSize = txtLen + 128; txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(txt + txtRealLen, delim); txtRealLen = txtLen; } return txt; }
int qspObjIndex(QSP_CHAR *name) { int i, objNameLen, bufSize; QSP_CHAR *uName, *buf; if (!qspCurObjectsCount) return -1; qspUpperStr(uName = qspGetNewText(name, -1)); bufSize = 32; buf = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); for (i = 0; i < qspCurObjectsCount; ++i) { objNameLen = qspStrLen(qspCurObjects[i].Desc); if (objNameLen >= bufSize) { bufSize = objNameLen + 8; buf = (QSP_CHAR *)realloc(buf, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(buf, qspCurObjects[i].Desc); qspUpperStr(buf); if (!qspStrsComp(buf, uName)) { free(uName); free(buf); return i; } } free(uName); free(buf); return -1; }
QSP_CHAR *qspCodeReCode(QSP_CHAR *str, QSP_BOOL isCode) { int len = qspStrLen(str); QSP_CHAR ch, *buf = (QSP_CHAR *)malloc((len + 1) * sizeof(QSP_CHAR)); buf[len] = 0; if (isCode) { while (--len >= 0) { ch = str[len]; if (ch == QSP_CODREMOV) ch = (QSP_CHAR)-QSP_CODREMOV; else ch -= QSP_CODREMOV; buf[len] = ch; } } else { while (--len >= 0) { ch = str[len]; if (ch == (QSP_CHAR)-QSP_CODREMOV) ch = QSP_CODREMOV; else ch += QSP_CODREMOV; buf[len] = ch; } } return buf; }
static int qspActIndex(QSP_CHAR *name) { int i, actNameLen, bufSize; QSP_CHAR *uName, *buf; if (!qspCurActionsCount) return -1; qspUpperStr(uName = qspGetNewText(name, -1)); bufSize = 64; buf = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); for (i = 0; i < qspCurActionsCount; ++i) { actNameLen = qspStrLen(qspCurActions[i].Desc); if (actNameLen >= bufSize) { bufSize = actNameLen + 16; buf = (QSP_CHAR *)realloc(buf, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(buf, qspCurActions[i].Desc); qspUpperStr(buf); if (!qspStrsComp(buf, uName)) { free(uName); free(buf); return i; } } free(uName); free(buf); return -1; }
static int qspActIndex(QSPString name) { QSPString uName, bufName; int i, actNameLen, bufSize; QSP_CHAR *buf; if (!qspCurActionsCount) return -1; uName = qspGetNewText(name); qspUpperStr(&uName); bufSize = 64; buf = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); for (i = 0; i < qspCurActionsCount; ++i) { actNameLen = qspStrLen(qspCurActions[i].Desc); if (actNameLen > bufSize) { bufSize = actNameLen + 16; buf = (QSP_CHAR *)realloc(buf, bufSize * sizeof(QSP_CHAR)); } memcpy(buf, qspCurActions[i].Desc.Str, actNameLen * sizeof(QSP_CHAR)); bufName = qspStringFromLen(buf, actNameLen); qspUpperStr(&bufName); if (!qspStrsComp(bufName, uName)) { qspFreeString(uName); free(buf); return i; } } qspFreeString(uName); free(buf); return -1; }
QSP_CHAR *qspFormatText(QSP_CHAR *txt, QSP_BOOL canReturnSelf) { QSPVariant val; QSP_CHAR *newTxt, *lPos, *rPos; int oldRefreshCount, len, txtLen, oldTxtLen, bufSize; if (qspGetVarNumValue(QSP_FMT("DISABLESUBEX"))) { if (canReturnSelf) return txt; return qspGetNewText(txt, -1); } lPos = qspStrStr(txt, QSP_LSUBEX); if (!lPos) { if (canReturnSelf) return txt; return qspGetNewText(txt, -1); } bufSize = 256; newTxt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR)); txtLen = oldTxtLen = 0; oldRefreshCount = qspRefreshCount; do { len = (int)(lPos - txt); if ((txtLen += len) >= bufSize) { bufSize = txtLen + 128; newTxt = (QSP_CHAR *)realloc(newTxt, bufSize * sizeof(QSP_CHAR)); } qspStrNCopy(newTxt + oldTxtLen, txt, len); oldTxtLen = txtLen; txt = lPos + QSP_LEN(QSP_LSUBEX); rPos = qspStrPos(txt, QSP_RSUBEX, QSP_FALSE); if (!rPos) { qspSetError(QSP_ERR_BRACKNOTFOUND); free(newTxt); return 0; } *rPos = 0; val = qspExprValue(txt); *rPos = QSP_RSUBEX[0]; if (qspRefreshCount != oldRefreshCount || qspErrorNum) { free(newTxt); return 0; } qspConvertVariantTo(&val, QSP_TRUE); if ((txtLen += qspStrLen(QSP_STR(val))) >= bufSize) { bufSize = txtLen + 128; newTxt = (QSP_CHAR *)realloc(newTxt, bufSize * sizeof(QSP_CHAR)); } qspStrCopy(newTxt + oldTxtLen, QSP_STR(val)); free(QSP_STR(val)); oldTxtLen = txtLen; txt = rPos + QSP_LEN(QSP_RSUBEX); lPos = qspStrStr(txt, QSP_LSUBEX); } while (lPos); return qspGetAddText(newTxt, txt, txtLen, -1); }
QSP_CHAR *qspStrPos(QSP_CHAR *txt, QSP_CHAR *str, QSP_BOOL isIsolated) { QSP_BOOL isLastDelim; int strLen, c1, c2, c3; QSP_CHAR quot, *pos = qspStrStr(txt, str); if (!pos) return 0; if (!(isIsolated || qspStrPBrk(txt, QSP_QUOTS QSP_LQUOT QSP_LRBRACK QSP_LSBRACK))) return pos; strLen = qspStrLen(str); pos = qspStrEnd(txt) - strLen + 1; c1 = c2 = c3 = 0; isLastDelim = QSP_TRUE; while (txt < pos) { if (qspIsInList(QSP_QUOTS, *txt)) { quot = *txt; while (++txt < pos) if (*txt == quot && *(++txt) != quot) break; if (txt >= pos) return 0; isLastDelim = QSP_TRUE; } if (*txt == QSP_LRBRACK[0]) ++c1; else if (*txt == QSP_RRBRACK[0]) { if (c1) --c1; } else if (*txt == QSP_LSBRACK[0]) ++c2; else if (*txt == QSP_RSBRACK[0]) { if (c2) --c2; } else if (*txt == QSP_LQUOT[0]) ++c3; else if (*txt == QSP_RQUOT[0]) { if (c3) --c3; } if (!(c1 || c2 || c3)) { if (isIsolated) { if (qspIsInList(QSP_DELIMS, *txt)) isLastDelim = QSP_TRUE; else if (isLastDelim) { if (qspIsInListEOL(QSP_DELIMS, txt[strLen]) && !qspStrsNComp(txt, str, strLen)) return txt; isLastDelim = QSP_FALSE; } } else if (!qspStrsNComp(txt, str, strLen)) return txt; } ++txt; } return 0; }
char *qspW2C(QSP_CHAR *src) { int ret; char *dst = (char *)malloc((qspStrLen(src) * 3) + 1); char *s = dst; while ((ret = qspUTF8_wctomb(s, *src, 3)) && *s) { ++src; s += ret; } *s = 0; return dst; }
static char *qspQSPToGameString(QSP_CHAR *s, QSP_BOOL isUCS2, QSP_BOOL isCode) { unsigned short uCh, *ptr; int len = qspStrLen(s); char ch, *ret = (char *)malloc((len + 1) * (isUCS2 ? 2 : 1)); if (isUCS2) { ptr = (unsigned short *)ret; ptr[len] = 0; if (isCode) { while (--len >= 0) { uCh = (unsigned short)QSP_BTOWC(s[len]); if (uCh == QSP_CODREMOV) uCh = (unsigned short)-QSP_CODREMOV; else uCh -= QSP_CODREMOV; ptr[len] = QSP_FIXBYTESORDER(uCh); } } else { while (--len >= 0) { uCh = (unsigned short)QSP_BTOWC(s[len]); ptr[len] = QSP_FIXBYTESORDER(uCh); } } } else { ret[len] = 0; if (isCode) { while (--len >= 0) { ch = QSP_FROM_OS_CHAR(s[len]); if (ch == QSP_CODREMOV) ch = -QSP_CODREMOV; else ch -= QSP_CODREMOV; ret[len] = ch; } } else { while (--len >= 0) ret[len] = QSP_FROM_OS_CHAR(s[len]); } } return ret; }
int qspAddText(QSP_CHAR **dest, QSP_CHAR *val, int destLen, int valLen, QSP_BOOL isCreate) { int ret; QSP_CHAR *destPtr; if (valLen < 0) valLen = qspStrLen(val); if (!isCreate && *dest) { if (destLen < 0) destLen = qspStrLen(*dest); ret = destLen + valLen; destPtr = (QSP_CHAR *)realloc(*dest, (ret + 1) * sizeof(QSP_CHAR)); *dest = destPtr; destPtr += destLen; } else { ret = valLen; destPtr = (QSP_CHAR *)malloc((ret + 1) * sizeof(QSP_CHAR)); *dest = destPtr; } qspStrNCopy(destPtr, val, valLen); destPtr[valLen] = 0; return ret; }
static int qspProcessEOLExtensions(QSPLineOfCode *s, int count, QSPLineOfCode **strs) { QSPLineOfCode *ret; QSPString str, eol, eolExt; int len, lastNum = 0, i = 0, bufSize = 8, newCount = 0; ret = (QSPLineOfCode *)malloc(bufSize * sizeof(QSPLineOfCode)); eolExt = QSP_STATIC_STR(QSP_PREEOLEXT QSP_EOLEXT); while (i < count) { qspAddText(&str, s[i].Str, QSP_TRUE); len = qspStrLen(str); if (len >= QSP_STATIC_LEN(QSP_PREEOLEXT QSP_EOLEXT)) { eol = str; eol.Str += len - QSP_STATIC_LEN(QSP_PREEOLEXT QSP_EOLEXT); while (!qspStrsComp(eol, eolExt)) { if (++i == count) break; str.End -= QSP_STATIC_LEN(QSP_EOLEXT); qspAddText(&str, s[i].Str, QSP_FALSE); len = qspStrLen(str); eol = str; eol.Str += len - QSP_STATIC_LEN(QSP_PREEOLEXT QSP_EOLEXT); } } if (newCount >= bufSize) { bufSize = newCount + 16; ret = (QSPLineOfCode *)realloc(ret, bufSize * sizeof(QSPLineOfCode)); } qspInitLineOfCode(ret + newCount++, str, lastNum); ++i; lastNum = s[i].LineNum; } *strs = ret; return newCount; }
QSPString qspGetAllActionsAsCode() { int count, i; QSPString res, temp; res = qspNewEmptyString(); for (i = 0; i < qspCurActionsCount; ++i) { qspAddText(&res, QSP_STATIC_STR(QSP_FMT("ACT '")), QSP_FALSE); temp = qspReplaceText(qspCurActions[i].Desc, QSP_STATIC_STR(QSP_FMT("'")), QSP_STATIC_STR(QSP_FMT("''"))); qspAddText(&res, temp, QSP_FALSE); qspFreeString(temp); if (qspCurActions[i].Image.Str) { qspAddText(&res, QSP_STATIC_STR(QSP_FMT("','")), QSP_FALSE); temp = qspReplaceText( qspStringFromPair(qspCurActions[i].Image.Str + qspStrLen(qspQstPath), qspCurActions[i].Image.End), QSP_STATIC_STR(QSP_FMT("'")), QSP_STATIC_STR(QSP_FMT("''"))); qspAddText(&res, temp, QSP_FALSE); qspFreeString(temp); } qspAddText(&res, QSP_STATIC_STR(QSP_FMT("':")), QSP_FALSE); count = qspCurActions[i].OnPressLinesCount; if (count == 1 && qspIsAnyString(qspCurActions[i].OnPressLines->Str)) qspAddText(&res, qspCurActions[i].OnPressLines->Str, QSP_FALSE); else { if (count >= 2) { qspAddText(&res, QSP_STATIC_STR(QSP_STRSDELIM), QSP_FALSE); temp = qspJoinPrepLines(qspCurActions[i].OnPressLines, count, QSP_STATIC_STR(QSP_STRSDELIM)); qspAddText(&res, temp, QSP_FALSE); qspFreeString(temp); } qspAddText(&res, QSP_STATIC_STR(QSP_STRSDELIM QSP_FMT("END")), QSP_FALSE); } qspAddText(&res, QSP_STATIC_STR(QSP_STRSDELIM), QSP_FALSE); } return res; }
int qspSplitGameStr(char *str, QSP_BOOL isUCS2, QSP_CHAR *delim, char ***res) { char *delimStr, *newStr, **ret, *found, *curPos = str; int charSize, delimSize, allocChars, count = 0, bufSize = 8; charSize = (isUCS2 ? 2 : 1); delimSize = qspStrLen(delim) * charSize; delimStr = qspQSPToGameString(delim, isUCS2, QSP_FALSE); found = (isUCS2 ? qspUCS2StrStr(str, delimStr) : strstr(str, delimStr)); ret = (char **)malloc(bufSize * sizeof(char *)); while (found) { allocChars = (int)(found - curPos); newStr = (char *)malloc(allocChars + charSize); memcpy(newStr, curPos, allocChars); if (isUCS2) *((unsigned short *)(newStr + allocChars)) = 0; else newStr[allocChars] = 0; if (++count > bufSize) { bufSize += 16; ret = (char **)realloc(ret, bufSize * sizeof(char *)); } ret[count - 1] = newStr; curPos = found + delimSize; found = (isUCS2 ? qspUCS2StrStr(curPos, delimStr) : strstr(curPos, delimStr)); } free(delimStr); allocChars = (isUCS2 ? (qspUCS2StrLen(curPos) + 1) * charSize : (int)strlen(curPos) + 1); newStr = (char *)malloc(allocChars); memcpy(newStr, curPos, allocChars); if (++count > bufSize) ret = (char **)realloc(ret, count * sizeof(char *)); ret[count - 1] = newStr; *res = ret; return count; }