Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #159 from youpong/rewrite-split
Browse files Browse the repository at this point in the history
Rewrote split()
  • Loading branch information
TanmayPatil105 authored Jul 19, 2024
2 parents 2c33758 + 094eeaf commit b2b660d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
Expand Down Expand Up @@ -194,36 +195,20 @@ class Command

static char **split(string cmd)
{
vector<string> v;
vector<string> v = {};

string token;
for (char c : cmd)
istringstream ss{cmd};
for (string arg{}; getline(ss, arg, ' '); )
{
if (c == ' ')
{
v.push_back(token);
token = "";
continue;
}
token += c;
if (arg != "")
v.push_back(arg);
}
v.push_back(token);

char **argv = (char **)calloc(
v.size() + 1,
sizeof(char *)); // +1 for the terminating NULL pointer
if (argv == NULL)
{
throw runtime_error("calloc failed");
}
char **argv = new char*[v.size() + 1]; // +1 for the terminating NULL pointer
char **p = argv;
for (string s : v)
for (auto &s : v)
{
if ((*p = strdup(s.c_str())) == NULL)
{
throw runtime_error("strdup failed");
}
p++;
*p++ = std::strcpy(new char[s.length() + 1], s.c_str());
}
*p = (char *)0; // terminated by a NULL pointer

Expand Down
3 changes: 3 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ static void test_Command()
expect(1, c->getOutputLines(), "getOutputLines()"s);
expect("akamoku\n"s, c->getErrorOutput(), "getErrorOutput()"s);

c = Command::exec("ls ../README.md"s);
expect("../README.md\n"s, c->getOutput(), "argc = 2"s);

c = Command::exec("false"s);
expect(1, c->getExitCode(), "Exit code"s);

Expand Down

0 comments on commit b2b660d

Please sign in to comment.