diff options
author | Rohan McGovern <rohan.mcgovern@nokia.com> | 2009-07-30 00:53:28 (GMT) |
---|---|---|
committer | Rohan McGovern <rohan.mcgovern@nokia.com> | 2009-07-30 04:54:44 (GMT) |
commit | 79ed97b8298e3c7e3c7d266c905024affeba7258 (patch) | |
tree | 9306b4a8bcaface2c243217aa3bc527aa553ef2f | |
parent | a92117be4323e26efe3f13b5c624e5010a7cd26a (diff) | |
download | Qt-79ed97b8298e3c7e3c7d266c905024affeba7258.zip Qt-79ed97b8298e3c7e3c7d266c905024affeba7258.tar.gz Qt-79ed97b8298e3c7e3c7d266c905024affeba7258.tar.bz2 |
Make QTestBasicStreamer use dynamically allocated strings.
This commit contains the bare minimum needed for the API to use dynamic
allocation; some parts of the code still use static buffers and
therefore have constraints on the size of output messages.
Task: 253861
Reviewed-by: Michael Goddard
-rw-r--r-- | src/testlib/qtestbasicstreamer.cpp | 24 | ||||
-rw-r--r-- | src/testlib/qtestbasicstreamer.h | 10 | ||||
-rw-r--r-- | src/testlib/qtestlightxmlstreamer.cpp | 42 | ||||
-rw-r--r-- | src/testlib/qtestlightxmlstreamer.h | 6 | ||||
-rw-r--r-- | src/testlib/qtestxmlstreamer.cpp | 28 | ||||
-rw-r--r-- | src/testlib/qtestxmlstreamer.h | 6 | ||||
-rw-r--r-- | src/testlib/qtestxunitstreamer.cpp | 34 | ||||
-rw-r--r-- | src/testlib/qtestxunitstreamer.h | 8 | ||||
-rw-r--r-- | src/testlib/qxmltestlogger.cpp | 91 | ||||
-rw-r--r-- | src/testlib/qxmltestlogger_p.h | 6 |
10 files changed, 153 insertions, 102 deletions
diff --git a/src/testlib/qtestbasicstreamer.cpp b/src/testlib/qtestbasicstreamer.cpp index 5fe9d4d..aac57ba 100644 --- a/src/testlib/qtestbasicstreamer.cpp +++ b/src/testlib/qtestbasicstreamer.cpp @@ -68,39 +68,39 @@ QTestBasicStreamer::QTestBasicStreamer() QTestBasicStreamer::~QTestBasicStreamer() {} -void QTestBasicStreamer::formatStart(const QTestElement *element, char *formatted) const +void QTestBasicStreamer::formatStart(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestBasicStreamer::formatEnd(const QTestElement *element, char *formatted) const +void QTestBasicStreamer::formatEnd(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestBasicStreamer::formatBeforeAttributes(const QTestElement *element, char *formatted) const +void QTestBasicStreamer::formatBeforeAttributes(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestBasicStreamer::formatAfterAttributes(const QTestElement *element, char *formatted) const +void QTestBasicStreamer::formatAfterAttributes(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestBasicStreamer::formatAttributes(const QTestElement *, const QTestElementAttribute *attribute, char *formatted) const +void QTestBasicStreamer::formatAttributes(const QTestElement *, const QTestElementAttribute *attribute, char **formatted) const { if(!attribute || !formatted ) return; - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } void QTestBasicStreamer::output(QTestElement *element) const @@ -113,7 +113,7 @@ void QTestBasicStreamer::output(QTestElement *element) const void QTestBasicStreamer::outputElements(QTestElement *element, bool) const { - char buf[1024]; + QTestCharBuffer buf; bool hasChildren; /* Elements are in reverse order of occurrence, so start from the end and work @@ -148,7 +148,7 @@ void QTestBasicStreamer::outputElements(QTestElement *element, bool) const void QTestBasicStreamer::outputElementAttributes(const QTestElement* element, QTestElementAttribute *attribute) const { - char buf[1024]; + QTestCharBuffer buf; while(attribute){ formatAttributes(element, attribute, buf); outputString(buf); diff --git a/src/testlib/qtestbasicstreamer.h b/src/testlib/qtestbasicstreamer.h index 84d1bce..432dd22 100644 --- a/src/testlib/qtestbasicstreamer.h +++ b/src/testlib/qtestbasicstreamer.h @@ -71,11 +71,11 @@ class QTestBasicStreamer const QTestLogger *logger() const; protected: - virtual void formatStart(const QTestElement *element = 0, char *formatted = 0) const; - virtual void formatEnd(const QTestElement *element = 0, char *formatted = 0) const; - virtual void formatBeforeAttributes(const QTestElement *element = 0, char *formatted = 0) const; - virtual void formatAfterAttributes(const QTestElement *element = 0, char *formatted = 0) const; - virtual void formatAttributes(const QTestElement *element = 0, const QTestElementAttribute *attribute = 0, char *formatted = 0) const; + virtual void formatStart(const QTestElement *element, char **formatted) const; + virtual void formatEnd(const QTestElement *element, char **formatted) const; + virtual void formatBeforeAttributes(const QTestElement *element, char **formatted) const; + virtual void formatAfterAttributes(const QTestElement *element, char **formatted) const; + virtual void formatAttributes(const QTestElement *element, const QTestElementAttribute *attribute, char **formatted) const; virtual void outputElements(QTestElement *element, bool isChildElement = false) const; virtual void outputElementAttributes(const QTestElement *element, QTestElementAttribute *attribute) const; diff --git a/src/testlib/qtestlightxmlstreamer.cpp b/src/testlib/qtestlightxmlstreamer.cpp index 170938a..a7e205a 100644 --- a/src/testlib/qtestlightxmlstreamer.cpp +++ b/src/testlib/qtestlightxmlstreamer.cpp @@ -59,7 +59,7 @@ QTestLightXmlStreamer::QTestLightXmlStreamer() QTestLightXmlStreamer::~QTestLightXmlStreamer() {} -void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *formatted) const +void QTestLightXmlStreamer::formatStart(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; @@ -70,7 +70,7 @@ void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *forma QXmlTestLogger::xmlQuote(quotedTf, element->attributeValue(QTest::AI_Name), sizeof(quotedTf)); - QTest::qt_snprintf(formatted, 1024, "<TestFunction name=\"%s\">\n", quotedTf); + QTest::qt_asprintf(formatted, "<TestFunction name=\"%s\">\n", quotedTf); break; } case QTest::LET_Failure: { @@ -78,7 +78,7 @@ void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *forma QXmlTestLogger::xmlCdata(cdataDesc, element->attributeValue(QTest::AI_Description), sizeof(cdataDesc)); - QTest::qt_snprintf(formatted, 1024, " <Description><![CDATA[%s]]></Description>\n", + QTest::qt_asprintf(formatted, " <Description><![CDATA[%s]]></Description>\n", cdataDesc); break; } @@ -91,7 +91,7 @@ void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *forma QXmlTestLogger::xmlCdata(cdataDesc, element->attributeValue(QTest::AI_Description), sizeof(cdataDesc)); - QTest::qt_snprintf(formatted, 1024, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n <Description><![CDATA[%s]]></Description>\n</Message>\n", + QTest::qt_asprintf(formatted, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n <Description><![CDATA[%s]]></Description>\n</Message>\n", element->attributeValue(QTest::AI_Type), element->attributeName(QTest::AI_File), quotedFile, @@ -109,7 +109,7 @@ void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *forma QXmlTestLogger::xmlQuote(quotedTag, element->attributeValue(QTest::AI_Tag), sizeof(quotedTag)); - QTest::qt_snprintf(formatted, 1024, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n", + QTest::qt_asprintf(formatted, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n", element->attributeName(QTest::AI_Metric), quotedMetric, element->attributeName(QTest::AI_Tag), @@ -121,61 +121,61 @@ void QTestLightXmlStreamer::formatStart(const QTestElement *element, char *forma break; } default: - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } } -void QTestLightXmlStreamer::formatEnd(const QTestElement *element, char *formatted) const +void QTestLightXmlStreamer::formatEnd(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; if (element->elementType() == QTest::LET_TestCase) { if( element->attribute(QTest::AI_Result) && element->childElements()) - QTest::qt_snprintf(formatted, 1024, "</Incident>\n</TestFunction>\n"); + QTest::qt_asprintf(formatted, "</Incident>\n</TestFunction>\n"); else - QTest::qt_snprintf(formatted, 1024, "</TestFunction>\n"); + QTest::qt_asprintf(formatted, "</TestFunction>\n"); } else - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestLightXmlStreamer::formatBeforeAttributes(const QTestElement *element, char *formatted) const +void QTestLightXmlStreamer::formatBeforeAttributes(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; if (element->elementType() == QTest::LET_TestCase && element->attribute(QTest::AI_Result)){ - char buf[900]; + QTestCharBuffer buf; char quotedFile[700]; QXmlTestLogger::xmlQuote(quotedFile, element->attributeValue(QTest::AI_File), sizeof(quotedFile)); - QTest::qt_snprintf(buf, sizeof(buf), "%s=\"%s\" %s=\"%s\"", + QTest::qt_asprintf(buf, "%s=\"%s\" %s=\"%s\"", element->attributeName(QTest::AI_File), quotedFile, element->attributeName(QTest::AI_Line), element->attributeValue(QTest::AI_Line)); if( !element->childElements() ) - QTest::qt_snprintf(formatted, 1024, "<Incident type=\"%s\" %s/>\n", - element->attributeValue(QTest::AI_Result), buf); + QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s/>\n", + element->attributeValue(QTest::AI_Result), buf.constData()); else - QTest::qt_snprintf(formatted, 1024, "<Incident type=\"%s\" %s>\n", - element->attributeValue(QTest::AI_Result), buf); + QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n", + element->attributeValue(QTest::AI_Result), buf.constData()); }else{ - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } } void QTestLightXmlStreamer::output(QTestElement *element) const { - char buf[1024]; - QTest::qt_snprintf(buf, sizeof(buf), "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n", + QTestCharBuffer buf; + QTest::qt_asprintf(buf, "<Environment>\n <QtVersion>%s</QtVersion>\n <QTestVersion>%s</QTestVersion>\n", qVersion(), QTEST_VERSION_STR ); outputString(buf); - QTest::qt_snprintf(buf, sizeof(buf), "</Environment>\n"); + QTest::qt_asprintf(buf, "</Environment>\n"); outputString(buf); QTestBasicStreamer::output(element); diff --git a/src/testlib/qtestlightxmlstreamer.h b/src/testlib/qtestlightxmlstreamer.h index 5a16327..6dafdcc 100644 --- a/src/testlib/qtestlightxmlstreamer.h +++ b/src/testlib/qtestlightxmlstreamer.h @@ -59,9 +59,9 @@ class QTestLightXmlStreamer: public QTestBasicStreamer QTestLightXmlStreamer(); ~QTestLightXmlStreamer(); - void formatStart(const QTestElement *element = 0, char *formatted = 0) const; - void formatEnd(const QTestElement *element = 0, char *formatted = 0) const; - void formatBeforeAttributes(const QTestElement *element = 0, char *formatted = 0) const; + void formatStart(const QTestElement *element, char **formatted) const; + void formatEnd(const QTestElement *element, char **formatted) const; + void formatBeforeAttributes(const QTestElement *element, char **formatted) const; void output(QTestElement *element) const; }; diff --git a/src/testlib/qtestxmlstreamer.cpp b/src/testlib/qtestxmlstreamer.cpp index 5172bcd..b9e0a38 100644 --- a/src/testlib/qtestxmlstreamer.cpp +++ b/src/testlib/qtestxmlstreamer.cpp @@ -60,7 +60,7 @@ QTestXmlStreamer::QTestXmlStreamer() QTestXmlStreamer::~QTestXmlStreamer() {} -void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) const +void QTestXmlStreamer::formatStart(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; @@ -71,7 +71,7 @@ void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) QXmlTestLogger::xmlQuote(quotedTf, element->attributeValue(QTest::AI_Name), sizeof(quotedTf)); - QTest::qt_snprintf(formatted, 1024, "<TestFunction name=\"%s\">\n", quotedTf); + QTest::qt_asprintf(formatted, "<TestFunction name=\"%s\">\n", quotedTf); break; } case QTest::LET_Failure: { @@ -94,14 +94,14 @@ void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) char cdataTag[100]; QXmlTestLogger::xmlCdata(cdataTag, element->attributeValue(QTest::AI_Tag), sizeof(cdataTag)); - QTest::qt_snprintf(formatted, 1024, "<Incident type=\"%s\" %s>\n" + QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n" " <DataTag><![CDATA[%s]]></DataTag>\n" " <Description><![CDATA[%s]]></Description>\n" "</Incident>\n", element->attributeValue(QTest::AI_Result), location, cdataTag, cdataDesc); } else { - QTest::qt_snprintf(formatted, 1024, "<Incident type=\"%s\" %s>\n" + QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s>\n" " <Description><![CDATA[%s]]></Description>\n" "</Incident>\n", element->attributeValue(QTest::AI_Result), location, cdataDesc); @@ -117,7 +117,7 @@ void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) QXmlTestLogger::xmlCdata(cdataDesc, element->attributeValue(QTest::AI_Description), sizeof(cdataDesc)); - QTest::qt_snprintf(formatted, 1024, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n <Description><![CDATA[%s]]></Description>\n</Message>\n", + QTest::qt_asprintf(formatted, "<Message type=\"%s\" %s=\"%s\" %s=\"%s\">\n <Description><![CDATA[%s]]></Description>\n</Message>\n", element->attributeValue(QTest::AI_Type), element->attributeName(QTest::AI_File), quotedFile, @@ -135,7 +135,7 @@ void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) QXmlTestLogger::xmlQuote(quotedTag, element->attributeValue(QTest::AI_Tag), sizeof(quotedTag)); - QTest::qt_snprintf(formatted, 1024, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n", + QTest::qt_asprintf(formatted, "<BenchmarkResult %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\" />\n", element->attributeName(QTest::AI_Metric), quotedMetric, element->attributeName(QTest::AI_Tag), @@ -147,23 +147,23 @@ void QTestXmlStreamer::formatStart(const QTestElement *element, char *formatted) break; } default: - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } } -void QTestXmlStreamer::formatEnd(const QTestElement *element, char *formatted) const +void QTestXmlStreamer::formatEnd(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; if (element->elementType() == QTest::LET_TestCase) { - QTest::qt_snprintf(formatted, 1024, "</TestFunction>\n"); + QTest::qt_asprintf(formatted, "</TestFunction>\n"); } else - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } -void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, char *formatted) const +void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, char **formatted) const { if(!element || !formatted) return; @@ -181,14 +181,14 @@ void QTestXmlStreamer::formatBeforeAttributes(const QTestElement *element, char element->attributeValue(QTest::AI_Line)); if( !element->childElements() ) { - QTest::qt_snprintf(formatted, 1024, "<Incident type=\"%s\" %s/>\n", + QTest::qt_asprintf(formatted, "<Incident type=\"%s\" %s/>\n", element->attributeValue(QTest::AI_Result), buf); } else { - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } }else{ - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } } diff --git a/src/testlib/qtestxmlstreamer.h b/src/testlib/qtestxmlstreamer.h index 3cb1c60..a601f60 100644 --- a/src/testlib/qtestxmlstreamer.h +++ b/src/testlib/qtestxmlstreamer.h @@ -59,9 +59,9 @@ class QTestXmlStreamer: public QTestBasicStreamer QTestXmlStreamer(); ~QTestXmlStreamer(); - void formatStart(const QTestElement *element = 0, char *formatted = 0) const; - void formatEnd(const QTestElement *element = 0, char *formatted = 0) const; - void formatBeforeAttributes(const QTestElement *element = 0, char *formatted = 0) const; + void formatStart(const QTestElement *element, char **formatted) const; + void formatEnd(const QTestElement *element, char **formatted) const; + void formatBeforeAttributes(const QTestElement *element, char **formatted) const; void output(QTestElement *element) const; }; diff --git a/src/testlib/qtestxunitstreamer.cpp b/src/testlib/qtestxunitstreamer.cpp index 6690cec..f59c0f5 100644 --- a/src/testlib/qtestxunitstreamer.cpp +++ b/src/testlib/qtestxunitstreamer.cpp @@ -73,7 +73,7 @@ void QTestXunitStreamer::indentForElement(const QTestElement* element, char* buf } } -void QTestXunitStreamer::formatStart(const QTestElement *element, char *formatted) const +void QTestXunitStreamer::formatStart(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; @@ -84,34 +84,34 @@ void QTestXunitStreamer::formatStart(const QTestElement *element, char *formatte // Errors are written as CDATA within system-err, comments elsewhere if (element->elementType() == QTest::LET_Error) { if (element->parentElement()->elementType() == QTest::LET_SystemError) { - QTest::qt_snprintf(formatted, 1024, "<![CDATA["); + QTest::qt_asprintf(formatted, "<![CDATA["); } else { - QTest::qt_snprintf(formatted, 1024, "%s<!--", indent); + QTest::qt_asprintf(formatted, "%s<!--", indent); } return; } - QTest::qt_snprintf(formatted, 1024, "%s<%s", indent, element->elementName()); + QTest::qt_asprintf(formatted, "%s<%s", indent, element->elementName()); } -void QTestXunitStreamer::formatEnd(const QTestElement *element, char *formatted) const +void QTestXunitStreamer::formatEnd(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; if(!element->childElements()){ - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); return; } char indent[20]; indentForElement(element, indent, sizeof(indent)); - QTest::qt_snprintf(formatted, 1024, "%s</%s>\n", indent, element->elementName()); + QTest::qt_asprintf(formatted, "%s</%s>\n", indent, element->elementName()); } -void QTestXunitStreamer::formatAttributes(const QTestElement* element, const QTestElementAttribute *attribute, char *formatted) const +void QTestXunitStreamer::formatAttributes(const QTestElement* element, const QTestElementAttribute *attribute, char **formatted) const { if(!attribute || !formatted ) return; @@ -124,7 +124,7 @@ void QTestXunitStreamer::formatAttributes(const QTestElement* element, const QTe if (attrindex != QTest::AI_Description) return; - QXmlTestLogger::xmlCdata(formatted, attribute->value(), 1024); + QXmlTestLogger::xmlCdata(formatted, attribute->value()); return; } @@ -137,14 +137,14 @@ void QTestXunitStreamer::formatAttributes(const QTestElement* element, const QTe if (key) { char quotedValue[900]; QXmlTestLogger::xmlQuote(quotedValue, attribute->value(), sizeof(quotedValue)); - QTest::qt_snprintf(formatted, 1024, " %s=\"%s\"", key, quotedValue); + QTest::qt_asprintf(formatted, " %s=\"%s\"", key, quotedValue); } else { - QTest::qt_snprintf(formatted, 10, ""); + QTest::qt_asprintf(formatted, ""); } } -void QTestXunitStreamer::formatAfterAttributes(const QTestElement *element, char *formatted) const +void QTestXunitStreamer::formatAfterAttributes(const QTestElement *element, char **formatted) const { if(!element || !formatted ) return; @@ -152,18 +152,18 @@ void QTestXunitStreamer::formatAfterAttributes(const QTestElement *element, char // Errors are written as CDATA within system-err, comments elsewhere if (element->elementType() == QTest::LET_Error) { if (element->parentElement()->elementType() == QTest::LET_SystemError) { - QTest::qt_snprintf(formatted, 1024, "]]>\n"); + QTest::qt_asprintf(formatted, "]]>\n"); } else { - QTest::qt_snprintf(formatted, 1024, " -->\n"); + QTest::qt_asprintf(formatted, " -->\n"); } return; } if(!element->childElements()) - QTest::qt_snprintf(formatted, 10, "/>\n"); + QTest::qt_asprintf(formatted, "/>\n"); else - QTest::qt_snprintf(formatted, 10, ">\n"); + QTest::qt_asprintf(formatted, ">\n"); } void QTestXunitStreamer::output(QTestElement *element) const @@ -176,7 +176,7 @@ void QTestXunitStreamer::output(QTestElement *element) const void QTestXunitStreamer::outputElements(QTestElement *element, bool) const { - char buf[1024]; + QTestCharBuffer buf; bool hasChildren; /* Elements are in reverse order of occurrence, so start from the end and work diff --git a/src/testlib/qtestxunitstreamer.h b/src/testlib/qtestxunitstreamer.h index 9817fd3..044307f 100644 --- a/src/testlib/qtestxunitstreamer.h +++ b/src/testlib/qtestxunitstreamer.h @@ -58,10 +58,10 @@ class QTestXunitStreamer: public QTestBasicStreamer QTestXunitStreamer(); ~QTestXunitStreamer(); - void formatStart(const QTestElement *element = 0, char *formatted = 0) const; - void formatEnd(const QTestElement *element = 0, char *formatted = 0) const; - void formatAfterAttributes(const QTestElement *element = 0, char *formatted = 0) const; - void formatAttributes(const QTestElement *element = 0, const QTestElementAttribute *attribute = 0, char *formatted = 0) const; + void formatStart(const QTestElement *element, char **formatted) const; + void formatEnd(const QTestElement *element, char **formatted) const; + void formatAfterAttributes(const QTestElement *element, char **formatted) const; + void formatAttributes(const QTestElement *element, const QTestElementAttribute *attribute, char **formatted) const; void output(QTestElement *element) const; void outputElements(QTestElement *element, bool isChildElement = false) const; diff --git a/src/testlib/qxmltestlogger.cpp b/src/testlib/qxmltestlogger.cpp index 85eb0dd..8fc8dd9 100644 --- a/src/testlib/qxmltestlogger.cpp +++ b/src/testlib/qxmltestlogger.cpp @@ -294,29 +294,30 @@ void QXmlTestLogger::addMessage(MessageTypes type, const char *message, XML characters as necessary so that dest is suitable for use in an XML quoted attribute string. */ -void QXmlTestLogger::xmlQuote(char* dest, char const* src, size_t n) +int QXmlTestLogger::xmlQuote(char* dest, char const* src, size_t n) { - if (n == 0) return; + if (n == 0) return 0; *dest = 0; - if (!src) return; + if (!src) return 0; + char* begin = dest; char* end = dest + n; while (dest < end) { switch (*src) { #define MAP_ENTITY(chr, ent) \ - case chr: \ - if (dest + sizeof(ent) < end) { \ - strcpy(dest, ent); \ - dest += sizeof(ent) - 1; \ - } \ - else { \ - *dest = 0; \ - return; \ - } \ - ++src; \ + case chr: \ + if (dest + sizeof(ent) < end) { \ + strcpy(dest, ent); \ + dest += sizeof(ent) - 1; \ + } \ + else { \ + *dest = 0; \ + return (dest+sizeof(ent)-begin); \ + } \ + ++src; \ break; MAP_ENTITY('>', ">"); @@ -333,7 +334,7 @@ void QXmlTestLogger::xmlQuote(char* dest, char const* src, size_t n) case 0: *dest = 0; - return; + return (dest-begin); default: *dest = *src; @@ -345,29 +346,31 @@ void QXmlTestLogger::xmlQuote(char* dest, char const* src, size_t n) // If we get here, dest was completely filled (dest == end) *(dest-1) = 0; + return (dest-begin); } /* Copy up to n characters from the src string into dest, escaping any special strings such that dest is suitable for use in an XML CDATA section. */ -void QXmlTestLogger::xmlCdata(char* dest, char const* src, size_t n) +int QXmlTestLogger::xmlCdata(char* dest, char const* src, size_t n) { - if (!n) return; + if (!n) return 0; if (!src || n == 1) { *dest = 0; - return; + return 0; } - char const CDATA_END[] = "]]>"; - char const CDATA_END_ESCAPED[] = "]]]><![CDATA[]>"; + static char const CDATA_END[] = "]]>"; + static char const CDATA_END_ESCAPED[] = "]]]><![CDATA[]>"; + char* begin = dest; char* end = dest + n; while (dest < end) { if (!*src) { *dest = 0; - return; + return (dest-begin); } if (!strncmp(src, CDATA_END, sizeof(CDATA_END)-1)) { @@ -378,7 +381,7 @@ void QXmlTestLogger::xmlCdata(char* dest, char const* src, size_t n) } else { *dest = 0; - return; + return (dest+sizeof(CDATA_END_ESCAPED)-begin); } continue; } @@ -390,6 +393,52 @@ void QXmlTestLogger::xmlCdata(char* dest, char const* src, size_t n) // If we get here, dest was completely filled (dest == end) *(dest-1) = 0; + return (dest-begin); +} + +typedef int (*StringFormatFunction)(char*,char const*,size_t); + +/* + A wrapper for string functions written to work with a fixed size buffer so they can be called + with a dynamically allocated buffer. +*/ +int allocateStringFn(char** str, char const* src, StringFormatFunction func) +{ + static const int MAXSIZE = 1024*1024*2; + + int size = 32; + delete[] *str; + *str = new char[size]; + + int res = 0; + + for (;;) { + res = func(*str, src, size); + (*str)[size - 1] = '\0'; + if (res < size) { + // We succeeded or fatally failed + break; + } + // buffer wasn't big enough, try again + size *= 2; + if (size > MAXSIZE) { + break; + } + delete[] *str; + *str = new char[size]; + } + + return res; +} + +int QXmlTestLogger::xmlQuote(char** str, char const* src) +{ + return allocateStringFn(str, src, QXmlTestLogger::xmlQuote); +} + +int QXmlTestLogger::xmlCdata(char** str, char const* src) +{ + return allocateStringFn(str, src, QXmlTestLogger::xmlCdata); } QT_END_NAMESPACE diff --git a/src/testlib/qxmltestlogger_p.h b/src/testlib/qxmltestlogger_p.h index 526b471..a7cc00a 100644 --- a/src/testlib/qxmltestlogger_p.h +++ b/src/testlib/qxmltestlogger_p.h @@ -79,8 +79,10 @@ public: void addMessage(MessageTypes type, const char *message, const char *file = 0, int line = 0); - static void xmlCdata(char* dest, char const* src, size_t n); - static void xmlQuote(char* dest, char const* src, size_t n); + static int xmlCdata(char** dest, char const* src); + static int xmlQuote(char** dest, char const* src); + static int xmlCdata(char* dest, char const* src, size_t n); + static int xmlQuote(char* dest, char const* src, size_t n); private: XmlMode xmlmode; |