samedi 25 juin 2016

Split string path with space


I am writing a program that should receive 3 parameters by User: file_upload "local_path" "remote_path"

code example:

std::vector split(std::string str, char delimiter) {
   std::vector<string> v;
   std::stringstream src(str);
   std::string buf;

   while(getline(src, buf, delimiter)) {
       v.push_back(buf);
   }
   return v;
}

void function() {
   std::string input
   getline(std::cin, input);
   // user input like this: file_upload /home/Space Dir/file c:dirfile
   std::vector<std::string> v_input = split(input, ' ');

   // the code will do something like this
   if(v_input[0].compare("file_upload") == 0) {        
     FILE *file;
     file = fopen(v_input[1].c_str(), "rb");
     send_upload_dir(v_input[2].c_str());
     // bla bla bla
   }
}

My question is: the second and third parameter are directories, then they can contain spaces in name. How can i make the split function does not change the spaces of the second and third parameter?

I thought to put quotes in directories and make a function to recognize, but not work 100% because the program has other functions that take only 2 parameters not three. can anyone help?

EDIT: /home/user/Space Dir/file.out <-- path with space name.

If this happens the vector size is greater than expected, and the path to the directory will be broken.. this can not happen..

the vector will contain something like this:

vector[1] = /home/user/Space

vector[2] = Dir/file.out

and what I want is this:

vector[1] = /home/user/Space Dir/file.out


Aucun commentaire:

Enregistrer un commentaire