** msPostGISReplaceBoxToken() ** ** Convert a fromsource data statement into something usable by replacing the !BOX! token. ** ** Returns malloc'ed char* that must be freed by caller. */
| 1738 | ** Returns malloc'ed char* that must be freed by caller. |
| 1739 | */ |
| 1740 | static char *msPostGISReplaceBoxToken(layerObj *layer, rectObj *rect, const char *fromsource) |
| 1741 | { |
| 1742 | char *result = NULL; |
| 1743 | |
| 1744 | if ( strstr(fromsource, BOXTOKEN) && rect ) { |
| 1745 | char *strBox = NULL; |
| 1746 | char *strSRID = NULL; |
| 1747 | |
| 1748 | /* We see to set the SRID on the box, but to what SRID? */ |
| 1749 | strSRID = msPostGISBuildSQLSRID(layer); |
| 1750 | if ( ! strSRID ) { |
| 1751 | return NULL; |
| 1752 | } |
| 1753 | |
| 1754 | /* Create a suitable SQL string from the rectangle and SRID. */ |
| 1755 | strBox = msPostGISBuildSQLBox(layer, rect, strSRID); |
| 1756 | if ( ! strBox ) { |
| 1757 | msSetError(MS_MISCERR, "Unable to build box SQL.", "msPostGISReplaceBoxToken()"); |
| 1758 | if (strSRID) free(strSRID); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | |
| 1762 | /* Do the substitution. */ |
| 1763 | while ( strstr(fromsource, BOXTOKEN) ) { |
| 1764 | char *start, *end; |
| 1765 | char *oldresult = result; |
| 1766 | size_t buffer_size = 0; |
| 1767 | start = strstr(fromsource, BOXTOKEN); |
| 1768 | end = start + BOXTOKENLENGTH; |
| 1769 | |
| 1770 | buffer_size = (start - fromsource) + strlen(strBox) + strlen(end) +1; |
| 1771 | result = (char*)msSmallMalloc(buffer_size); |
| 1772 | |
| 1773 | strlcpy(result, fromsource, start - fromsource +1); |
| 1774 | strlcpy(result + (start - fromsource), strBox, buffer_size-(start - fromsource)); |
| 1775 | strlcat(result, end, buffer_size); |
| 1776 | |
| 1777 | fromsource = result; |
| 1778 | if (oldresult != NULL) |
| 1779 | free(oldresult); |
| 1780 | } |
| 1781 | |
| 1782 | if (strSRID) free(strSRID); |
| 1783 | if (strBox) free(strBox); |
| 1784 | } |
| 1785 | else |
| 1786 | { |
| 1787 | result = msStrdup(fromsource); |
| 1788 | } |
| 1789 | return result; |
| 1790 | |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | ** msPostGISBuildSQLFrom() |
no test coverage detected