Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24326184
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/utils/strutils.h b/src/utils/strutils.h
index 86ee43b..ca3b750 100644
--- a/src/utils/strutils.h
+++ b/src/utils/strutils.h
@@ -1,107 +1,112 @@
/* -------------------------------------------------------------
RabbitMQ TCL - String utilities header library
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
___ __ \_____ ___ /____ /____(_)_ /___ |/ /_ __ \
__ /_/ / __ `/_ __ \_ __ \_ /_ __/_ /|_/ /_ / / /
_ _, _// /_/ /_ /_/ / /_/ / / / /_ _ / / / / /_/ /
/_/ |_| \__,_/ /_.___//_.___//_/ \__/ /_/ /_/ \___\_\
_____________________
RabbitMQ C AMQP client library TCL wrapper ___ __/_ ____/__ /
TCL module to connect to AMQP brokers. __ / _ / __ /
_ / / /___ _ /___
(c) 2015, Nasqueron, some rights reserved. /_/ \____/ /_____/
Released under BSD-2-Clause license.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Software: RabbitMQ TCL
Author: Sébastien Santoro aka Dereckson
Filename: strutils.h
Created: 2015-12-08
Licence: BSD-2-Clause
------------------------------------------------------------- */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* -------------------------------------------------------------
Magic constants
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define STR_NOT_FOUND -1
/* -------------------------------------------------------------
Common string functions inspired by PHP standard functions:
strpos
str_replace
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Finds the position of the first occurrence of a substring in a string
*
* @param[in] haystack The string to search in
* @param[in] needle The string to search
* @return The position of where the needle exists, or STR_NOT_FOUND
*/
int strpos(const char *haystack, const char *needle) {
// http://stackoverflow.com/a/7655509/1930997 - snippet by Miere
char *p = strstr(haystack, needle);
if (p)
return p - haystack;
return STR_NOT_FOUND;
}
/**
* Replaces all occurrences of the search string with the replacement string
*
* @param[in] needle The value being searched for (search)
* @param[in] replace The replacement value
* @param[in] haystack The string being searched and replaced on (subject)
* @return A string with the replaced values
*/
char *str_replace(const char *needle, const char *replace, char *haystack) {
// This code is based on a str_replace function designed by
// Chantra <chantra@debuntu.org> and Iain R. Learmonth <irl@sdf.org>
- // Source: https://github.com/irl/la-cucina/blob/master/str_replace.c
+ // Source: https://github.com/irl/la-cucina/blob/master/str_replace.c
- char *tok = NULL;
- char *newstr = NULL;
- char *oldstr = NULL;
- int oldstr_len = 0;
- int needle_len = 0;
- int replacement_len = 0;
+ char *token = NULL;
+ char *replacedString = NULL;
+ char *oldString = NULL;
- newstr = strdup(haystack);
- needle_len = strlen(needle);
- replacement_len = strlen(replace);
+ int oldStringLen = 0;
+ int needleLen = 0;
+ int replaceLen = 0;
- if (needle == NULL || replace == NULL || needle_len == 0) {
- return newstr;
+ int len; // Stores len before a malloc call
+
+ replacedString = strdup(haystack);
+ needleLen = strlen(needle);
+ replaceLen = strlen(replace);
+
+ if (needle == NULL || replace == NULL || needleLen == 0) {
+ return replacedString;
}
- while ((tok = strstr(newstr, needle))) {
- oldstr = newstr;
- oldstr_len = strlen(oldstr);
- newstr = (char *)malloc(
- sizeof(char) * (oldstr_len - needle_len + replacement_len + 1));
+ while ((token = strstr(replacedString, needle))) {
+ oldString = replacedString;
+ oldStringLen = strlen(oldString);
+
+ len = oldStringLen - needleLen + replaceLen + 1;
+ replacedString = (char *)malloc(sizeof(char) * len);
- if (newstr == NULL) {
- free(oldstr);
+ if (replacedString == NULL) {
+ free(oldString);
return NULL;
}
- memcpy(newstr, oldstr, tok - oldstr);
- memcpy(newstr + (tok - oldstr), replace, replacement_len);
- memcpy(newstr + (tok - oldstr) + replacement_len, tok + needle_len,
- oldstr_len - needle_len - (tok - oldstr));
- memset(newstr + oldstr_len - needle_len + replacement_len, 0, 1);
+ memcpy(replacedString, oldString, token - oldString);
+ memcpy(replacedString + (token - oldString), replace, replaceLen);
+ memcpy(replacedString + (token - oldString) + replaceLen,
+ token + needleLen,
+ oldStringLen - needleLen - (token - oldString));
+ memset(replacedString + oldStringLen - needleLen + replaceLen, 0, 1);
- free(oldstr);
+ free(oldString);
}
- return newstr;
+ return replacedString;
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Feb 16, 08:35 (20 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3451611
Default Alt Text
(5 KB)
Attached To
Mode
rRABBITMQTCL RabbitMQ TCL extension
Attached
Detach File
Event Timeline
Log In to Comment