Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with file path strings on Linux, what encoding to use?

What encoding does Linux use for its file APIs? How should I work with path strings in C++, what class to use? I mean paths with non-ASCII characters. On Windows I use UTF-16 and std::wstring, on Mac - UTF-8 and my own UTF-8 string class. But unfortunately my class is not available on Linux, so what should I use?

like image 967
Violet Giraffe Avatar asked Jul 21 '26 07:07

Violet Giraffe


2 Answers

Internally, Linux permits to use any byte sequence for file name, except for null byte 0 and forward slash '/' (which is used as directory separator).

Common convention to permit Unicode file names on Linux is to use UTF-8 encoding for file names. Easiest way to achieve that is to use good old std::string (not std::wstring which is suggested on Windows), however, you may need to write your own class which will validate that it is indeed valid UTF-8.

There are few examples of ready-to-use libraries that provide handling of UTF-8 strings:

  • ICU (robust but very heavy).
  • Glib::ustring (has implicit casts to std::string, GPL).
  • UTF8-CPP (very lightweight, header-only).
like image 117
mvp Avatar answered Jul 23 '26 21:07

mvp


Linux does not enforce an encoding on file names. Using UTF-8 is common though.

like image 40
Prof. Falken Avatar answered Jul 23 '26 22:07

Prof. Falken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!