This is a line of C++ code.
s.append(str, pos - str - 1);
This is how the the variables are defined.
char *s;
const char *str;
const char *pos = str;
I want to convert this line for use in C.
What do I need to change so it works in C?.
This is a line of C++ code.
s.append(str, pos - str - 1);
This is how the the variables are defined.
char *s;
const char *str;
const char *pos = str;
I want to convert this line for use in C.
What do I need to change so it works in C?.
Are those definitions the original code or did you try converting it from C++? I'm asking because I don't think they do what you think they do. Why don't you show us the entire code you want to convert and we'll see what we can do.
Devoted my life to programming...
This is the original C++
Code:String String::msgEscape(const char* str, char extraEsc) { String s; if (TelEngine::null(str)) return s; char c; const char* pos = str; char buff[3] = {'%', '%', '\0'}; while ((c=*pos++)) { if ((unsigned char)c < ' ' || c == ':' || c == extraEsc) c += '@'; else if (c != '%') continue; buff[1] = c; s.append(str,pos - str - 1); s += buff; str = pos; } s += str; return s; }
This is my attempt to convert to C.
Obvoulsy I need to convert the c++ append function to strcat or something.
Code:char *msgEscape(const char *str, char extraEsc) { char *s; if ((s != NULL) && (s[0] == '\0')) { return s; } char c; const char *pos = str; char buff[3] = {'%', '%', '\0'}; while ((c = *pos++)) { if ((unsigned char)c < ' ' || c == ':' || c == extraEsc) { c += '@'; } else if (c != '%') { continue; } buff[1] = c; s.append(str, pos - str - 1); s += buff; str = pos; s += buff; str = pos; } s += str; return s; }
This is the original C++ code
Code:String String::msgEscape(const char* str, char extraEsc) { String s; if (TelEngine::null(str)) return s; char c; const char* pos = str; char buff[3] = {'%', '%', '\0'}; while ((c=*pos++)) { if ((unsigned char)c < ' ' || c == ':' || c == extraEsc) c += '@'; else if (c != '%') continue; buff[1] = c; s.append(str,pos - str - 1); s += buff; str = pos; } s += str; return s; }
This is my attempt to convert to C.
obviously append function need to be replaced by strcat or something
Code:char *msgEscape(const char *str, char extraEsc) { char *s; if ((s != NULL) && (s[0] == '\0')) { return s; } char c; const char *pos = str; char buff[3] = {'%', '%', '\0'}; while ((c = *pos++)) { if ((unsigned char)c < ' ' || c == ':' || c == extraEsc) { c += '@'; } else if (c != '%') { continue; } buff[1] = c; s.append(str, pos - str - 1); s += buff; str = pos; } s += str; return s; }
Wait, let's take a step back for a moment. Before converting the code, we need to understand what it really does.
It seems to me that the code escapes most ASCII control characters by adding 64( '@' ) to them and appending a percent sign( '%' ) before them. Doing the same in C can be more complicated because you don't know the final string size beforehand. I wouldn't try to convert it if I were you, I'd re-implement it for C from "scratch". The algorithm would be:
Don't get me wrong, there is a way to convert it directly to C from C++, but I don't know which would be better. It would involve calls to realloc, strlen, strcat and/or a whole lot of size and pointer management.Code:1) Calculate the required size for the escaped string ( 2 bytes if (c < ' ' || c == ':' || c == extraEsc || c == '%'), 1 otherwise ). 2) Allocate the required amount of memory. 3) Convert the original string to the escaped version, by using a similar method.
For example, say you wanted to append a string to another string, you could do:
in this case though we already know the current size of both the escaped string and the characters we want to append, so it could be like:Code:// 's' must already be a dynamically allocated string s = realloc(s, strlen(s) + strlen(str) + 1); // Need to extend the string's memory first strcat(s, str); // Then append the new string at the end.
Code:s = NULL; curSize = 0; while ((c = *pos++)) { if (c < ' ' || c == ':' || c == extraEsc || c == '%') { curSize += 2; s = realloc(s, curSize); s[curSize-2] = '%'; if (c != '%') { s[curSize-1] = c + '@'; } else { s[curSize-1] = c; } } else { curSize += 1; s = realloc(s, curSize); s[curSize-1] = c; } } s = realloc(s, curSize + 1); s[curSize] = '\0';
Last edited by GReaper; 09-25-2017 at 06:38 AM.
Devoted my life to programming...
Perfect. It works. Thank you
Code:char *msgEscape(const char *str, char extraEsc) { char *s; char c; const char *pos = str; int curSize = 0; if ((s != NULL) && (s[0] == '\0')) { return s; } while ((c = *pos++)) { if (c < ' ' || c == ':' || c == extraEsc || c == '%') { curSize += 2; s = realloc(s, curSize); s[curSize-2] = '%'; if (c != '%') { s[curSize-1] = c + '@'; } else { s[curSize-1] = c; } } else { curSize += 1; s = realloc(s, curSize); s[curSize-1] = c; } } s = realloc(s, curSize + 1); s[curSize] = '\0'; return s; }
Oh, I provided an almost complete solution without even realizing it...
I just wanted to add that you should set 's' to NULL before the loop starts and that the if statement at the top (line #9) is totally useless in this case.
Devoted my life to programming...