1 struct Argv { 2 public: ArgvArgv3 Argv() : Argv({"node", "-p", "process.version"}) {} 4 ArgvArgv5 Argv(const std::initializer_list<const char*> &args) { 6 nr_args_ = args.size(); 7 int total_len = 0; 8 for (auto it = args.begin(); it != args.end(); ++it) { 9 total_len += strlen(*it) + 1; 10 } 11 argv_ = static_cast<char**>(malloc(nr_args_ * sizeof(char*))); 12 argv_[0] = static_cast<char*>(malloc(total_len)); 13 int i = 0; 14 int offset = 0; 15 for (auto it = args.begin(); it != args.end(); ++it, ++i) { 16 int len = strlen(*it) + 1; 17 snprintf(argv_[0] + offset, len, "%s", *it); 18 // Skip argv_[0] as it points the correct location already 19 if (i > 0) { 20 argv_[i] = argv_[0] + offset; 21 } 22 offset += len; 23 } 24 } 25 ~ArgvArgv26 ~Argv() { 27 free(argv_[0]); 28 free(argv_); 29 } 30 nr_argsArgv31 int nr_args() const { 32 return nr_args_; 33 } 34 35 char** operator*() const { 36 return argv_; 37 } 38 39 private: 40 char** argv_; 41 int nr_args_; 42 }; 43