-1

What it says on the tin. Is there a cleverer way to replace white spaces in a boost::fs::path that does not require a regex?

EDIT as an example:

_appBundlePath = boost::fs::path("/path/with spaces/here");
regex space(" ");
string sampleFilename = regex_replace((_appBundlePath/"audio/samples/C.wav").string(), space, "\\ ");

Question: is there a way that avoids using a regex? Seems like an overkill to me.

EDIT 2 My issue is when passing a string to Pure Data via libpd. PD will interpret a space as a separator, so my string will be chopped up into multiple symbols. Surrounding it with double quotes won't work, and I'm not even sure that escaping white space would, but it's worth a shot.

Morpheu5
  • 2,610
  • 6
  • 39
  • 72
  • [X/Y problem](http://xyproblem.info/) is when you ask questions about the chosen solution path instead of about the goal. In this case, the question seems to be X/.../Z since you ask about the solution path you think you wish to avoid, but don't specify the goal. – sehe Jan 23 '17 at 11:24
  • Any specific API call in mind? – sehe Jan 23 '17 at 14:21
  • Not sure what you mean… – Morpheu5 Jan 23 '17 at 14:38
  • "EDIT 2 My issue is when passing a string to Pure Data ***via libpd***". That's a library. What _function_ do you wish to use. Because it's the function that determines what input is required/accepted. – sehe Jan 23 '17 at 23:11
  • I see. Given I'm dealing with the C++ wrapper, this is the method I'm calling (https://github.com/libpd/libpd/blob/master/cpp/PdBase.cpp#L237) which in turn calls this function (https://github.com/libpd/libpd/blob/master/libpd_wrapper/z_libpd.c#L331). I do honestly fail to see how any of this is relevant to my original question, though. – Morpheu5 Jan 24 '17 at 11:39
  • Quick glance through documentation of symbols or [symbolic atoms](https://puredata.info/dev/HansDataTypeDefinitions) ([§2.1.2](http://msp.ucsd.edu/Pd_documentation/x2.htm#s1.2) etc.) seems to suggest that no escaping is necessary (anything non-number is a valid atom). Perhaps on the other end a channel is interpreting the string as a path and DoingTheWrongThing. There's no guarding against that, you can only /guess/ what kind of escaping helps (unless you can see their implementation. In which case you should probably fix that part instead) – sehe Jan 24 '17 at 12:44
  • But if you've ever used PD, you'd know that spaces are used to identify different atoms, so if you do something like [send /this/path/with a/space/file.wav], the result is that you get a list of two symbols: "/this/path/with", and "a/space/file.wav". You are right that I can only guess, and that's exactly what I was trying to do, hence why I asked if there was a simpler/cleverer way of substituting spaces to escaped spaces. And yes, I tried surrounding the string with double quotes, that does not work either. – Morpheu5 Jan 26 '17 at 15:23
  • That does *not* look like a call to `int libpd_symbol(const char *recv, const char *sym)`. It looks like "send"-ing a symbol `/this/path/with a/space/file.wav` (among other API calls, perhaps). Have you made it working with paths without spaces? – sehe Jan 26 '17 at 15:32
  • A call to `int libpd_symbol(const char *recv, const char *sym)` and "send"-ing a symbol is exactly the same thing, which you would know if you'd ever used PD. My point precisely: there was no need from the beginning to reveal that the path was going to be sent to PD. And yes, I made it work without spaces. – Morpheu5 Jan 26 '17 at 15:37
  • Stop bashing me. You're making a thinko. If that is exactly the same thing, then you MUST just say `int result = libpd_symbol(my_receiver, "/path/with spaces/here");`. There's no way you need to escape that on the C++ side. – sehe Jan 26 '17 at 15:40
  • But `libpd` is not the ultimate endpoint of that call, that's all I'm trying to say. `libpd` is in fact simply a binding between C and Pure Data. So yes, a call like that does not need escaping, but that call passes that string to Pure Data, which is a whole other language with a whole other interpreter, which you would know if you bothered checking it out even briefly instead of arguing that I don't know what I'm talking about. So, you had a point, it only took you an attempted answer and a whole bunch of comments to make it. Come on, let's just agree you are grasping at straws. – Morpheu5 Jan 26 '17 at 15:44
  • I don't know why you're acting upset. I am trying to help. Basically, you're confirming that the receiving end is the problem and interprets the atom as-if it's PureData code (your repeated "sending it to Pure Data" or "passes it to Pure Data" fails to describe /how/ which is why I keep asking for the specific ways in which you pass that data). It's quite simple, if you want to _evaluate_ the atom as valid Pure Data code, then obviously it needs to be valid Pure Data code, and you need to escape it as such. That has nothing to do with the call you pointed me to. – sehe Jan 26 '17 at 16:30

1 Answers1

0

The cleverest way is not to do it.

For example, use execve instead of system (so you can pass arguments in an array, no need for shell escaping). See e.g. How can I escape variables sent to the 'system' command in C++?

Or, if you e.g. talk to a database server, do not concatenate your queries but bind parameters into a prepared statement. Again this precludes the need for any escaping.

Avoiding escaping avoids a whole slew of security issues (RCE, SQLi etc.)


If you must, probably just do

 "'" + replace_all(path.string(), "'", "''") + "'"

This would be fine for e.g. bash shells

For anything else, find out which characters need escaping and use the existing library functions that suit the goal, e.g.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • That would be OK, but I'm talking to Pure Data, sending the string as symbols. Pure Data takes spaces as separators, so my string gets broken up into multiple symbols. I'm not sure that escaping spaces would work either, but it's worth a shot. – Morpheu5 Jan 23 '17 at 10:33
  • @Morpheu5 I hear you, but will correct you: You were **not** talking about Pure Data. In fact, you never mentioned the word until your last comment. Context is important! If you update the question with a LINK to the relevant documentation (what is Pure Data?) I'll have a look whether I can answer your practical question – sehe Jan 23 '17 at 11:19
  • Heh, if I could find a solution by looking at the documentation, I would not be here asking a question. I edited the question, but as you'll see, PD is quite the obscure specialist tool, and I don't think providing it as a context would help at all. In fact, there aren't that many people who are intimately knowledgeable in both PD and text-based programming languages, so my chances of getting an answer are going to be so diminished that, in practice, me asking a question about a chosen solution is arguably the best course of action. Anyway, you are welcome to try :) – Morpheu5 Jan 23 '17 at 14:05