#include "stdafx.h"
#include "global.hpp"
//---------------------------------------------------------------------------
// Description: generate an error message
//---------------------------------------------------------------------------
int Error(long err, char *fmt, ...)
{
static char buf[256];
static char buf2[256];
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
sprintf(buf2, "Error: %s (HRESULT= 0x%x)", buf, err);
va_end(args);
AfxMessageBox(buf2);
TRACE2("Error: HRESULT: 0x%x, %s\n", err, buf);
return err;
}
//---------------------------------------------------------------------------
// Description: returns the next complete word found starting at string
// index sIdx
//---------------------------------------------------------------------------
int getWordFrom( CString &instr, int &sIdx, CString &word )
{
int len = instr.GetLength();
word = "";
if ( sIdx == len ) return FALSE;
for ( ; sIdx < len; sIdx++) { // find first non-space
if (instr[sIdx] != ' ') break;
}
for (int e=sIdx; e < len; e++) { // find the first space
if (instr[e] == ' ') break;
}
if (sIdx == len) return FALSE;
word = instr.Mid(sIdx, e-sIdx);
sIdx = e;
return TRUE;
}
//---------------------------------------------------------------------------
// Description: returns the nth word in instr. The word is returned in
// word.
//---------------------------------------------------------------------------
int words( CString &instr )
{
int count = 0;
int idx = 0;
CString word("");
while (1) {
if ( ! getWordFrom( instr, idx, word ) ) break;
count++;
}
return count;
}
//---------------------------------------------------------------------------
// Description: returns the nth word in instr. The word is returned in
// word.
//---------------------------------------------------------------------------
int word( CString &instr, int n, CString &word )
{
int idx = 0;
word = "";
for (int i = 1; i <= n; i++) {
if ( ! getWordFrom( instr, idx, word ) ) break;
}
return (word != "");
}
//---------------------------------------------------------------------------
// Description: returns a series of words in instr. The words are
// returned in word.
// Parameters: sn = starting word index (starts at 1)
// nw = # of words to retrieve (-1 means remaining words)
//---------------------------------------------------------------------------
int subword( CString &instr, int sn, int nw, CString &substr )
{
substr = "";
if (0 == nw) return TRUE; // OK, you asked for 0 words, you got 0 words
if (sn < 1) return FALSE; // bad parameter, word index starts at 1
int idx = 0;
CString word;
// first get to the first wanted word
for (int i = 1; i < sn; i++) {
if ( ! getWordFrom( instr, idx, word ) ) break;
}
// get all the words needed
i = 0;
while (1) {
if ( ! getWordFrom( instr, idx, word ) ) break;
if (i > 0) substr += " ";
substr += word;
if (++i == nw) break;
}
return (substr != "");
}
//---------------------------------------------------------------------------
// Description: returns the index or the word that matches the character */
// position in the string */
//---------------------------------------------------------------------------
int wordIdxFromCharIdx( CString &instr, int cIdx)
{
int idx = 0;
int wIdx = 0;
CString word;
while (1) {
if ( ! getWordFrom( instr, idx, word ) ) break;
wIdx++;
if ( cIdx < idx ) return wIdx;
}
return 0;
}