Filename vs String
On 28/07/2017 at 22:47, xxxxxxxx wrote:
Cinema 4D Version: R17, R18
Platform: Windows ;
Language(s) : C++ ;
Until now I have only needed to access files in my Python plugins, and didn't encounter an issue then.
In C++, however, it seems there's something odd. And as I guess this is a very basic feature used by a large number of plugins (?), it would have been brought up earlier. Since I couldn't find anything related, and as assumed this is a frequently used feature, I suppose this is not a bug but a user error ... hence the reason I 'm bringing this up here and not in the bug report part of the forum.
So, please, educate me. What am I doing wrong?
// step 1. get the plugin path, and append subfolder and filename Filename path = GeGetPluginPath(); path += "//subfolder"; path += "//filename.txt"; // appending seem to have no effect, as "path" still only contains // the original plugin path // step 2. get the plugin path as a string and append subfolder and filename as strings String pathStr = GeGetPluginPath().GetString(); pathStr += "\\subfolder"; pathStr += "\\filename.txt"; // appending is successful, now get it into a Filename path.SetString(pathStr); // bingo !!! // step 3. String subfolder = "subfolder"; String filename = "filename"; Filename fullPath = GeGetPluginPath() + "//" + subfolder + "//" + filename + ".txt"; // -> results in "C:/Users/.../plugins/pluginname/subfolder/filename/.txt" // why the extra / between filename and .txt ???
Why does step 1 not work?
Why do I need to construct a path via String as shown in step 2. This works on Windows, but does it also work on MacOS?
And why do I get that weird result in step 3
Thanks in advance.
On 31/07/2017 at 06:43, xxxxxxxx wrote:
in your step 1 you are using double slashes ("//"), is there a special reason to do so? I mean, it's not like in the case of double backslash ("\"). In the later case, the point is, that the backslash is used by C/C++ to initiate a control sequence (often called escaping). So a single backslash isn't interpreted as a character of the string, but instead the begin of a control sequence. Like for example non-printable characters, a byte (lets ignore wider characters here) has 256 possible values and only a fraction of these values represent printable characters. In order to be able to write these non-printable characters, escaping is used. For example '\0' is a null byte, usually terminating a string, or '\n' representing a newline. And of course, now, that '' is used for something else than having a backslash in the string, '\' is used for a backslash. But a slash ('/') is just a printable slash character.
Sorry if I'm telling something you already know. And sorry for using more words than probably needed...
Ok, back to your actual problem:
In step 2 you are only working with String functions (especially the += operator of the String class) and in the end the string gets converted into a Filename, so this one is straight forward. Using backslash as directory separator, this is a Win only solution.
Step 1 and step 3 basically suffer from the same issue. There you are not concatenating strings, but instead you use the + operator of the Filename class. This will always only add one sub-directory/filename at a time and it will take care of introducing the directory separator. Behavior when adding a Filename consisting of more parts is undefined. So having the (double-)slashes in the beginning of the parts you want to add, results in basically adding nothing. And in step 3 that's the added slash between filename and suffix.
In order to properly (and OS independently) build a filename, rather write something like this:
Filename fn = GeGetPluginPath() + Filename("subfolder") + Filename("filename.txt");
Note the use of Filename("") in order to assure the Filename + operator is used and it's not strings being concatenated.
I agree we need to enhance the API reference here. Added it to our ToDo list. On the other hand the Filename manual should do as a starter.
On 31/07/2017 at 11:26, xxxxxxxx wrote:
Don't worry about the lengthy introduction explanation of double slash-backslash. I know about escaped characters, but always mix up slash and backslash. Useful information is always welcome. And if not for me, then for anyone else stumbling upon this thread while doing a forum search.
THe other day I was playing with the SDKBrowser, and found following post in the forum:
That's where I probably picked up the double slashes ... assuming it was the double backslash (windows) for separating folders.
I know this post I linked to is about the preset library, but as tests didn't work out as expected I further tried with regular files, and that's when I came upon issues when combining subfolders, filenames, extensions using the Filename class.
Note that I still assumed the double slashes from the preset protocol to be the folder separator (double backslash in windows).
I now understand my erroneous assumption ...
Pretty obvious if it's well explained. Thank you for that.