void test_stringTrim_should_trim_both_sides3(void) { Text *text = textNew(" \t\t BeedFack \t\t "); String *str = stringNew(text); stringTrim(str); TEST_ASSERT_EQUAL(4,str->start); TEST_ASSERT_EQUAL(8,str->length); }
void test_stringTrim_should_trim_both_sides(void) { Text *text = textNew(" \t \t \t VeaLer "); String *str = stringNew(text); stringTrim(str); TEST_ASSERT_EQUAL(9,str->start); TEST_ASSERT_EQUAL(6,str->length); }
void test_stringTrim_should_trim_both_sides2(void) { Text *text = textNew(" GaLger \t\t "); String *str = stringNew(text); stringTrim(str); TEST_ASSERT_EQUAL(2,str->start); TEST_ASSERT_EQUAL(6,str->length); }
/** * To extract the argument of address * * Input: * String the whole argument * * Return the value of the address * Throw if value is invalid */ int extractValue(String *arguments){ char *returnChar; int returnInt; String *string; stringTrimLeft(arguments); if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringCharAt(arguments,0) == ';' || stringLength(arguments) == 0) Throw(ERR_NO_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0) Throw(ERR_EMPTY_ARGUMENT); returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); free(string); return returnInt; } //pass to jason
void renderSelectionList() { // Draws the three presets in the list (Selected-1,Selected,Selected+1) // Find config id to render pthread_mutex_lock(&gui_select); /*static*/ int tmpRenderConfID = 0; // Render first entry if (gui_selectConfig > 0) tmpRenderConfID = gui_selectConfig-1; else tmpRenderConfID = numConfigs; char* renderLine = stringTrim((char*)getConfigName(tmpRenderConfID),25); g15r_renderString(canvas, (unsigned char*)renderLine, 0, G15_TEXT_MED, 1, 17); free(renderLine); renderLine = NULL; // Render middle entry available for selection renderLine = stringTrim((char*)getConfigName(gui_selectConfig),25); g15r_renderString(canvas, (unsigned char*)renderLine, 0, G15_TEXT_MED, 1, 26); free(renderLine); renderLine = NULL; // Render third (last) entry if (gui_selectConfig < numConfigs) tmpRenderConfID = gui_selectConfig+1; else tmpRenderConfID = 0; pthread_mutex_unlock(&gui_select); renderLine = stringTrim((char*)getConfigName(tmpRenderConfID),25); g15r_renderString(canvas, (unsigned char*)renderLine, 0, G15_TEXT_MED, 1, 35); free(renderLine); renderLine = NULL; // Make middle look selected by inverting colours g15r_pixelReverseFill(canvas, 0, 24, 121, 33, 0,G15_COLOR_BLACK); g15_send(g15screen_fd,(char *)canvas->buffer,G15_BUFFER_LEN); pthread_mutex_lock(&gui_select); gui_oldConfig = gui_selectConfig; pthread_mutex_unlock(&gui_select); }
std::string Unit::printString(const unsigned int mode) { std::string result; std::string unicodeExp[10] = {"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"}; if(prefixes != 0) { std::string strPre = numberToString<int>(prefixes); if(mode == UNIT_EXP) { for(int j = 0; j < 10; j++) { stringReplace(strPre, numberToString<int>(j), unicodeExp[j]); } stringReplace(strPre, "-", "⁻"); result.append("10" + strPre); } else { result.append("10^" + strPre); } } for(size_t i = 0; i < UNIT_NUMBER_BASE_UNITS; i++) { if(units[i] == 1) { result.append(" " + baseUnitList[i]); } else if(units[i] != 0) { std::string strExp = numberToString<int>(units[i]); if(mode == UNIT_EXP) { for(int j = 0; j < 10; j++) { stringReplace(strExp, numberToString<int>(j), unicodeExp[j]); } stringReplace(strExp, "-", "⁻"); result.append(" " + baseUnitList[i] + strExp); } else { result.append(" " + baseUnitList[i] + "^" + strExp); } } } stringTrim(result); return result; }
/** * To extract the argument of access or banked * * Input: * String the whole argument * * Return the value of access/banked * Throw if value is invalid */ int extractAccessBanked(String *arguments){ char location; char *returnChar; int returnInt; String *string; String *banked = stringNew(textNew("BANKED")); //1 String *access = stringNew(textNew("ACCESS")); //0 if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_NO_ARGUMENT); stringTrimLeft(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_EMPTY_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0){ if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); Throw(ERR_EMPTY_ARGUMENT); } else if(stringLength(string) == 6){ if(stringIsEqual(string,banked)) returnInt = 1; else if(stringIsEqual(string,access)) returnInt = 0; else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } } else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); free(string); return returnInt; }
/** * To extract the argument of location to save * * Input: * String the whole argument * * Return the value of the location * Throw if value is invalid */ int extractDestination(String *arguments){ char location; char *returnChar; int returnInt; String *string; if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_NO_ARGUMENT); stringTrimLeft(arguments); if(stringLength(arguments) == 0 || stringCharAt(arguments,0)== ';') Throw(ERR_EMPTY_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0){ if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); Throw(ERR_EMPTY_ARGUMENT); } else if(stringLength(string) == 1){ location = stringCharAt(string,0); if(location == 'F') returnInt = 1; else if(location == 'W') returnInt = 0; else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } } else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } free(string); return returnInt; }//if f, return 1, w is 0
void test_all(void) { String *toCompare; Text *text = textNew(" \t\t ahaha \t\t "); String *str = stringNew(text); String *str2 = stringAssign(str); TEST_ASSERT_EQUAL(2,text->reference); toCompare = stringDel(str); //still got 1 TEST_ASSERT_EQUAL(2,text->reference); TEST_ASSERT_EQUAL(1,toCompare->reference); toCompare = stringDel(str); //no more //unable to TEST_ASSERT_EQUAL, bad memory because already removed TEST_ASSERT_EQUAL(1,text->reference); stringTrim(str); TEST_ASSERT_EQUAL(4,str->start); TEST_ASSERT_EQUAL(5,str->length); }
//bool TTecladoPantalla::gestionTeclado(char *textoBusqueda) bool TTecladoPantalla::gestionTeclado() { char *arrayLetras = {"abcdefghijklmnñopqrstuvwxyz0123456789 "}; int tamArrayletras = strlen(arrayLetras); int posArrayLetras = -1; bool salir = false; int posTriangulo = 0; //char textoBusqueda[numLetras]; //memset(textoBusqueda,' ',sizeof(textoBusqueda)/sizeof(char)); bool editarPalabra = false; int posMenuTeclado = 0; Clear(); this->miImagen->fondoCirculos(); pintarPalabraBuscar(textoBusqueda,0,0, false); SDL_UpdateRect(screen, 0, 0, 0, 0); bool pressed = false; bool first_press = true; unsigned int last_press = 0; int inicio = SDL_GetTicks(); int tiempo_empleado = 0; while (!salir) { /* Check for events */ SDL_Event event; while (SDL_PollEvent(&event) && !salir) { switch (event.type) { case SDL_KEYDOWN: case SDL_JOYBUTTONDOWN: pressed = true; last_press = SDL_GetTicks(); break; case SDL_KEYUP: case SDL_JOYBUTTONUP: pressed = false; first_press = true; break; default: break; }//switch (event.type) }//while (SDL_PollEvent if (pressed) { if (last_press < (SDL_GetTicks() - this->key_init_delay) || first_press) { first_press = false; inicio = SDL_GetTicks(); if (event.key.keysym.sym == SDLK_RETURN || event.jbutton.button == GP2X_BUTTON_B) { pressed = false; //Orden de editar el texto if (editarPalabra) { posMenuTeclado = 2; editarPalabra = false; } else { if (posMenuTeclado == 0) editarPalabra = true; else editarPalabra = false; //Orden de borrar el texto if (posMenuTeclado == 1) { memset(textoBusqueda,' ',numLetras); textoBusqueda[numLetras] = '\0'; //strcpy(textoBusqueda,""); posTriangulo = 0; } else if (posMenuTeclado == 2) { stringTrim(textoBusqueda); if (strcmp(textoBusqueda," ") != 0) { return true; } else { memset(textoBusqueda,' ',numLetras); textoBusqueda[numLetras] = '\0'; } } else if (posMenuTeclado == 3) { salir = true; } } pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (event.key.keysym.sym == SDLK_ESCAPE || event.jbutton.button == GP2X_BUTTON_X) { pressed = false; if (editarPalabra == true) editarPalabra = false; else salir = true; pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (!editarPalabra) { if (event.key.keysym.sym == SDLK_RIGHT || event.jbutton.button == GP2X_BUTTON_RIGHT) { if (posMenuTeclado < 3) posMenuTeclado++; } else if (event.key.keysym.sym == SDLK_LEFT || event.jbutton.button == GP2X_BUTTON_LEFT) { if (posMenuTeclado > 0) posMenuTeclado--; } pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else { if (event.key.keysym.sym == SDLK_RIGHT || event.jbutton.button == GP2X_BUTTON_RIGHT) { if (posTriangulo < numLetras-1) { posTriangulo++; posArrayLetras = -1; } pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (event.key.keysym.sym == SDLK_LEFT || event.jbutton.button == GP2X_BUTTON_LEFT) { if (posTriangulo > 0) { posTriangulo--; posArrayLetras = -1; } pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (event.key.keysym.sym == SDLK_UP || event.jbutton.button == GP2X_BUTTON_UP) { if (posArrayLetras < tamArrayletras-1) posArrayLetras++; else posArrayLetras = 0; textoBusqueda[posTriangulo] = arrayLetras[posArrayLetras]; pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (event.key.keysym.sym == SDLK_DOWN || event.jbutton.button == GP2X_BUTTON_DOWN) { if (posArrayLetras > 0) posArrayLetras--; else posArrayLetras = tamArrayletras-1; textoBusqueda[posTriangulo] = arrayLetras[posArrayLetras]; pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (event.key.keysym.sym == SDLK_BACKSPACE) { if (posTriangulo > 0) posTriangulo--; textoBusqueda[posTriangulo] = ' '; pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); } else if (posTriangulo < numLetras) { //printf("la pulsada es: %c\n",event.key.keysym.sym); textoBusqueda[posTriangulo] = event.key.keysym.sym; pintarPalabraBuscar(textoBusqueda,posTriangulo,posMenuTeclado, editarPalabra); posTriangulo++; } } }//if (last_press }//if (pressed) tiempo_empleado = SDL_GetTicks() - inicio; if ( tiempo_empleado < this->key_delay) { SDL_Delay(this->key_delay - tiempo_empleado); } }//while (!salir) SDL_UpdateRect(screen, 0, 0, 0, 0); return false; }
int Unit::set(const std::string &value) { this->clear(); std::string unitString = value; stringTrim(unitString); if(unitString.empty()) return 0; stringReplace(unitString, "**", ""); // remove exponentiation symbols stringReplace(unitString, "^", ""); stringReplace(unitString, "*", " "); // replace multiplication symbols with space for tokenising stringReplace(unitString, "/", " /"); // pad division symbols with space for tokenising while(stringReplace(unitString, "/ ", "/") == 0); // remove all spaces after division symbols std::vector<std::string> tokens; // Tokenise string using spaces: if(stringTok(unitString, tokens, " ") != 0) { std::cerr << "Error (Unit): Failed to tokenise unit string." << std::endl; this->clear(); return 1; } if(tokens.size() < 1) { std::cerr << "Error (Unit): Failed to tokenise unit string." << std::endl; this->clear(); return 1; } // Loop through all tokens: for(size_t i = 0; i < tokens.size(); i++) { int factorDiv = 1; // Will become -1 if division sign '/' found. int exponent = 1; // Exponent of the unit; defaults to 1 if unspecified. // Check for division symbol: if(tokens[i].substr(0, 1) == "/") { tokens[i] = tokens[i].substr(1, tokens[i].size() - 1); factorDiv = -1; } // Extract exponent: size_t posExp = tokens[i].find_first_of("+-0123456789"); if(posExp != std::string::npos) { exponent = stringToNumber<int>(tokens[i].substr(posExp)); } exponent *= factorDiv; // Only proceed if unit is not unity: if(exponent != 0 and tokens[i] != "1") { // Extract base unit and prefix: bool success = false; std::map<std::string, std::vector<int> >::const_iterator iterUnit = unitMap.begin(); do { std::map<std::string, int>::const_iterator iterPrefix = prefixMap.begin(); do { std::string searchKey = iterPrefix->first + iterUnit->first; if(searchKey == tokens[i].substr(0, posExp)) { success = true; int prefix = iterPrefix->second; std::vector<int> unit(iterUnit->second); // Take care of special case 'kg': if(iterUnit->first == "g") prefix -= 3; for(size_t ii = 0; ii < UNIT_NUMBER_BASE_UNITS; ii++) { units[ii] += unit[ii] * exponent; } prefixes += prefix * exponent; } iterPrefix++; } while(success == false and iterPrefix != prefixMap.end()); iterUnit++; } while(success == false and iterUnit != unitMap.end()); if(success == false) { std::cerr << "Error (Unit): Unknown unit: \'" << value << "\'." << std::endl; std::cerr << " Creating dimensionless unit instead." << std::endl; clear(); return 1; } } } return 0; }
bool append_file(const char* path,const char* file_name,MPQPackage* pPkg,FILE* fp_log) { if ( !path || !file_name || !pPkg) { return false; } string fullPathKey = stringTrim(path,"\n"); string filePathKey = stringTrim(file_name,"\n"); if ( filePathKey.empty() ) { return false; } if ( fullPathKey.empty() ) { fullPathKey = filePathKey; } else { fullPathKey.append("/"); fullPathKey.append(filePathKey); } FILE* fp = fopen(fullPathKey.c_str(),"rb"); if ( !fp ) { if ( fp_log ) { fprintf(fp_log,"can't open file %s\n",fullPathKey.c_str()); } return false; } // md5 check code MD5 m; m.update(fp); fseek(fp,0,SEEK_END); unsigned int file_size = ftell(fp); m.update(&file_size,sizeof(unsigned int)); unsigned char* pData = new unsigned char[file_size]; if ( !pData ) { if ( fp_log ) { fprintf(fp_log,"can't assign memory for file %s\n",fullPathKey.c_str()); } fclose(fp); return false; } fseek(fp,0,SEEK_SET); fread(pData,1,file_size,fp); fclose(fp); bool bCompress = true; if ((std::string::npos != fullPathKey.find(".jpg")) || (std::string::npos != fullPathKey.find(".png")) || (std::string::npos != fullPathKey.find(".pvr"))) { bCompress = false; } MPQHashNode* pNode = pPkg->get_hash_node_new(filePathKey.c_str()); if ( !pNode ) { // no space if ( fp_log ) { fprintf(fp_log,"no hash node left for file %s\n",filePathKey.c_str()); } delete pData; return false; } unsigned int blockIndex = pPkg->append_data(pData,file_size,bCompress); if ( blockIndex == MPQ_INVALID ) { pPkg->reset_hash_node(pNode); delete pData; if ( fp_log ) { fprintf(fp_log,"append data failed for file %s\n",filePathKey.c_str()); } return false; } pNode->block_index = blockIndex; MPQBlock* pBlock = pPkg->get_block(pNode); memcpy(pBlock->md5_code,m.result(),MPQ_MD5_CODE_LEN); pBlock->name = filePathKey; delete pData; if ( fp_log ) { fprintf(fp_log,"added file %s \n",filePathKey.c_str()); } return true; }
unsigned long packWithConfig(const char* path,const char* mpq,const char* config) { FILE* fp_config = fopen(config,"r+"); if ( !fp_config ) { return 0; } vector<string> pack_files; while( !feof(fp_config) ) { char szBuffer[MAX_PATH] = ""; fgets(szBuffer,MAX_PATH,fp_config); string fileKey = stringTrim(szBuffer,"\n"); fileKey = stringTrim(fileKey," "); if ( !fileKey.empty() ) { pack_files.push_back(fileKey); } } fclose(fp_config); MPQPackage* pPkg = new MPQPackage(); if ( !pPkg || !pPkg->create(mpq) ) { if ( pPkg ) { delete pPkg; } return 0; } FILE* fp_log = fopen("log.txt","w+"); int pack_count = 0; m_pOwner->setRange((int)pack_files.size()); for ( size_t i = 0;i< pack_files.size(); ++i ) { if( append_file(path,pack_files[i].c_str(),pPkg,fp_log) ) { ++pack_count; } m_pOwner->setPos(i+1); } if ( fp_log ) { fprintf(fp_log,"file count:%d,packed file count:%d\n",pack_files.size(),pack_count); fclose(fp_log); } if ( pPkg ) { pPkg->format_block_table("packWithConfig.log"); pPkg->close(); delete pPkg; } return pack_count; }
/* To extract out number token , identifier token or operator token from the expression string * input : * expression is the string that we wanted to extract the token * output : * return number token if number is found in string * return idendifier token if identifier is found in string * return operator token if operator is found in string */ Token *getToken(String *expression) { Number *num; Identifier *iden; Operator *op; File *file; int tempStart = 0 , tempLength = 0, hex = 0; char *tempIden; //temporary store idendifier name stringTrim(expression); //Remove all the spaces in string /*Character at first position*/ int charAtThisPos = expression->startindex; if(expression->length == 0 || expression->length < 0) Throw(ERR_EMPTY_STRING); //if character start with numbers it is number token else if(stringCharAtInSet(expression , charAtThisPos , numSet)) { String *removedWord = stringRemoveWordContaining (expression , hexNumSet); //Remove numbers in string tempStart = removedWord->startindex; tempLength = removedWord->length; char *numSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed numbers become substring charAtThisPos = expression->startindex; if(numSubString[0] == '0' && (numSubString[1] == 'x' || numSubString[1] == 'X')) { sscanf(numSubString, "%x", &hex); num = numberNew(hex); //get integer from subStringToInteger and create a new Number Token num->line = stringSubString(expression , tempStart , tempLength); } else if(stringCharAtInSet(expression , charAtThisPos , alphaSet)) { Throw(ERR_NOT_NUMBER_TOKEN); } else { int integer = subStringToInteger(numSubString); //Convert substring to integer num = numberNew(integer); //get integer from subStringToInteger and create a new Number Token num->line = stringSubString(expression , tempStart , tempLength); } return (Token*)num; } else if(stringCharAtInSet(expression , charAtThisPos, alphaNumericSet) && stringCharAtInSet(expression , charAtThisPos + 1, ":")) { String *removedWord = stringRemoveWordContaining (expression , folderNameSet); //Remove identifier from string tempStart = removedWord->startindex; tempLength = removedWord->length; char *fileSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed identifier become substring file = fileNew(fileSubString); //create a new identifier token file->line = stringSubString(expression , tempStart , tempLength); return (Token*)file; } //if character start with A~Z/a~z or '_' it is identifier token else if(stringCharAtInSet(expression , charAtThisPos , alphaNumericSet)) { String *removedWord = stringRemoveWordContaining (expression , alphaNumericSet); //Remove identifier from string tempStart = removedWord->startindex; tempLength = removedWord->length; char *idenSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed identifier become substring iden = identifierNew(idenSubString); //create a new identifier token iden->line = stringSubString(expression , tempStart , tempLength); return (Token*)iden; } // if character not start with A~Z/a~z , '_' or numbers it is operator token else if(stringCharAtInSet(expression , charAtThisPos , opSet)) { String *removedWord = stringRemoveOperator(expression , opSet); //Remove operator in string tempStart = removedWord->startindex; tempLength = removedWord->length; char *opSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed operator become substring op = operatorNewByName(opSubString); op->line = stringSubString(expression , tempStart , tempLength); return (Token*)op; } }