plog
plog copied to clipboard
Some clarification on wide vs narrow string on linux
HI, I'm probably missing something, but...
I am trying to use plog in a program that targets both windows and linux. Windows build is fine so far, and if I understand correctly, it uses these settings:
#define PLOG_ENABLE_WCHAR_INPUT 1
#define PLOG_CHAR_IS_UTF8 0
When I try to use these in linux, I am having issues with the file access functions being narrowchar for linux, e.g. in Utils:
size_t open(const nstring& fileName)
{
#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))
m_file = ::_wsopen(toWide(fileName).c_str(), _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, SH_DENYWR, _S_IREAD | _S_IWRITE);
#elif defined(_WIN32)
::_wsopen_s(&m_file, toWide(fileName).c_str(), _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, _SH_DENYWR, _S_IREAD | _S_IWRITE);
#elif defined(O_CLOEXEC)
m_file = ::open(fileName.c_str(), O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#else
m_file = ::open(fileName.c_str(), O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#endif
return seek(0, SEEK_END);
}
however nstring was already defined as wchar_t due to the defines above. Do i need to keep linux using narrowstring?
Oh, you've found a bug! To be more precise there is a missing check for the unsupported configuration. On Linux you need PLOG_CHAR_IS_UTF8 to be always 1. wchar_t is not native to Linux, so there is no sense in nstring based on it.
Thank you, clarifying the intent behind the implementation sets me on the right path. I am not sure if I fully agree with the "no sense" part because it would create a simpler, more elegant API if the user doesn't care about the cost of the occasional conversion from wstring to string for file system operations, and for those who care can still use the preprocessor flags.
But I can work with this. ;)
I don't know if to close this, please close as needed unless you need it to track the missing check.
I don't know if to close this, please close as needed unless you need it to track the missing check.
Yes, don't close it. I'll add the missing check and a note in the documentation.
I am not sure if I fully agree with the "no sense" part because it would create a simpler, more elegant API
Oh, could you elaborate more about that?
Well, I had to go through my code and find all references to plog and either address them through a set of typedefs or conditional compiles. I don't like conditional compiles, they make the code ugly. :) In my case I'd gladly accept the performance penalty if you had an overload in linux that accepted wstring and took care of downcasting to UTF8 string for me.
Also, i think that the wstring linux problem is mostly around the filesystem, so rest of it would not have a performance penalty since conversion would not be needed? I could be wrong, didn't read the whole code.
I mean, the work wasn't too bad, but it made my code a little messier.
I also can't quite understand how the two defines play together. I mean, I kinda do, but there are four possible combinations of those preprocessor vars, and my head can't wrap around what all combinations result in.
@vpopescu Hm... exactly the interface should be stable in both scenarios: wide and narrow. Could you post your code snippet?
#define PLOG_ENABLE_WCHAR_INPUT 1
This define controls if plog can accept wide chars. Not all Linux/Unix systems have wide chars, so here is the switch.
Sorry have not had time to look into this further, so who knows, maybe I'm confused. ;)
My problem stems from the function at the top of this thread.. Since that open() function calls _open() without any conversions, it would only work when nchar is narrowchar.
I am trying to avoid conditionals based on PLOG_ENABLE_WCHAR_INPUT and PLOG_CHAR_IS_UTF8 , and want to be able to just use all wchar_t on all platforms (and some people may want the opposite).
Should PLOG_ENABLE_CHAR_INPUT=1 and PLOG_CHAR_IS_UTF8=0 work on linux? How would it be handled by the above function?