Skip to content

Commit

Permalink
Improve tokenizer method to handle spaces in filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Anand committed Sep 1, 2018
1 parent 0b90cc4 commit 88c4813
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 26 additions & 2 deletions util.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <dirent.h>
#include "util.h"

/**
Expand Down Expand Up @@ -47,4 +46,29 @@ std::string get_proper_path(const std::string &path, const std::string &HOME_PAT

// currrent directory relative path
return path;
}
}

std::vector<std::string> escaped_tokenizer(std::string command) {
std::vector<std::string> tokens;
std::string token;
bool escaped = false;
char escape_char = '\'';

for (char c: command) {
// build the token until we encounter a space
if (c == ' ' and !escaped) {
tokens.push_back(token);
token.clear();
} else if ((c == '\'' || c == '\"') and !escaped) {
escape_char = c;
escaped = !escaped;
} else if (c == escape_char) {
escaped = !escaped;
} else {
token += c;
}
}
tokens.push_back(token); // push last token

return tokens;
}
5 changes: 5 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <string>
#include <unistd.h>
#include <dirent.h>
#include <vector>


#define RES_QUIT 0
#define RES_NORMAL_MODE 1
Expand All @@ -25,4 +28,6 @@ bool dir_exists(const std::string &path);

std::string get_proper_path(const std::string &path, const std::string &HOME_PATH);

std::vector<std::string> escaped_tokenizer(std::string command);

#endif //UTIL_H

0 comments on commit 88c4813

Please sign in to comment.