Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 133 134 135 136 137 138 < 139 > 140 141 142 143 144 145 .. 168 >> Next

Important note: You should not rely on any of these examples, or the methods they contain, in your own applications without having completely satisfied yourself that they are correct and appropriate for your needs. They are intended only to illustrate how techniques discussed in earlier chapters can be applied.
10.1 STRING FUNCTIONS
Excel has a number of very efficient basic string functions, but string operations can quickly become unnecessarily complex when just using these. Consider, for example, the case where you want to substitute commas for stops (periods) dynamically. This is easily done using Excel’s SUBSTITUTE(). However, if you want to simultaneously substitute commas for stops and stops for commas things are more complex. (You could do this in three applications of SUBSTITUTE(), but this is messy.) Writing a function in C that does this is straightforward (see replace_mask() below).
336
Excel Add-in Development in C/C++
The C and C++ libraries both contain a number of low-level string functions that can easily be given Excel worksheet wrappers or declared and used from VBA. (The latter is a good place to start when optimising VB code.) This section presents a number of example functions, some of which are just wrappers of standard library functions and some of which are not. The code for all of these functions is listed in the Example project on the CD ROM in the source file XllStrings.cpp. When registered with Excel, they are added to the Text category.
Function name count_char (exported) CountChar (registered with Excel)
Description Counts the number of occurrences of a given character.
Prototype short stdcall count_char(char *text, short ch) ;
Type string "ICI"
Notes Safe to return a short as Excel will only pass a 255-max character string to the function. Function does not need to be volatile and does not access any C API functions that might require it to be registered as a macro sheet equivalent function.
short stdcall count_char(char *text, short ch)
{
if(!text || ch <= 0 || ch > 255) return 0;
short count = 0;
while(*text)
if(*text++ == ch) count++;
return count;
}
Function name replace_mask (exported) ReplaceMask (registered with Excel)
Description Replaces all occurrences of characters in a search string with corresponding characters from a replacement string, or removes all such occurrences if no replacement string is provided.
Prototype void stdcall replace mask(char *text, char *old_chars, xloper *op_new_chars);
Type string "1CCP"
Example Add-ins and Financial Applications
337
Notes Declared as returning void. Return value is the 1st argument modified in place. Third argument is optional and passed as an oper (see page 119) to avoid the need to dereference a range reference.

void stdcall replace_mask(char *text, char *old_chars, xloper
*op_new_chars)
{
if(!text || !old_chars) return;
char *p_old, *p;
if((op_new_chars->xltype & (xltypeMissing | xltypeNil)))
{
// Remove all occurrences of all characters in old_chars for(; *text; text++)
{
p_old = old_chars;
for(;*p_old;)
{
if(*text == *p_old++)
{
p = text; do {
*p = p[1];
}
while (*(++p));
}
}
}
return;
}
// Substitute all occurrences of old chars with corresponding new if(op_new_chars->xltype != xltypeStr
|| (char)strlen(old_chars) != op_new_chars->val.str[0]) return;
char *p_new;
for(; *text; text++)
{
p_old = old_chars;
p_new = op_new_chars->val.str;
for(; *p_old; p_old++, p_new++)
{
if(*text == *p_old)
{
*text = *p_new; break;
}
}
}
}
338
Excel Add-in Development in C/C++
Function name reverse_text (exported) Reverse (registered with Excel)
Description Reverses a string.
Prototype void stdcall reverse_text(char *text);
Type string " ]_f "
Notes Declared as returning void. Return value is the 1st argument modified in place. This function is simply a wrapper for the C library function strrev (). This function is useful in the creation of Halton quasi-random number sequences, for example.
void stdcall reverse_text(char *text)
{
strrev(text);
}
Function name find_first (exported) FindFirst (registered with Excel)
Description Returns the position of the first occurrence of any character from a search string, or zero if none found.
Prototype short stdcall first inclusive(char *text, char *search_text);
Type string "ICC"
Notes Any error in input is reflected with a zero return value, rather than an error type. This function is simply a wrapper for the C library function strpbrk ().
short stdcall find_first(char *text, char *search_text)
{
if(!text || !search_text) return 0;
char *p = strpbrk(text, search_text);
if(!p)
return 0;
return 1 + p - text;
}
Example Add-ins and Financial Applications
339
Function name f ind_f irst_excluded (exported) FindFirstExcl (registered with Excel)
Description Returns the position of the first occurrence of any character that is not in a search string, or zero if no such character is found.
Prototype short stdcall find_first_excluded(char *text, char * search_text);
Previous << 1 .. 133 134 135 136 137 138 < 139 > 140 141 142 143 144 145 .. 168 >> Next