//convert MyString to int noexcept int myStringToInt(const MyString& str) { if (str.size() == 0) return 0; int res = 0; if (str[0] == '-') { int sign(-1); int mnog(1); for (int i = str.size() - 1; i != 0; i--) { res =res + mnog*((int)str[i] - '0'); mnog *= 10; } res *= sign; } else { int mnog(1); for (int i = str.size() - 1; i != -1; i--) { res = res + mnog*(str[i] - '0'); mnog *= 10; } } return res; }
Vector<MyString> split(const MyString& path) { Vector<MyString> ret; int spos = 0; while (spos < path.size()) { int epos = path.find(_T('\\'), spos); if (epos != -1) { int i = epos + 1; while (i < path.size() && path[i] == _T('\\')) ++i; MyString name(path.substr(spos, epos - spos)); ret.append(name); spos = i; } else if (spos < path.size()) { MyString name(path.substr(spos)); ret.append(name); spos = path.size(); } } return ret; }
static int getPath(const MyString& cmd, int pos, MyString* p) { int i = pos; while (i < cmd.size() && _istspace(cmd[i])) ++i; int j = i; // 处理以双引号包括的路径 if (cmd[j] == _T('"')) { ++j; while (j < cmd.size() && cmd[j] != _T('"')) ++j; if (j == cmd.size()) { return -1; } else { *p = cmd.substr(i + 1, j - i - 1); return j + 1; } } else { while (j < cmd.size() && !_istspace(cmd[j]) && cmd[j] != _T('/')) ++j; *p = cmd.substr(i, j - i); return j; } }
bool operator==(const MyString& lhs, const MyString& rhs) { if (lhs.size() != rhs.size()) return false; for (size_t i = 0; i < lhs.size(); i++) { if (lhs[i] != rhs[i]) return false; } return true; }
bool isLegalName(MyString name) { for (int i = 0; i < name.size(); ++i) { if (!isLegalChar(name[i])) return false; } if (name.startWith(_T(" ")) || name.size() > g_MAX_NAME_SIZE) return false; return true; }
static int getSwitch(const MyString& cmd, int pos, MyString* s) { int i = pos; while (i < cmd.size() && !_istgraph(cmd[i])) ++i; // 仅有首个字符为"\" int j = i + 1; while (j < cmd.size() && _istalpha(cmd[j])) ++j; *s = cmd.substr(i, j - i); return j; }
// 取得命令名 static int getCmd(const MyString& cmd, int pos, MyString* name) { int i = pos; while (i < cmd.size() && !_istgraph(cmd[i])) ++i; int j = i; while (j < cmd.size() && _istalpha(cmd[j])) ++j; // 仅在识别出类似绝对路径时进行回溯 if ((i + 1) < cmd.size() && _istalpha(cmd[i]) && cmd[i + 1] == _T(':')) return i; *name = cmd.substr(i, j - i); return j; }
MyString dirname(MyString path) { assert(isPath(path)); int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } return path.substr(0, pos); }
MyString join(MyString path, MyString name) { if (path[path.size() - 1] != _T('\\') && !name.isEmpty() && !name.startWith(_T("\\"))) { path += _T("\\"); } path += name; return path; }
MyString dirname(MyString path) { if (!isPath(path)) { return _T(""); } int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } return path.substr(0, pos); }
MyString basename(MyString path) { assert(isPath(path)); int pos = path.size() - 1; for (; pos >= 0 && path[pos] != _T('\\'); --pos) { } if (pos < 0) { return _T(""); } return path.substr(pos + 1); }
// 不改变路径中的大小写情况 MyString VirtualDiskNode::pathNormalize(MyString path) const { MyString ret; if (!isPath(path)) { assert(0); return ret; } if (isVolumnRelative(path)) { ret = join(m_pwd.substr(0, 2), path); } else if (isRelative(path)) { if (path == _T(".")) { ret = m_pwd; } else if (path == _T("..")) { ret = dirname(m_pwd); } else if (path.startWith(_T("..\\"))) { ret = dirname(m_pwd) + path.substr(2); } else if (path.startWith(_T(".\\"))) { ret = m_pwd + path.substr(1); } // 直接输入名字的情况 else if (!match(path, _T("?:*"))) { ret = m_pwd + _T("\\") + path; } } // 绝对路径中处理:\dir\file的情况 //else if (path.startWith(_T("\\"))) //{ // ret = _T("c:") + path; //} if (path.endWith(_T("\\"))) { ret = ret.substr(0, ret.size() - 1); } return ret; }
// 在src中确定从位置pos开始,长度为len的范围内的首个与c匹配字符的位置 static int skipToEqual(const MyString& src, int pos, TCHAR c, int len = 0) { assert(len >= 0); if (len == 0) { return src.find(c, pos); } for (int i = 0; i < len && (pos + i) < src.size(); ++i) { if (src[pos + i] == c) { return pos + i; } } return -1; }
int main() { const MyString cs("Ein konstanter String"); MyString s(cs); s.assign(cs); s.append(cs); cout << cs.c_str() << endl; cout << cs.size() << endl; cout << cs.capacity() << endl; cout << boolalpha << cs.empty() << endl; s = cs + cs; cout << (cs == cs) << endl; s = cs; cout << cs << endl; s.at(1) = 'X'; s[2] = 'Y'; // Hallo cout << s << endl; cin.get(); return 0; }
int main() { cout << "Testing default constructor\n\n"; const MyString s1; cout << "s1: " << s1 << endl; // Makes MyString cout << "s1 size: " << s1.size() << endl; // Gets stringSize cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n"); // Displays weather string is empty or not cout << endl; // Endl and goes onto next code chunk cout << "Testing second constructor\n\n"; MyString s2 = "some text"; cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n"); cout <<endl; cout << "Testing size limit on second constructor\n\n"; MyString s3 = "This is a really long string and not all of it will actually end up in the array, but that is okay"; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n"); cout << endl; cout << "Testing write form of subscript operator\n\n"; s2[0] = 'S'; s2[5] = 'T'; cout << "s2: " << s2 << endl << endl; cout << "Testing read form of subscript operator\n\n"; cout << "s2: "; for (unsigned int i = 0; i < s2.size(); i++) cout << s2[i]; cout << endl << endl; cout << "Testing equality operators\n\n"; const MyString s4 = "Some Text"; cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n"); cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n\n"); cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n"); cout << "s4 and \"More Text\" are " << ((s4 == "More Text") ? "equal\n" : "not equal\n\n"); cout << "\"Some Text\" and s4 are " << (("Some Text" == s4) ? "equal\n" : "not equal\n"); cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n\n"); cout << "Testing clear() method\n\n"; s3.clear(); cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n"); return 0; }
int parse(const MyString& cmd, MyString* name, Vector<MyString>* pathes, Vector<MyString>* switches) { if (name == nullptr || pathes == nullptr || switches == nullptr) { return -1; } pathes->clear(); switches->clear(); int i = getCmd(cmd, 0, name); if (i == cmd.size()) { return 0; } if (*name == _T("mkdir")) { while (_istspace(cmd[i])) ++i; int j = cmd.size() - 1; while (_istspace(cmd[j])) --j; MyString path = cmd.substr(i, j - i + 1); pathes->append(path); return 0; } for (; i < cmd.size();) { if (_istspace(cmd[i])) { ++i; } else if (cmd[i] != _T('/')) { MyString path; i = getPath(cmd, i, &path); if (!isPath(path) && !hasWildcard(path)) { printf("文件名、目录名或卷标语法不正确\n"); *name = _T(""); pathes->clear(); switches->clear(); return -1; } pathes->append(path); } else if (cmd[i] == _T('/')) { MyString s; i = getSwitch(cmd, i, &s); switches->append(s); } else { assert(0); return -1; } } return 0; }
bool match(const MyString& src, const MyString& pattern) { int i = 0; int j = 0; for (; i < src.size() && j < pattern.size(); ++i, ++j) { if (pattern[j] == '*') { if ((j + 1) == pattern.size()) { return true; } else { ++j; int next_i = skipToEqual(src, i + 1, pattern[j]); if (next_i == -1) { return false; } i = next_i; } } else if (pattern[j] == '?') { if ((j + 1) == pattern.size()) { if ((i + 1) == src.size()) { return true; } else { return false; } } else { ++j; int next_i = skipToEqual(src, i, pattern[j], 2); if (next_i == -1) { return false; } i = next_i; } } else if (pattern[j] != src[i]) { return false; } } if (i == src.size() && j == pattern.size()) { return true; } // 说明src的长度短于pattern else if ((j + 1) == pattern.size()) { if (pattern[j] == '*' || pattern[j] == '?') { return true; } } return false; }
//拷贝构造 MyString::MyString(const MyString& str) { init(str.size(), str.data); }
// // FIXME: We have a lot of boilerplate code in this function and file. // int DockerAPI::version( std::string & version, CondorError & /* err */ ) { ArgList versionArgs; if ( ! add_docker_arg(versionArgs)) return -1; versionArgs.AppendArg( "-v" ); MyString displayString; versionArgs.GetArgsStringForLogging( & displayString ); dprintf( D_FULLDEBUG, "Attempting to run: '%s'.\n", displayString.c_str() ); #if 1 MyPopenTimer pgm; if (pgm.start_program(versionArgs, true, NULL, false) < 0) { // treat 'file not found' as not really error int d_level = (pgm.error_code() == ENOENT) ? D_FULLDEBUG : (D_ALWAYS | D_FAILURE); dprintf(d_level, "Failed to run '%s' errno=%d %s.\n", displayString.c_str(), pgm.error_code(), pgm.error_str() ); return -2; } int exitCode; if ( ! pgm.wait_for_exit(default_timeout, &exitCode)) { pgm.close_program(1); dprintf( D_ALWAYS | D_FAILURE, "Failed to read results from '%s': '%s' (%d)\n", displayString.c_str(), pgm.error_str(), pgm.error_code() ); return -3; } if (pgm.output_size() <= 0) { dprintf( D_ALWAYS | D_FAILURE, "'%s' returned nothing.\n", displayString.c_str() ); return -3; } MyStringSource * src = &pgm.output(); MyString line; if (line.readLine(*src, false)) { line.chomp(); bool jansens = strstr( line.c_str(), "Jansens" ) != NULL; bool bad_size = ! src->isEof() || line.size() > 1024 || line.size() < (int)sizeof("Docker version "); if (bad_size && ! jansens) { // check second line of output for the word Jansens also. MyString tmp; tmp.readLine(*src, false); jansens = strstr( tmp.c_str(), "Jansens" ) != NULL; } if (jansens) { dprintf( D_ALWAYS | D_FAILURE, "The DOCKER configuration setting appears to point to OpenBox's docker. If you want to use Docker.IO, please set DOCKER appropriately in your configuration.\n" ); return -5; } else if (bad_size) { dprintf( D_ALWAYS | D_FAILURE, "Read more than one line (or a very long line) from '%s', which we think means it's not Docker. The (first line of the) trailing text was '%s'.\n", displayString.c_str(), line.c_str() ); return -5; } } if( exitCode != 0 ) { dprintf( D_ALWAYS, "'%s' did not exit successfully (code %d); the first line of output was '%s'.\n", displayString.c_str(), exitCode, line.c_str() ); return -4; } version = line.c_str(); #else FILE * dockerResults = my_popen( versionArgs, "r", 1 , 0, false); if( dockerResults == NULL ) { dprintf( D_ALWAYS | D_FAILURE, "Failed to run '%s'.\n", displayString.c_str() ); return -2; } char buffer[1024]; if( NULL == fgets( buffer, 1024, dockerResults ) ) { if( errno ) { dprintf( D_ALWAYS | D_FAILURE, "Failed to read results from '%s': '%s' (%d)\n", displayString.c_str(), strerror( errno ), errno ); } else { dprintf( D_ALWAYS | D_FAILURE, "'%s' returned nothing.\n", displayString.c_str() ); } my_pclose( dockerResults ); return -3; } if( NULL != fgets( buffer, 1024, dockerResults ) ) { if( strstr( buffer, "Jansens" ) != NULL ) { dprintf( D_ALWAYS | D_FAILURE, "The DOCKER configuration setting appears to point to OpenBox's docker. If you want to use Docker.IO, please set DOCKER appropriately in your configuration.\n" ); } else { dprintf( D_ALWAYS | D_FAILURE, "Read more than one line (or a very long line) from '%s', which we think means it's not Docker. The (first line of the) trailing text was '%s'.\n", displayString.c_str(), buffer ); } my_pclose( dockerResults ); return -5; } int exitCode = my_pclose( dockerResults ); if( exitCode != 0 ) { dprintf( D_ALWAYS, "'%s' did not exit successfully (code %d); the first line of output was '%s'.\n", displayString.c_str(), exitCode, buffer ); return -4; } size_t end = strlen(buffer); if (end > 0 && buffer[end-1] == '\n') { buffer[end-1] = '\0'; } version = buffer; #endif sscanf(version.c_str(), "Docker version %d.%d", &DockerAPI::majorVersion, &DockerAPI::minorVersion); return 0; }
int main() { cout << "Testing default constructor\n\n"; const MyString s1; cout << "s1: " << s1 << endl; cout << "s1 size: " << s1.size() << endl; cout << "s1 capacity: " << s1.capacity() << endl; cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n"); cout << endl; cout << "Testing second constructor\n\n"; MyString s2 = "some text"; cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 capacity: " << s2.capacity() << endl; cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n"); cout << endl; cout << "Testing second constructor with long string\n\n"; MyString s3 = "This is a really long string and this time all of it will actually end up in the array - pretty neat, huh?"; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing write form of subscript operator\n\n"; s2[0] = 'S'; s2[5] = 'T'; cout << "s2: " << s2 << endl << endl; cout << "Testing read form of subscript operator\n\n"; cout << "s2: "; for (unsigned int i = 0; i < s2.size(); i++) cout << s2[i]; cout << endl << endl; cout << "Testing equality operators\n\n"; const MyString s4 = "Some Text"; cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n"); cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n\n"); cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n"); cout << "s4 and \"More Text\" are " << ((s4 == "More Text") ? "equal\n" : "not equal\n\n"); cout << "\"Some Text\" and s4 are " << (("Some Text" == s4) ? "equal\n" : "not equal\n"); cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n\n"); cout << "Testing clear() method\n\n"; s3.clear(); cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n"); cout << "Testing copy constructor\n\n"; MyString s5(s4); // Problem cout << "s5: " << s5 << endl; cout << "s5 size: " << s5.size() << endl; cout << "s5 capacity: " << s5.capacity() << endl; cout << endl; cout << "Testing assignment operator\n\n"; s3 = s5; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; s3 = "a different string"; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing self-assignment\n\n"; s3 = s3; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing chained assignment\n\n"; s3 = s2 = "Hello, world"; cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 capacity: " << s2.capacity() << endl; cout << endl; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; return 0; }