int main(int argc, char** argv) { char copy[10]; char copy2[10]; char copy3[10]; memset(copy3,0,sizeof(copy3)); memset(copy2,0,sizeof(copy2)); char * result = stringCopy(copy, "SoftUni", 7); printf("%s\n", copy); //keep in mind that this like the default implementation of strncpy will not automatically add a null terminator if the size copied // is less than the original word, printing a non null terminated pointer may result in incorrect behavior // the null terminator of the original word will be copied if it's within the given size though stringCopy(copy2, "SoftUni", 3); printf("%s\n", copy2); // no bytes are overwritten so no null terminator is added, it is essentially printing garbage memory // in the standard implementation it's up to the user to make sure the destination contains no garbage data stringCopy(copy3, "C is cool", 0); printf("%s\n",copy3); //returning a char* from the function without using a buffer will return a pointer to garbage memory // one way to correct it is to malloc the memory before we return a pointer to it, // however then we would have to free it return (EXIT_SUCCESS); }
void inputTestCases() { int i; struct testcases test[12] = { {"1010","0101","1111"}, {"0000","0000","0000"}, {"1111 1111","i111 1111","error"}, {"0 0 0 0 0 0 0 0 0","0 0 1 1","0 0 0 0 0 0 0 1 1"}, {"11 00","00 11","11 11"}, {""," 111 ","111"}, {"1111 1111","","11 1111 11"}, {"11 11","1+ 11","error"}, {"1 0 101","1 01","1 1 010"}, {"1","1","1 0"}, {" 0 "," 1 "," 1 "}, {"","",""} }; char *finalresult,*myinput1,*myinput2,*myoutput; for(i=0;i<12;i++) { myinput1 = (char *)malloc(inputlen(test[i].input1)*sizeof(char)); myinput2 = (char *)malloc(inputlen(test[i].input2)*sizeof(char)); myoutput = (char *)malloc(inputlen(test[i].output)*sizeof(char)); stringCopy(myinput1,test[i].input1); stringCopy(myinput2,test[i].input2); stringCopy(myoutput,test[i].output); trimString(myoutput); finalresult = getResult(myinput1,myinput2); if(isEqual(finalresult,myoutput)) printf("pass\n"); else printf("fail\n"); } }
extern void setDefaultTagFileName (void) { if (Option.tagFileName != NULL) ; /* accept given name */ else if (Option.etags) Option.tagFileName = stringCopy (ETAGS_FILE); else Option.tagFileName = stringCopy (CTAGS_FILE); }
tABC_TxDetails * Metadata::toDetails() const { tABC_TxDetails *out = structAlloc<tABC_TxDetails>(); out->szName = stringCopy(name); out->szCategory = stringCopy(category); out->szNotes = stringCopy(notes); out->bizId = bizId; out->amountCurrency = amountCurrency; out->amountSatoshi = 0; out->amountFeesAirbitzSatoshi = 0; out->amountFeesMinersSatoshi = 0; return out; }
Config2Cpp::Config2Cpp(const char * progName) { m_progName = stringCopy(progName); m_cfgFileName = 0; m_schemaOverrideCfg = 0; m_schemaOverrideScope = stringCopy(""); m_className = 0; m_cppExt = 0; m_hExt = 0; m_wantSingleton = false; m_wantSchema = true; m_namespaceArraySize = 0; m_namespaceArray = 0; }
//Gets the last token of a list char *getLastToken (struct token *list) { struct token *current = list; while (current -> next) { current = current -> next; } return stringCopy(current -> text); }
int main(int argc, char **argv) { char **newArray; int argLength; int i; int j; argLength = argc - 1; i = 1; while (i < argLength) { newArray[i - 1] = stringCopy(argv[i]); i++; } i = 0; j = 0; while (newArray[i] != '\0') { while(newArray[i][j] != '\0') { ft_putchar(newArray[i][j]); j++; } i++; } return (0); }
void test_updateTheString_should_update_the_string_object_if_the_identifier_is_being_in_front_of_the_expression() { String *testTokenizer =malloc(sizeof(String)); testTokenizer->rawString = "num1+12"; testTokenizer->startIndex = 0; testTokenizer->length = 7; String *newTokenizer; Token *testToken; Operator *opeToken; Number *numToken; Identifier *testIdentifier= malloc (sizeof(Identifier)); testIdentifier->name = malloc (5); stringCopy("num1",testIdentifier->name,0,5); testIdentifier->type=IDENTIFIER; DefineElement element; element.ID = "num1"; element.actualID = "4"; getElement_ExpectAndReturn(DefineList, testIdentifier->name,&element); //Now start put the identifier and tokenizer into the function. newTokenizer = getFromListAndUpdate(testTokenizer,testIdentifier->name); TEST_ASSERT_EQUAL(4,newTokenizer->length); TEST_ASSERT_EQUAL(0,newTokenizer->startIndex); //check the string inside the String. TEST_ASSERT_EQUAL('4',newTokenizer->rawString[0]); TEST_ASSERT_EQUAL('+',newTokenizer->rawString[1]); TEST_ASSERT_EQUAL('1',newTokenizer->rawString[2]); TEST_ASSERT_EQUAL('2',newTokenizer->rawString[3]); TEST_ASSERT_EQUAL_STRING("4+12",newTokenizer->rawString); //Since the string been recreate try to get token from it until the last of the string. testToken = getToken(newTokenizer); //Should be 4. TEST_ASSERT_EQUAL(NUMBER,*testToken); numToken = (Number*)testToken; TEST_ASSERT_EQUAL(NUMBER,numToken->type); TEST_ASSERT_EQUAL(4,numToken->value); free(numToken); testToken = getToken(newTokenizer); //Should be +. TEST_ASSERT_EQUAL(OPERATOR,*testToken); opeToken = (Operator*)testToken; TEST_ASSERT_EQUAL(OPERATOR,opeToken->type); TEST_ASSERT_EQUAL(ADD,opeToken->id); free(opeToken); testToken = getToken(newTokenizer); //Should be 12. TEST_ASSERT_EQUAL(NUMBER,*testToken); numToken = (Number*)testToken; TEST_ASSERT_EQUAL(NUMBER,numToken->type); TEST_ASSERT_EQUAL(12,numToken->value); free(numToken); testToken = getToken(newTokenizer); //Should be NULL. TEST_ASSERT_NULL(testToken); free(newTokenizer); free(testIdentifier->name); free(testIdentifier); free(testTokenizer); }
char * stringAppend(char * dest, const char * src, size_t size) { size_t start = strlen(dest); size_t sizeLeft = size - start; return stringCopy(dest + start, src, sizeLeft); }
int main(void) { str_t str1,str2; str1=stringCrea(20,"hello "); str2=stringCrea(20,"world"); stringPrint(str1); putchar('\n'); printf("stringLeng:%d\n",stringLeng(str1)); switch(stringComp(str1,str2)){ case 0:puts("str1 = str2");break; case 1:puts("str1 > str2");break; case -1:puts("str1 < str2");break; } stringConcat(str1,str2); stringPrint(str1); putchar('\n'); stringCopy(str1,str2); stringPrint(str1); putchar('\n'); return(0); }
void setIdentifiantFichier(Identifiant i, const char * fichier) { if (fichier != NULL) { i->fichier = stringCopy(fichier); } }
void test_updateTheString_should_return_NULL_when_the_identifier_is_not_defined() { String testTokenizer; testTokenizer.rawString = "12+num1"; testTokenizer.startIndex = 0; testTokenizer.length = 7; String *newTokenizer; free(getToken(&testTokenizer)); free(getToken(&testTokenizer)); Identifier testIdentifier; testIdentifier.name = malloc (5); stringCopy("num1",testIdentifier.name,0,5); testIdentifier.type=IDENTIFIER; Error exception; getElement_ExpectAndReturn(DefineList, testIdentifier.name,NULL); //Now start put the identifier and tokenizer into the function. Try { newTokenizer = getFromListAndUpdate(&testTokenizer,testIdentifier.name); } Catch(exception) { TEST_ASSERT_EQUAL(UNDEFINED_IDENTIFIER,exception); TEST_ASSERT_EQUAL_STRING("Undefined Identifier! ",errorMessage.message); } free(testIdentifier.name); }
void setDocumentationRetour(Documentation d, const char * retour) { if (d->retour != NULL) { free(d->retour); } d->retour = stringCopy(retour); }
void setDocumentationBrief(Documentation d,const char * brief) { if (d->brief != NULL) { free(d->brief); } d->brief = stringCopy(brief); }
END_TEST START_TEST(stringCopyTest) { #line 21 char* source; char* dest; source = "test"; dest = stringCopy(source); fail_unless(strcmp(dest, source) == 0, "strcmp failed between dest and src"); free(dest); dest = stringCopy("test"); fail_unless(strcmp(dest, source) == 0, "strcmp failed between dest and src"); free(dest); dest = stringCopy(""); fail_unless(strcmp(dest, "") == 0, "strcmp failed between dest and an empty string"); }
status_t BHashMapCatalog::SetString(const char *string, const char *translated, const char *context, const char *comment) { BString stringCopy(string); BString translatedCopy(translated); parseQuotedChars(stringCopy); parseQuotedChars(translatedCopy); CatKey key(stringCopy.String(), context, comment); return fCatMap.Put(key, translatedCopy.String()); // overwrite existing element }
Identifiant creerIdentifiant(const char * nom) { Identifiant i = (Identifiant)malloc(sizeof(struct Identifiant_t)); i->nom = stringCopy(nom); if (i->nom == NULL) { } i->doc = NULL; i->fichier =NULL; return i; }
int main(){ char str1[100],str2[100]; printf("Enter any string:\n ") ; scanf("%s",str1); stringCopy(str1,str2); printf("After copying: %s \n",str2); return 0; }
static void collectPpmData() { // ber integer _streams[0].streamValue.choice.integer = ((rand() % 80) - 64) * 32; // from -64 to +15 in db/32 // octets containing big endian int16 *(short *)&_streams[1].streamValue.choice.octets.pOctets[0] = htons(((rand() % 256) - 128) * 32); // from -128 to +127 in db/32 // octets containing little endian int32 *(berint *)&_streams[1].streamValue.choice.octets.pOctets[4] = ((rand() % 256) - 255) * 32; // from -255 to 0 in db/32 stringCopy(_streams[2].streamValue.choice.pString, STREAMS_MAX_STRING_LENGTH, "abc"); _itoa_s(rand() % 100, &_streams[2].streamValue.choice.pString[3], STREAMS_MAX_STRING_LENGTH - 3, 10); }
/** * Gets the bit coin public address for a specified request * * @param szRequestID ID of request * @param pszAddress Location to store allocated address string (caller must free) * @param pError A pointer to the location to store the error if there is one */ tABC_CC ABC_TxGetRequestAddress(Wallet &self, const char *szRequestID, char **pszAddress, tABC_Error *pError) { tABC_CC cc = ABC_CC_Ok; Address address; ABC_CHECK_NEW(self.addresses.get(address, szRequestID)); *pszAddress = stringCopy(address.address); exit: return cc; }
//Returns the text for the token at possition pos char *getTokenForPos (struct token *list, int pos) { int count = 0; struct token *current = list; while (count != pos) { if (current -> next) { current = current -> next; } else { return NULL; } count ++; } return stringCopy(current -> text); }
void parse_membres(char *chaine,cmd *c){ unsigned int i=0; char * cTmp=NULL, * cTok=NULL; // stringCopy(c->cmd_initial, chaine); c->cmd_initial = chaine; c->cmd_membres = (char **) malloc(sizeof(char *)*1); if(c->cmd_membres==NULL){ perror("allocation raté // parse_membres"); exit(EXIT_FAILURE); } stringCopy(&cTok, c->cmd_initial); cTmp = strtok(cTok, "|"); while( cTmp != NULL){ // réallocation du tableau c->cmd_membres = (char **) realloc(c->cmd_membres, sizeof(char *)*(i+1)); if(c->cmd_membres==NULL){ perror("réallocation raté // parse_membres"); exit(EXIT_FAILURE); } c->cmd_membres[i]=NULL; // affectation stringCopy(&(c->cmd_membres[i]), cTmp); //extraction du membres suivants cTmp = strtok(NULL, "|"); i++; } c->nb_membres = i; free(cTok); free(cTmp); }
/** @brief Send a debug message * * Sends a null terminated debug message out on the broadcast address of all available interfaces. * * @author Fred Cooke * * @warning ONLY use this function from within the communication handler. * * @see sendDebugIfClear() * @see sendDebugBusyWait() * * @note This function exists as a convenience to developers, do not publish code that calls this function. * * @todo TODO clean up the mess of commented out crap in here! * * @param message is a pointer to the null terminated debug message string. */ void sendDebugInternal(unsigned char* message){ // set buffer in use, consider blocking interrupts to do this cleanly // if(TRUE){ // Counters.serialDebugUnsentCounter++; // return; // } // wrong : /* No need for atomic block here as one of two conditions will always be */ /* true when calling this. Either we have been flagged to receive and */ /* decode a packet, or we are in an ISR. In either case it is safe to */ /* check the flags and initiate the sequence if they are clear. */ //if(RXTXSerialStateFlags & TX_IN_PROGRESS){ // wrong : /* It's OK to return without resetting as it will be done by */ /* either of those processes if they are underway. The other */ /* processes are not overridden because they have priority. */ //TXBufferInUseFlags = 0; //return; // }else{ /* Turn off reception */ /* It's OK to turn this off if nothing was currently being received */ // SCI0CR2 &= SCICR2_RX_ISR_DISABLE; // SCI0CR2 &= SCICR2_RX_DISABLE; /* Build up the packet */ /* Set the pointer to the start and init the length */ TXBufferCurrentPositionHandler = (unsigned char*)&TXBuffer; /* Load a protocol with length header into the TX buffer ready for masking */ *TXBufferCurrentPositionHandler = 0x11; TXBufferCurrentPositionHandler++; /* Set the payload ID */ *((unsigned short*)TXBufferCurrentPositionHandler) = asyncDebugInfoPacket; TXBufferCurrentPositionHandler += 2; /* Store the length location */ unsigned short* TXLength = (unsigned short*)TXBufferCurrentPositionHandler; TXBufferCurrentPositionHandler += 2; /* Copy the string into place and record the length copied */ unsigned short messageLength = stringCopy(TXBufferCurrentPositionHandler, message); *TXLength = messageLength; TXBufferCurrentPositionHandler += messageLength; finaliseAndSend(0); //} }
void parse_distant(cmd *c){ unsigned int i = 0; char * str, * cTok, * cTmp; if(c->distant==NULL){ c->distant = (char ***) malloc(sizeof(char **) * c->nb_membres); if(c->distant==NULL){ perror("allocation raté // parse_distant"); exit(EXIT_FAILURE); } for(i = 0; i<c->nb_membres; i++){ // c->distant[i] = NULL; c->distant[i] = (char **) malloc(sizeof(char *) * 2); if(c->distant[i]==NULL){ perror("allocation raté // parse_distant"); exit(EXIT_FAILURE); } str = strstr(c->cmd_membres[i], "s:"); if(str!= NULL){ stringCopy(&cTok, c->cmd_membres[i]); cTmp = strtok(cTok, ":"); cTmp = strtok(NULL, ":"); stringCopy(&(c->distant[i][0]), cTmp); cTmp = strtok(NULL, " "); stringCopy(&(c->distant[i][1]), cTmp); free(cTok); }else{ c->distant[i][0] = NULL; c->distant[i][1] = NULL; } } } }
int main() { const int S_LENGTH = 30; char dest[S_LENGTH], source[S_LENGTH]; std::cout << "Enter a string with no more than " << S_LENGTH - 1 << " charactesrs : " << std::endl; std::cin.getline(source, S_LENGTH); stringCopy(dest, source); std::cout << "The string you entered is:" << std::endl << dest << std::endl; return 0; }
int getProductCardType(U8_t id, char* type, int bufferLength) { int returnValue=false; MYKI_CD_Product_t product; if (getProduct(id, product) == false) { CsDebug(CD_DEBUG, (CD_DEBUG, "MYKI_CD:getProductCardType: getProduct failed, returning fail")); } else { stringCopy(type, product.type, bufferLength); returnValue = true; } return returnValue; }
static gint xdmcp_add_text(proto_tree *tree, const char *text, tvbuff_t *tvb, gint offset) { const guint8 *p; char *str; guint len; len = tvb_get_ntohs(tvb, offset); p = tvb_get_ptr(tvb, offset+2, len); str = g_malloc(len+1); stringCopy(str, (gchar*)p, len); proto_tree_add_text(tree, tvb, offset, len+2, "%s: %s", text, str); g_free(str); return len+2; }
//Creates a string out of all the texts in a token list //Excluding the first token char *convertAllButFirst (struct token *list) { struct token *current = list; char *outputString = malloc(1); outputString[0] = '\0'; while (current -> next) { if (outputString[0]) { char *lineSpace = malloc(2); lineSpace[0] = ' '; lineSpace[1] = '\0'; outputString = combine(outputString, lineSpace); } char *tokenText = stringCopy(current -> next -> text); outputString = combine(outputString, tokenText); current = current -> next; } return outputString; }
NS_IMETHODIMP nsIOService::EscapeString(const nsACString& aString, uint32_t aEscapeType, nsACString& aResult) { NS_ENSURE_ARG_MAX(aEscapeType, 4); nsAutoCString stringCopy(aString); nsCString result; if (!NS_Escape(stringCopy, result, (nsEscapeMask) aEscapeType)) return NS_ERROR_OUT_OF_MEMORY; aResult.Assign(result); return NS_OK; }
void test_updateTheString_should_update_the_string_object_as_defined_by_user() { String *testTokenizer =malloc(sizeof(String)); testTokenizer->rawString = "12+num1"; testTokenizer->startIndex = 0; testTokenizer->length = 7; String *newTokenizer; Token *testToken; Number *numToken; free(getToken(testTokenizer)); free(getToken(testTokenizer)); Identifier *testIdentifier= malloc (sizeof(Identifier)); testIdentifier->name = malloc (5); stringCopy("num1",testIdentifier->name,0,5); testIdentifier->type=IDENTIFIER; DefineElement element; element.ID = "num1"; element.actualID = "4"; getElement_ExpectAndReturn(DefineList, testIdentifier->name,&element); //Now start put the identifier and tokenizer into the function. newTokenizer = getFromListAndUpdate(testTokenizer,testIdentifier->name); TEST_ASSERT_EQUAL(1,newTokenizer->length); TEST_ASSERT_EQUAL(3,newTokenizer->startIndex); //check the string inside the String. TEST_ASSERT_EQUAL('1',newTokenizer->rawString[0]); TEST_ASSERT_EQUAL('2',newTokenizer->rawString[1]); TEST_ASSERT_EQUAL('+',newTokenizer->rawString[2]); TEST_ASSERT_EQUAL('4',newTokenizer->rawString[3]); TEST_ASSERT_EQUAL_STRING("12+4",newTokenizer->rawString); //Since the string been recreate try to get token from it until the last of the string. testToken = getToken(newTokenizer); //Should be 4. TEST_ASSERT_EQUAL(NUMBER,*testToken); numToken = (Number*)testToken; TEST_ASSERT_EQUAL(NUMBER,numToken->type); TEST_ASSERT_EQUAL(4,numToken->value); free(numToken); testToken = getToken(newTokenizer); //Should be NULL. TEST_ASSERT_NULL(testToken); free(newTokenizer); free(testIdentifier->name); free(testIdentifier); free(testTokenizer); }