static JSONCPP_STRING removeSuffix(const JSONCPP_STRING& path, const JSONCPP_STRING& extension) { if (extension.length() >= path.length()) return JSONCPP_STRING(""); JSONCPP_STRING suffix = path.substr(path.length() - extension.length()); if (suffix != extension) return JSONCPP_STRING(""); return path.substr(0, path.length() - extension.length()); }
void Path::makePath(const JSONCPP_STRING& path, const InArgs& in) { const char* current = path.c_str(); const char* end = current + path.length(); InArgs::const_iterator itInArg = in.begin(); while (current != end) { if (*current == '[') { ++current; if (*current == '%') addPathInArg(path, in, itInArg, PathArgument::kindIndex); else { ArrayIndex index = 0; for (; current != end && *current >= '0' && *current <= '9'; ++current) index = index * 10 + ArrayIndex(*current - '0'); args_.push_back(index); } if (current == end || *++current != ']') invalidPath(path, int(current - path.c_str())); } else if (*current == '%') { addPathInArg(path, in, itInArg, PathArgument::kindKey); ++current; } else if (*current == '.' || *current == ']') { ++current; } else { const char* beginName = current; while (current != end && !strchr("[.", *current)) ++current; args_.push_back(JSONCPP_STRING(beginName, current)); } } }
JSONCPP_STRING Value::asString() const { switch (type_) { case nullValue: return ""; case stringValue: { if (value_.string_ == 0) return ""; unsigned this_len; char const* this_str; decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); return JSONCPP_STRING(this_str, this_len); } case booleanValue: return value_.bool_ ? "true" : "false"; case intValue: return valueToString(value_.int_); case uintValue: return valueToString(value_.uint_); case realValue: return valueToString(value_.real_); default: JSON_FAIL_MESSAGE("Type is not convertible to string"); } }
static int parseCommandLine( int argc, const char* argv[], Options* opts) { opts->parseOnly = false; opts->write = &useStyledWriter; if (argc < 2) { return printUsage(argv); } int index = 1; if (JSONCPP_STRING(argv[index]) == "--json-checker") { opts->features = Json::Features::strictMode(); opts->parseOnly = true; ++index; } if (JSONCPP_STRING(argv[index]) == "--json-config") { printConfig(); return 3; } if (JSONCPP_STRING(argv[index]) == "--json-writer") { ++index; JSONCPP_STRING const writerName(argv[index++]); if (writerName == "StyledWriter") { opts->write = &useStyledWriter; } else if (writerName == "StyledStreamWriter") { opts->write = &useStyledStreamWriter; } else if (writerName == "BuiltStyledStreamWriter") { opts->write = &useBuiltStyledStreamWriter; } else { printf("Unknown '--json-writer %s'\n", writerName.c_str()); return 4; } } if (index == argc || index + 1 < argc) { return printUsage(argv); } opts->path = argv[index]; return 0; }
Value::Members Value::getMemberNames() const { JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::getMemberNames(), value must be objectValue"); if (type_ == nullValue) return Value::Members(); Members members; members.reserve(value_.map_->size()); ObjectValues::const_iterator it = value_.map_->begin(); ObjectValues::const_iterator itEnd = value_.map_->end(); for (; it != itEnd; ++it) { members.push_back(JSONCPP_STRING((*it).first.data(), (*it).first.length())); } return members; }
static JSONCPP_STRING readInputTestFile(const char* path) { FILE* file = fopen(path, "rb"); if (!file) return JSONCPP_STRING(""); fseek(file, 0, SEEK_END); long const size = ftell(file); unsigned long const usize = static_cast<unsigned long>(size); fseek(file, 0, SEEK_SET); JSONCPP_STRING text; char* buffer = new char[size + 1]; buffer[size] = 0; if (fread(buffer, 1, usize, file) == usize) text = buffer; fclose(file); delete[] buffer; return text; }
void StyledWriter::indent() { indentString_ += JSONCPP_STRING(indentSize_, ' '); }
static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) { if (value == NULL) return ""; // Not sure how to handle unicode... if (strnpbrk(value, "\"\\\b\f\n\r\t", length) == NULL && !containsControlCharacter0(value, length)) return JSONCPP_STRING("\"") + value + "\""; // We have to walk value and escape any special characters. // Appending to JSONCPP_STRING is not efficient, but this should be rare. // (Note: forward slashes are *not* rare, but I am not escaping them.) JSONCPP_STRING::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL JSONCPP_STRING result; result.reserve(maxsize); // to avoid lots of mallocs result += "\""; char const* end = value + length; for (const char* c = value; c != end; ++c) { switch (*c) { case '\"': result += "\\\""; break; case '\\': result += "\\\\"; break; case '\b': result += "\\b"; break; case '\f': result += "\\f"; break; case '\n': result += "\\n"; break; case '\r': result += "\\r"; break; case '\t': result += "\\t"; break; // case '/': // Even though \/ is considered a legal escape in JSON, a bare // slash is also legal, so I see no reason to escape it. // (I hope I am not misunderstanding something.) // blep notes: actually escaping \/ may be useful in javascript to avoid </ // sequence. // Should add a flag to allow this compatibility mode and prevent this // sequence from occurring. default: if ((isControlCharacter(*c)) || (*c == 0)) { JSONCPP_OSTRINGSTREAM oss; oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*c); result += oss.str(); } else { result += *c; } break; } } result += "\""; return result; }