From e2cb5838c6b89bf0f34aa08562d3e3763729cba6 Mon Sep 17 00:00:00 2001 From: mniip Date: Thu, 22 Aug 2013 19:50:20 +0400 Subject: [PATCH] specially per @savask's request, put matcher into a function returns 0 if no match, returns position of pipe character if there is --- src/Misc.cpp | 21 +++++++++++++++++++++ src/Misc.h | 3 +++ src/graphics/Renderer.cpp | 18 +----------------- src/gui/game/GameController.cpp | 23 +++-------------------- src/gui/game/SignTool.cpp | 18 ++---------------- src/simulation/Sign.cpp | 27 ++++----------------------- 6 files changed, 34 insertions(+), 76 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index 6b0c1d537..918e5c4fe 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -621,5 +621,26 @@ void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize) dest[i] = dest[i] & src[i%srcsize]; } } + +int splitsign(const char* str) +{ + int match=0,r; + if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9') + { + const char* p=str+4; + while (*p>='0' && *p<='9') + p++; + if (*p=='|') + { + r=p-str; + while (*p) + p++; + if (p[-1]=='}') + return r; + } + } + return 0; +} + vector2d v2d_zero = {0,0}; matrix2d m2d_identity = {1,0,0,1}; diff --git a/src/Misc.h b/src/Misc.h index 33601f632..0f739504b 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -85,6 +85,9 @@ void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v); void OpenURI(std::string uri); void membwand(void * dest, void * src, size_t destsize, size_t srcsize); + +int splitsign(const char* str); + // a b // c d diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index 28e4d9ef9..45bc15e7b 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -924,26 +924,10 @@ void Renderer::DrawSigns() if (signs[i].text.length()) { std::string text = signs[i].getText(sim); - const char* str = signs[i].text.c_str(); signs[i].pos(text, x, y, w, h); clearrect(x, y, w+1, h); drawrect(x, y, w+1, h, 192, 192, 192, 255); - match=0; - // check if it's a link sign - if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9') - { - const char* p=str+4; - while (*p>='0' && *p<='9') - p++; - if (*p=='|') - { - while (*p) - p++; - if (p[-1]=='}') - match=1; - } - } - if (match) + if (splitsign(signs[i].text.c_str())) drawtext(x+3, y+3, text, 0, 191, 255, 255); else drawtext(x+3, y+3, text, 255, 255, 255, 255); diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index c514d3ea7..539b084ca 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -571,30 +571,13 @@ bool GameController::MouseUp(int x, int y, unsigned button) (*iter).pos((*iter).getText(sim), signx, signy, signw, signh); if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh) { - int match=0; const char* str=(*iter).text.c_str(); - const char* e; - if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9') - { - const char* p=str+4; - while (*p>='0' && *p<='9') - p++; - e=p; - if (*p=='|') - { - while (*p) - p++; - if (p[-1]=='}') - { - match=1; - } - } - } - if (match) + int pos=splitsign(str); + if (pos) { char buff[256]; strcpy(buff, str+3); - buff[e-str-3]=0; + buff[pos]=0; int tempSaveID = format::StringToNumber(std::string(buff)); if (tempSaveID) { diff --git a/src/gui/game/SignTool.cpp b/src/gui/game/SignTool.cpp index a71f8e2bf..9cb389a26 100644 --- a/src/gui/game/SignTool.cpp +++ b/src/gui/game/SignTool.cpp @@ -177,27 +177,13 @@ void SignWindow::DoDraw() for(std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) { sign & currentSign = *iter; - int x, y, w, h, dx, dy, match=0; + int x, y, w, h, dx, dy; Graphics * g = ui::Engine::Ref().g; std::string text = currentSign.getText(sim); - const char* str = currentSign.text.c_str(); currentSign.pos(text, x, y, w, h); g->clearrect(x, y, w+1, h); g->drawrect(x, y, w+1, h, 192, 192, 192, 255); - if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9') - { - const char* p=str+4; - while (*p>='0' && *p<='9') - p++; - if (*p=='|') - { - while (*p) - p++; - if (p[-1]=='}') - match=1; - } - } - if (match) + if (splitsign(currentSign.text.c_str())) g->drawtext(x+3, y+3, text, 0, 191, 255, 255); else g->drawtext(x+3, y+3, text, 255, 255, 255, 255); diff --git a/src/simulation/Sign.cpp b/src/simulation/Sign.cpp index f6b8420f9..57a8a0d24 100644 --- a/src/simulation/Sign.cpp +++ b/src/simulation/Sign.cpp @@ -33,30 +33,11 @@ std::string sign::getText(Simulation *sim) } else { - int match=0; - const char* r; - const char* e; - if (signText[0]=='{' && (signText[1]=='c' || signText[1]=='t') && signText[2]==':' && signText[3]>='0' && signText[3]<='9') + int pos=splitsign(signText); + if (pos) { - const char* p=signText+4; - while (*p>='0' && *p<='9') - p++; - if (*p=='|') - { - r=p+1; - while (*p) - p++; - if (p[-1]=='}') - { - match=1; - e=p; - } - } - } - if (match) - { - strcpy(buff, r); - buff[e-r-1]=0; + strcpy(buff, signText+pos+1); + buff[strlen(signText)-pos-2]=0; } else strcpy(buff, signText);