You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7 KiB

#ifndef CJS_PROC_STRING
#define CJS_PROC_STRING
#include <vector>
#include <sstream>
#include <ostream>
struct cjs_str {
int length;
char* string;
std::string to_str() {
std::string str(this->string);
return str;
}
void from_str(std::string str) {
this->length = str.length();
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
this->string = cstr;
}
std::string c_str() {
return this->to_str().c_str();
}
std::vector<std::string> split() {
std::string s = this->to_str();
std::vector<std::string> result;
std::istringstream iss(s);
for(std::string s; iss >> s; )
result.push_back(s);
return result;
}
};
namespace cjs {
namespace str {
int pass_size() {
return 3 * sizeof(int);
}
char* cpp_to_c(std::string str) {
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
return cstr;
}
std::string join(const std::vector<std::string> strings, const char* by) {
if ( strings.size() < 1 ) return "";
else if ( strings.size() == 1 ) return strings.front();
else {
std::stringstream res;
copy(strings.begin(), strings.end() - 1, std::ostream_iterator<std::string>(res, by));
res << strings.back();
return res.str();
}
}
cjs_str* cpp_to_cjs(std::string str) {
return new cjs_str{ (int) str.length(), cpp_to_c(str) };
}
cjs_str* c_to_cjs(char* string, int len) {
return new cjs_str{ len, string };
}
std::string c_to_cpp(char* string) {
std::string str(string);
return str;
}
std::string cjs_to_cpp(cjs_str* cjs) {
return c_to_cpp(cjs->string);
}
cjs_str* receive(int offset) {
// Get the string pass information
// Format: [ string offset, string length, (unused) ]
int* str_pass = reinterpret_cast<int*>(offset);
int length = str_pass[1];
char* string = reinterpret_cast<char*>(str_pass[0]);
free(str_pass);
return new cjs_str{ length, string };
}
int set_pass(int address, int size, int other = 0) {
int* pass = new int[3];
pass[0] = address;
pass[1] = size;
pass[2] = other;
return (int) pass;
}
int pass(char* string, int length) {
return set_pass((int) string, length);
}
}
}
#endif //CJS_PROC_STRING