Thread: line of code convert from C++ to C

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    line of code convert from C++ to 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?.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    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...

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    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;
    }

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    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:
    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.
    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.

    For example, say you wanted to append a string to another string, you could do:
    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.
    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 = 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...

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    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;
    }

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Comment each line and convert this C++ OOP code into C++ code.
    By Shayaan_Mustafa in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 01:23 PM
  3. Convert ASM to C Code
    By clag in forum C Programming
    Replies: 2
    Last Post: 01-28-2010, 03:48 AM
  4. convert vb.net code to C#
    By Nyoud33 in forum C# Programming
    Replies: 3
    Last Post: 01-11-2009, 03:29 PM
  5. Replies: 14
    Last Post: 04-01-2008, 02:23 AM

Tags for this Thread