diff --git a/doc/helianthus-doc-ru.odt b/doc/helianthus-doc-ru.odt index 88321de..796b18c 100644 Binary files a/doc/helianthus-doc-ru.odt and b/doc/helianthus-doc-ru.odt differ diff --git a/src/common.c b/src/common.c index 8eff884..9df704d 100644 --- a/src/common.c +++ b/src/common.c @@ -114,6 +114,12 @@ char* heliStringCopy(const char *x) { return cp; } +char* heliStringCopyLower(const char *x) { + char *xx = heliStringCopy(x); + heliStringLower(xx); + return xx; +} + char* heliStringConcat(const char *a, const char *b) { int la = strlen(a); int lb = strlen(b); @@ -136,6 +142,22 @@ char* heliStringConcat3(const char *a, const char *b, const char *c) { return s; } +int heliStringCompareCi(const char *a, const char *b) { + if (!a && !b) return 0; + if (!a) return -1; + if (!b) return 1; + while(*a || *b) { + if (!a) return -1; + if (!b) return 1; + char aa = tolower(*a); + char bb = tolower(*b); + if (aa < bb) return -1; + if (bb < aa) return 1; + ++a, ++b; + } + return 0; +} + int heliStringEndsWithLowcase(const char *s, const char *tail) { int ls = strlen(s); int lt = strlen(tail); @@ -146,7 +168,7 @@ int heliStringEndsWithLowcase(const char *s, const char *tail) { return TRUE; } -void heliLowercase(char *x) +void heliStringLower(char *x) { while(*x) { *x = tolower(*x); ++x; } } @@ -281,14 +303,9 @@ unsigned int colorByNameA(const char *colorName, double a) { } else if (isalpha(*x)) { int count = (int)(sizeof(colors)/sizeof(*colors)); - for(int i = 0; i < count; ++i) { - const char *a = x, *b = colors[i][0]; - do { - if (tolower(*a) != tolower(*b)) break; - ++a, ++b; - if (!*a && !*b) return colorByNameA(colors[i][1], 1); - } while (*a && *b); - } + for(int i = 0; i < count; ++i) + if (heliStringCompareCi(x, colors[i][0]) == 0) + return colorByNameA(colors[i][1], a); } else { double r = 0, g = 0, b = 0, aa = 1; sscanf(x, "%lf %lf %lf %lf", &r, &g, &b, &aa); diff --git a/src/private.h b/src/private.h index bd0342c..f95b701 100644 --- a/src/private.h +++ b/src/private.h @@ -39,10 +39,13 @@ extern int heliBlobUnicodeFontSize asm("heliBlobUnicodeFontSize"); // string char* heliStringCopy(const char *x); +char* heliStringCopyLower(const char *x); char* heliStringConcat(const char *a, const char *b); char* heliStringConcat3(const char *a, const char *b, const char *c); +int heliStringCompareCi(const char *a, const char *b); int heliStringEndsWithLowcase(const char *s, const char *tail); -void heliLowercase(char *x); +void heliStringLower(char *x); + // common diff --git a/src/world.c b/src/world.c index 857b894..809d603 100644 --- a/src/world.c +++ b/src/world.c @@ -105,7 +105,7 @@ const char *keyEventGet(KeyEvent mode, int i) static int keyEventCheck(KeyEvent mode, const char *code) { int count = keyEventGetCount(mode); for(int i = 0; i < count; ++i) - if (strcmp(keyEventGet(mode, i), code) == 0) + if (heliStringCompareCi(keyEventGet(mode, i), code) == 0) return TRUE; return FALSE; } @@ -113,14 +113,14 @@ static int keyEventCheck(KeyEvent mode, const char *code) { static int keyEventAliasesCheck(KeyEvent mode, const char *code) { if (keyEventCheck(mode, code)) return TRUE; for(int i = 0; i < keyAliasesCount; ++i) - if (strcmp(code, keyAliases[i][0]) == 0) + if (heliStringCompareCi(code, keyAliases[i][0]) == 0) if (keyEventCheck(mode, keyAliases[i][1])) return TRUE; return FALSE; } static int keyEventAdd(KeyEvent mode, const char *code) { if ((int)mode >= 0 && mode <= (int)keyEventsCount && !keyEventCheck(mode, code)) { - heliArrayInsert(&keyEvents[mode], -1, heliStringCopy(code), &free); + heliArrayInsert(&keyEvents[mode], -1, heliStringCopyLower(code), &free); return TRUE; } return FALSE; @@ -130,7 +130,7 @@ static int keyEventRemove(KeyEvent mode, const char *code) { int removed = FALSE; if ((int)mode >= 0 && mode <= (int)keyEventsCount) for(int i = keyEvents[mode].count-1; i >= 0; --i) - if (strcmp(keyEvents[mode].items[i].value, code) == 0) + if (heliStringCompareCi(keyEvents[mode].items[i].value, code) == 0) { heliArrayRemove(&keyEvents[mode], i); removed = TRUE; } return removed; } @@ -140,7 +140,7 @@ static void pressKey(int mouse, const char *code) { keyEventAdd(mouse ? KEYEVENT_MOUSE_WENTDOWN : KEYEVENT_KEY_WENTDOWN, code); if (!mouse) { for(int i = 0; i < keyGroupsCount; ++i) { - if (strcmp(code, keyGroups[i][0]) == 0) { + if (heliStringCompareCi(code, keyGroups[i][0]) == 0) { keyEventAdd(KEYEVENT_KEY_DOWN, keyGroups[i][1]); keyEventAdd(KEYEVENT_KEY_WENTDOWN, keyGroups[i][1]); } @@ -155,10 +155,11 @@ static void releaseKey(int mouse, const char *code) { if (mouse) return; for(int i = 0; i < keyGroupsCount; ++i) { - if (strcmp(code, keyGroups[i][0]) != 0) continue; + if (heliStringCompareCi(code, keyGroups[i][0]) != 0) continue; int allRemoved = TRUE; for(int j = 0; j < keyGroupsCount; ++j) - if (strcmp(keyGroups[i][1], keyGroups[j][1]) == 0 && keyEventCheck(KEYEVENT_KEY_DOWN, keyGroups[j][0])) + if ( heliStringCompareCi(keyGroups[i][1], keyGroups[j][1]) == 0 + && keyEventCheck(KEYEVENT_KEY_DOWN, keyGroups[j][0])) { allRemoved = FALSE; break; } if (allRemoved) if (keyEventRemove(KEYEVENT_KEY_DOWN, keyGroups[i][1])) @@ -523,13 +524,10 @@ static void handleEvent(SDL_Event *e) { } } else if (e->type == SDL_KEYDOWN || e->type == SDL_KEYUP) { - const char *keynameOrig = SDL_GetKeyName(e->key.keysym.sym); - if (keynameOrig && *keynameOrig) { - char *keyname = heliStringCopy(keynameOrig); - heliLowercase(keyname); + const char *keyname = SDL_GetKeyName(e->key.keysym.sym); + if (keyname && *keyname) { if (e->type == SDL_KEYDOWN) pressKey(FALSE, keyname); else releaseKey(FALSE, keyname); - free(keyname); } } else if (e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP) {