/* Copyright (C) 2003 by NCR Corporation. All Rights Reserved. NCR CONFIDENTIAL AND TRADE SECRET */ /* REPLACE Syntax REPLACE(char, search_string [, replacement_string]) Purpose Returns char with every occurrence of search_string replaced with replacement_string, where char, search_string, and replacement_string are string arguments. Usage Notes If replacement_string is omitted or null, all occurrences of search_string are removed. If search_string is null, then char is returned. This function provides a super-set of the functionality provided by the TRANSLATE function. TRANSLATE provides-single character, one-to-one, substitution. REPLACE allows you to substitute one string for another as well as to remove character strings. Example SELECT REPLACE('JACK and JUE','J','BL') "Changes" FROM DUAL; Returns the following result: Changes --------------- BLACK and BLUE */ #define SQL_TEXT Latin_Text #include "sqltypes_td.h" #include #include #define MAX_STRING_LENGTH 256 #define IsNull -1 #define IsNotNull 0 #define NoSqlError "00000" void oreplace2(VARCHAR_LATIN *inputStr, VARCHAR_LATIN *inputFrom, VARCHAR_LATIN *inputTo, VARCHAR_LATIN *result, int *inputStrIsNull, int *inputFromIsNull, int *inputToIsNull, int *resultIsNull, char sqlstate[6], SQL_TEXT extname[129], SQL_TEXT specific_name[129], SQL_TEXT error_message[257]) { int linputFrom; int linputTo; char *pinputStr; /* Initiate return values */ strcpy(sqlstate, NoSqlError); strcpy((char *) error_message, " "); *resultIsNull = IsNotNull; *result = '\0'; if (*inputStrIsNull == IsNull) { strcpy(sqlstate, "22004") ; strcpy((char *) error_message, "First argument cannot be null.") ; *resultIsNull = IsNull; return; } if (*inputFromIsNull == IsNull || (linputFrom=strlen((const char *)inputFrom)) == 0) { strcpy((char *)result, (const char *)inputStr); return; } linputTo = (*inputToIsNull == IsNull ? 0 : strlen((const char *)inputTo)); /* Replace */ for (pinputStr = (char *)inputStr; *pinputStr != '\0'; ) { if (strncmp((const char *)pinputStr, (const char *)inputFrom, linputFrom) == 0) { strncat((char *)result, (const char *)inputStr, (int)(pinputStr-(char *)inputStr)); if (linputTo != 0) strcat((char *)result, (const char *)inputTo); pinputStr = (char *)inputStr = pinputStr + linputFrom; } else { ++pinputStr; } } strcat((char *)result, (const char *)inputStr); }