| 2158 | */ |
| 2159 | |
| 2160 | int CPLSerializeXMLTreeToFile(const CPLXMLNode *psTree, const char *pszFilename) |
| 2161 | |
| 2162 | { |
| 2163 | /* -------------------------------------------------------------------- */ |
| 2164 | /* Serialize document. */ |
| 2165 | /* -------------------------------------------------------------------- */ |
| 2166 | char *pszDoc = CPLSerializeXMLTree(psTree); |
| 2167 | if (pszDoc == nullptr) |
| 2168 | return FALSE; |
| 2169 | |
| 2170 | const vsi_l_offset nLength = strlen(pszDoc); |
| 2171 | |
| 2172 | /* -------------------------------------------------------------------- */ |
| 2173 | /* Create file. */ |
| 2174 | /* -------------------------------------------------------------------- */ |
| 2175 | VSILFILE *fp = VSIFOpenL(pszFilename, "wt"); |
| 2176 | if (fp == nullptr) |
| 2177 | { |
| 2178 | CPLError(CE_Failure, CPLE_OpenFailed, "Failed to open %.500s to write.", |
| 2179 | pszFilename); |
| 2180 | CPLFree(pszDoc); |
| 2181 | return FALSE; |
| 2182 | } |
| 2183 | |
| 2184 | /* -------------------------------------------------------------------- */ |
| 2185 | /* Write file. */ |
| 2186 | /* -------------------------------------------------------------------- */ |
| 2187 | if (VSIFWriteL(pszDoc, 1, static_cast<size_t>(nLength), fp) != nLength) |
| 2188 | { |
| 2189 | CPLError(CE_Failure, CPLE_FileIO, |
| 2190 | "Failed to write whole XML document (%.500s).", pszFilename); |
| 2191 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
| 2192 | CPLFree(pszDoc); |
| 2193 | return FALSE; |
| 2194 | } |
| 2195 | |
| 2196 | /* -------------------------------------------------------------------- */ |
| 2197 | /* Cleanup */ |
| 2198 | /* -------------------------------------------------------------------- */ |
| 2199 | const bool bRet = VSIFCloseL(fp) == 0; |
| 2200 | if (!bRet) |
| 2201 | { |
| 2202 | CPLError(CE_Failure, CPLE_FileIO, |
| 2203 | "Failed to write whole XML document (%.500s).", pszFilename); |
| 2204 | } |
| 2205 | CPLFree(pszDoc); |
| 2206 | |
| 2207 | return bRet; |
| 2208 | } |
| 2209 | |
| 2210 | /************************************************************************/ |
| 2211 | /* CPLCleanXMLElementName() */ |
no test coverage detected