开发者

C++: Correct implementation for passing a std::string to a C function that wants to change the string?

开发者 https://www.devze.com 2023-03-25 00:26 出处:网络
I have a function in a third-party library written in C: char* fix_filename_slashes(char* path). This function expects a mutable C-string passed to it so it can change all the slashes in the path to t

I have a function in a third-party library written in C: char* fix_filename_slashes(char* path). This function expects a mutable C-string passed to it so it can change all the slashes in the path to the correct use based on the operating system. All the strings I'm using in my 开发者_JAVA百科Facade are declared as std::strings. I attempted to simply use foo.c_str() as every other function that expects a C string doesn't change it and expects a const char *, but this function causes an error: Error: Argument of type "const char *" is incompatible with parameter of type "char *"

Is the result I came up with:

char* tempf = const_cast<char*>(filename.c_str());
filename = std::string(fix_filename_slashes(tempf));
tempf = NULL;

considered "correct" or are there other (more correct?) ways to accomplish the task?

EDIT

Whups. Apparently the function returns a COPY of the string. Still there are some nice answers already given.


If the string length does not change, you can use a pointer to the first character of the string. This is undefined behavior in the C++03 standard, but all known implementations work properly and it is explicitly allowed under the C++11 standard.

fix_filename_slashes(&filename[0]);

If the size of the string may change, you'll have to do a little more work.

filename.resize(max_size, 0);
append_filename_suffix(&filename[0]);
filename.resize(strlen(filename.c_str()));


Convert it into a null-terminated sequence of characters stored in a std::vector:

template <typename Character>
std::vector<Character> to_vector(std::basic_string<Character> const& s)
{
    std::vector<Character> v;
    v.reserve(s.size() + 1);
    v.insert(v.end(), s.begin(), s.end());
    v.push_back(0);
    return v;
}

Usage example:

std::string filename = get_filename();
std::vector<char> filename_cstr = to_vector(filename);
filename = std::string(fix_filename_slashes(&filename_cstr[0]));


Since you are going to all the trouble you could just comply with the requirements of the C function and copy your string to a char array then after the function create a string from the char array or force a copy assignment on your original string.

    char* temp = new char[str.size() + 1]
    // Force a copy of the result into another string
    str = (const char*)fix_filename_slashes(strncpy(temp, str.c_str(), str.size() + 1));
    delete [] temp;


If the string used a separate buffer to store the c_str string this wouldn't modify the original string.

Better is to create a char buffer on the stack or heap, copy the characters into it (null terminated), call the fix function, and then assign the buffer back into the string.


Here's another approach that takes a little setup, but works automatically after that. It relies on a temporary object, which takes a copy of the original string and copies the modified string back in the destructor. Obviously all this copying won't be too efficient, but in most cases the efficiency won't matter.

class mutable_string
{
public:
    mutable_string(std::string & str, int maxlen = 0) : m_str(str)
    {
        m_buffer.resize(max(maxlen, str.length()) + 1);
        memcpy(&m_buffer[0], str.c_str(), str.length()+1);
    }
    ~mutable_string()
    {
        m_str = m_buffer;
    }
    operator char* ()
    {
        return &m_buffer[0];
    }
private:
    std::string &     m_str;
    std::vector<char> m_buffer;
};

fix_filename_slashes(mutable_string(filename));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号