📄 types.cpp
字号:
strcpy(s, buf); usetypemap[t] = s; if (pointer || is_pointer) ptrtypemap[t] = s; if (vflag) fprintf(stderr, "Defined %s in namespace %s as %s\n", qname, URI?URI:(prefix?prefix:""), s); return t;}// get enumeration value. URI/type refers to the enum simpleType.const char *Types::ename(const char *type, const char *value){ const char *s = enames[Pair(type,value)]; if (!s) { s = fname(NULL, NULL, value, &rnames, NOLOOKUP); if (!eflag && type && *type) { // Add prefix to enum if (!*s || (s[0] == '_' && s[1] == '\0')) s = "_x0000"; char *buf = (char*)emalloc(strlen(type) + strlen(s) + 3); if (s[0] == '_' && s[1] != 'x') // _xXXXX is OK here sprintf(buf, "%s_%s", type, s); else sprintf(buf, "%s__%s", type, s); s = buf; } else rnames.insert(s); enames[Pair(type,value)] = s; } return s;}// get operation nameconst char *Types::oname(const char *prefix, const char *URI, const char *qname){ const char *s = fname(prefix, URI, qname, NULL, LOOKUP); if (s && usetypemap.find(s) != usetypemap.end()) { // Avoid name clash with structs/classes of the same name onames.insert(s); } s = fname(prefix, URI, qname, &onames, NOLOOKUP); onames.insert(s); return s;}// generate struct nameconst char *Types::sname(const char *URI, const char *name){ const char *s; char *t; if (!aflag && name) { size_t len = 0; for (VectorOfString::const_iterator i = scope.begin(); i != scope.end(); ++i) len += strlen(*i) + 1; t = (char*)emalloc(len + strlen(name) + 1); *t = '\0'; for (VectorOfString::const_iterator j = scope.begin(); j != scope.end(); ++j) { strcat(t, *j); strcat(t, "-"); } strcat(t, name); s = fname("_", URI, t, &rnames, NOLOOKUP); rnames.insert(s); } else if (URI) { s = nsprefix(NULL, URI); /* TODO: this may be affected? */ t = (char*)emalloc(strlen(s) + 16); sprintf(t, "_%s__struct_%d", s, snum++); s = t; } else { t = (char*)emalloc(16); sprintf(t, "struct_%d", snum++); s = t; } return s;}// generate union nameconst char *Types::uname(const char *URI){ const char *s; char *t; if (!aflag) { size_t len = 0; for (VectorOfString::const_iterator i = scope.begin(); i != scope.end(); ++i) len += strlen(*i) + 1; t = (char*)emalloc(len + 6); strcpy(t, "union"); for (VectorOfString::const_iterator j = scope.begin(); j != scope.end(); ++j) { strcat(t, "-"); strcat(t, *j); } s = fname("_", URI, t, &rnames, NOLOOKUP); rnames.insert(s); } else if (URI) { s = nsprefix(NULL, URI); /* TODO: this may be affected? */ t = (char*)emalloc(strlen(s) + 16); sprintf(t, "_%s__union_%d", s, unum++); s = t; } else { t = (char*)emalloc(16); sprintf(t, "_union_%d", unum++); s = t; } return s;}// generate enum nameconst char *Types::gname(const char *URI, const char *name){ const char *s; char *t; if (!aflag && name) { size_t len = 0; for (VectorOfString::const_iterator i = scope.begin(); i != scope.end(); ++i) len += strlen(*i) + 1; t = (char*)emalloc(len + strlen(name) + 1); *t = '\0'; for (VectorOfString::const_iterator j = scope.begin(); j != scope.end(); ++j) { strcat(t, *j); strcat(t, "-"); } strcat(t, name); s = fname("_", URI, t, &rnames, NOLOOKUP); rnames.insert(s); } else if (URI) { s = nsprefix(NULL, URI); /* TODO: this may be affected? */ t = (char*)emalloc(strlen(s) + 16); sprintf(t, "_%s__enum_%d", s, gnum++); s = t; } else { t = (char*)emalloc(16); sprintf(t, "enum_%d", gnum++); s = t; } return s;}// check if nillable or minOccurs=0 (and no default value is present)bool Types::is_nillable(const xs__element& element){ return !element.default_ && (element.nillable || (element.minOccurs && !strcmp(element.minOccurs, "0")));}bool Types::is_basetype(const char *URI, const char *type){ const char *t = tname(NULL, URI, type); if (!strcmp(t, "std::string") || strstr(t, "__")) return false; return !strncmp(type, "xs:", 3) || !strncmp(type, "SOAP-ENC:", 9);}void Types::dump(FILE *fd){ fprintf(fd, "\nTypes:\n"); for (MapOfStringToString::const_iterator i = usetypemap.begin(); i != usetypemap.end(); ++i) fprintf(fd, "%s=%s\n", (*i).first, (*i).second?(*i).second:"(null)"); fprintf(fd, "\nPointers:\n"); for (MapOfStringToString::const_iterator j = ptrtypemap.begin(); j != ptrtypemap.end(); ++j) fprintf(fd, "%s=%s\n", (*j).first, (*j).second?(*j).second:"(null)");}void Types::define(const char *URI, const char *name, const xs__complexType& complexType){ // generate prototype for structs/classes and store name const char *prefix = NULL; if (complexType.name) name = complexType.name; else prefix = "_"; if (complexType.complexContent && complexType.complexContent->restriction && !strcmp(complexType.complexContent->restriction->base, "SOAP-ENC:Array")) { if (strcmp(schema_prefix, "ns")) prefix = "*"; else prefix = ""; } if (cflag) { const char *t = deftname(STRUCT, "*", true, prefix, URI, name); if (t) { if (yflag) fprintf(stream, "\n/// Typedef synonym for struct %s.\ntypedef struct %s %s;\n", t, t, t); } else if (name) { t = deftypemap[cname(prefix, URI, name)]; if (t) { fprintf(stream, "\n/// Imported complexType \"%s\":%s from typemap %s.\n", URI, name, mapfile?mapfile:""); document(complexType.annotation); if (*t) format(t); else fprintf(stream, "// complexType definition intentionally left blank.\n"); } } } else { const char *t = deftname(CLASS, "*", true, prefix, URI, name); if (t) fprintf(stream, "\n// Forward declaration of class %s.\nclass %s;\n", t, t); else if (name) { t = deftypemap[cname(prefix, URI, name)]; if (t) { fprintf(stream, "\n/// Imported complexType \"%s\":%s from typemap %s.\n", URI, name, mapfile?mapfile:""); document(complexType.annotation); if (*t) format(t); else fprintf(stream, "// complexType definition intentionally left blank.\n"); } } }}void Types::gen(const char *URI, const char *name, const xs__simpleType& simpleType, bool anonymous){ const char *t = NULL; const char *prefix = NULL; if (simpleType.name) name = simpleType.name; else prefix = "_"; if (!anonymous) { t = deftypemap[cname(NULL, URI, name)]; if (t) { fprintf(stream, "\n/// Imported simpleType \"%s\":%s from typemap %s.\n", URI, name, mapfile?mapfile:""); document(simpleType.annotation); if (*t) format(t); else fprintf(stream, "// simpleType definition intentionally left blank.\n"); return; } } if (simpleType.restriction && simpleType.restriction->base) { const char *base = simpleType.restriction->base; const char *baseURI = NULL; if (simpleType.restriction->simpleTypePtr() && simpleType.restriction->simpleTypePtr()->schemaPtr()) baseURI = simpleType.restriction->simpleTypePtr()->schemaPtr()->targetNamespace; if (!anonymous) fprintf(stream, "\n/// \"%s\":%s is a simpleType restriction of %s.\n", URI?URI:"", name, simpleType.restriction->base); document(simpleType.annotation); document(simpleType.restriction->annotation); if (!simpleType.restriction->enumeration.empty()) { bool is_numeric = true; // check if all enumeration values are numeric bool is_qname = !strcmp(base, "xs:QName"); if (!anonymous) { t = deftname(ENUM, NULL, false, prefix, URI, name); if (t && !eflag) fprintf(stream, "/// Note: enum values are prefixed with '%s' to avoid name clashes, please use wsdl2h option -e to omit this prefix\n", t); } if (!t) t = gname(URI, name); if (!anonymous) fprintf(stream, "enum %s\n{\n", t); else fprintf(stream, " enum %s\n {\n", t); for (vector<xs__enumeration>::const_iterator enumeration1 = simpleType.restriction->enumeration.begin(); enumeration1 != simpleType.restriction->enumeration.end(); ++enumeration1) { const char *s; if ((s = (*enumeration1).value)) is_numeric &= is_integer(s); } SetOfString enumvals; for (vector<xs__enumeration>::const_iterator enumeration2 = simpleType.restriction->enumeration.begin(); enumeration2 != simpleType.restriction->enumeration.end(); ++enumeration2) { const char *s; document((*enumeration2).annotation); if ((s = (*enumeration2).value)) { if (!enumvals.count(s)) { enumvals.insert(s); if (is_numeric) fprintf(stream, "\t%s = %s,\t///< %s value=\"%s\"\n", ename(t, s), s, base, s); else if (is_qname && (*enumeration2).value_) fprintf(stream, "\t%s,\t///< %s value=\"%s\"\n", ename(t, (*enumeration2).value_), base, (*enumeration2).value_); else fprintf(stream, "\t%s,\t///< %s value=\"%s\"\n", ename(t, s), base, s); } } else fprintf(stream, "//\tunrecognized: enumeration '%s' has no value\n", name?name:""); } if (!anonymous) { fprintf(stream, "};\n"); if (yflag) fprintf(stream, "/// Typedef synonym for enum %s.\ntypedef enum %s %s;\n", t, t, t); if (pflag && simpleType.name) { const char *s = aname(prefix, URI, name); knames.insert(s); s = aname(prefix, URI, name); fprintf(stream, "\n/// Class wrapper\n"); fprintf(stream, "class %s : public xsd__anyType\n{ public:\n", s); fprintf(stream, elementformat, tname(prefix, URI, name), "__item;"); modify(s); fprintf(stream, "\n};\n"); } } else fprintf(stream, " }\n"); } else { if (simpleType.restriction->length && simpleType.restriction->length->value) fprintf(stream, "/// Length of this string is exactly %s characters\n", simpleType.restriction->length->value); else { const char *a = NULL, *b = NULL; if (simpleType.restriction->minLength) a = simpleType.restriction->minLength->value; if (simpleType.restriction->maxLength) b = simpleType.restriction->maxLength->value; if (a || b) fprintf(stream, "/// Length of this string is within %s..%s characters\n", a?a:"0", b?b:""); } if (simpleType.restriction->precision && simpleType.restriction->precision->value) fprintf(stream, "/// %sprecision is %s\n", simpleType.restriction->precision->fixed?"fixed ":"", simpleType.restriction->precision->value); if (simpleType.restriction->scale && simpleType.restriction->scale->value) fprintf(stream, "/// %sscale is %s\n", simpleType.restriction->scale->fixed?"fixed ":"", simpleType.restriction->scale->value); if (simpleType.restriction->totalDigits && simpleType.restriction->totalDigits->value) fprintf(stream, "/// %snumber of total digits is %s\n", simpleType.restriction->totalDigits->fixed?"fixed ":"", simpleType.restriction->totalDigits->value); if (simpleType.restriction->fractionDigits && simpleType.restriction->fractionDigits->value) fprintf(stream, "/// %snumber of fraction digits is %s\n", simpleType.restriction->fractionDigits->fixed?"fixed ":"", simpleType.restriction->fractionDigits->value); for (vector<xs__pattern>::const_iterator pattern1 = simpleType.restriction->pattern.begin(); pattern1 != simpleType.restriction->pattern.end(); ++pattern1) fprintf(stream, "/// Content pattern is \"%s\"\n", xstring((*pattern1).value)); const char *ai = NULL, *ae = NULL, *bi = NULL, *be = NULL; if (simpleType.restriction->minInclusive) ai = simpleType.restriction->minInclusive->value; else if (simpleType.restriction->minExclusive) ae = simpleType.restriction->minExclusive->value; if (simpleType.restriction->maxInclusive) bi = simpleType.restriction->maxInclusive->value; else if (simpleType.restriction->maxExclusive) be = simpleType.restriction->maxExclusive->value; if (ai || ae || bi || be) { fprintf(stream, "/// Value range is "); if (ai) fprintf(stream, "[%s..", ai); else if (ae) fprintf(stream, "(%s..", ae); else fprintf(stream, "[.."); if (bi) fprintf(stream, "%s]\n", bi); else if (be) fprintf(stream, "%s)\n", be); else fprintf(stream, "]\n"); } if (!simpleType.restriction->attribute.empty()) { fprintf(stderr, "\nWarning: simpleType '%s' should not have attributes\n", name?name:""); } const char *s = tname(NULL, baseURI, base); if (!anonymous) { bool is_ptr = false; is_ptr = (strchr(s, '*') != NULL) || (s == pname(true, NULL, baseURI, base)); t = deftname(TYPEDEF, NULL, is_ptr, prefix, URI, name); if (t) fprintf(stream, "typedef %s %s", s, t); } else { t = ""; fprintf(stream, elementformat, s, ""); fprintf(stream, "\n"); } if (t)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -