Question

I am working with the communication for some TCP/IP connected equipment in C++. The equipment requires that the commands sent are ended with \r\n.

I am using a configuration file from which I am reading the commands used in the communication.

The problem I have is that the commands \r\n are interpreted as the 4 characters they are and not as carriage return and line feed.

I have tried to use the string.data() but I get the same result as string.c_str().

Is there any nice function to get it correct from the beginning or do I have to solve this with a normal replace function? Or some other way that I have not thought about?

I guess, if I don't find a really neat way to do this I will just omitt the \r\n in the configuration file and add it afterwards, but it would be nice to have it all in the configuration file without any hard coding. I think I would need to do some hard coding as well if I would try to replace the four characters \r\n with their correct characters.

Thanks for any help

Edit: The config file contains lines like this one.

MONITOR_REQUEST = "TVT?\r\n"

Was it helpful?

Solution

If the data in the configuration file requires translation, you have to translate it. Short of regular expressions (which are clearly overkill for this), I don't know of any standard function which would do this. We use something like:

std::string
globalReplace(
    std::string::const_iterator begin,
    std::string::const_iterator end,
    std::string const& before,
    std::string const& after )
{
    std::string retval;
    std::back_insert_iterator<std::string> dest( retval );
    std::string::const_iterator current = begin;
    std::string::const_iterator next
            = std::search( current, end, before.begin(), before.end() );
    while ( next != end ) {
        std::copy( current, next, dest );
        std::copy( after.begin(), after.end(), dest );
        current = next + before.size();
        next = std::search( current, end, before.begin(), before.end() );
    }
    std::copy( current, next, dest );
    return retval;
}

for this. You could call it with "\\r\\n", "\r\n" for the last to arguments, for example.

OTHER TIPS

Sounds like your configuration file actually contains 4 distinct characters '\', 'r', '\', and 'n'... you must either change the file (e.g. by using actual line endings in the file to encode line endings in the strings, and even then some systems won't happen to use \r\n as their text file line delimiters), or do some replace/translation as you suggest. The translation is the more portable approach. It can also be pretty fast... just maintain two pointers/iterators/indices into the string - one for the read position and one for the write, and copy across while compacting the escape notation.

You need not had code anything in code. You can very well read the characters to append to outgoing string from the same configuration file.

Assuming that you have name value pair in your conf file :

APPEND_EOL="\n\r" str_to_send1="this is a test%%APPEND_EOL%%"

while parsing the line before sending you can generate the actual line. In case you can't store the APPEND_EOL in the same line, then you can pick it up from ENV with a default in case it is not defined, or, maybe in another config file of yours.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top