ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > Living Room

Insert Char into String - I "Hit the Wall" ...again.

(1/2) > >>

Stoic Joker:
Okay in attempting what's supposed to be a simple function...I have once again been thwarted at every turn.

(As usual I'm working in pure Win32 API C++)

I'm grabbing a block of text from an edit control, this is then passed to a function that puts it together with other bits of info that are used to build a SQL Query. The SQL Query is then fired against the db where it hopefully doesn't fail (Error checking abounds...).

Simple Right? ...Ha!

The problem is that if someone enters a possessive like "Joker's", the apostrophe which is a SQL control character causes the Query/function to fail. So I'm trying to replace every instance of ' with '' (Which is 2 apostrophes Not a double quote). Therein lying the rub...

I have spent the last 3 days trying the find a way to walk the string and insert a 2nd apostrophe anywhere there is a first. Everything that Google turns up referrers to the insert(...) function in namespace std; ... That's nice... except my use of the StringSafe routines (in the other 80 pages of code...) causes the compiler to spew errors for about a half an hour due to the inclusion of string.h (required by namespace std; ) .

I've seen this type of function before where the string to parsed through a series of pointers... but I never have quite understood it. ...Hence my inability to use it effectively.

So... Here's the block of poo I'm working with which will hopefully convey what I'm after:

--- Code: C++ ---void CleanString(char *szDirty) {        char  *p;  p =  szDirty;  if(p == 0) {          return;  }else{          while(*p) {                  if(*p == '\'') {                          MessageBox(0, p, "Found IT!", MB_OK);                          *p += '\'';                          MessageBox(0, p, "*pTmp++ = ?", MB_OK);                          p = CharNext(p);//                        pTmp += 1;                  }else{                          p = CharNext(p);                  }          }          *p = 0;          MessageBox(0, p, "*pTmp = 0", MB_OK);  }}
The MessageBoxes are only there for debugging purposes so I can tell what it's interpretation of what I just told it was.

Everything I try either drops the 2nd apostrophe or the s that comes after it. *Sigh*

What am I doing wrong?

Thank You,
Stoic Joker

mwb1100:
The inster() method is part of the C++ string class, so you'd need to include <string>  - not <string.h> which gets you the good, old standard C string functions (which, as you find, are deprecated in MSVC 9 for the most part as being unsafe).  Since you're using raw strings instead of the string class, the insert() method will not be much help unless you're willing to move to using the string class.  That might be a worthwhile thing, but it might be a lot of work.

If you're going to insert a character in a raw string, you'll need to move the characters in the tail end of string (after the insertion point) over one character each.  So before line 10 in your example, you'll need something equivalent to:


--- Code: C++ ---memmove( p + 1, p, strlen( p) + 1)
But you'll need to adjust that bit of code to use the SafeString equivalent to memmove().  Note that you need memmove() semantics - not memcpy() - since the source and destination buffers overlap.

Stoic Joker:
 :wallbash: :huh: :graduate:

And the Correct Answer is ... What ^^he^^ said.

I pasted in the example you gave, compiled, tossed in a buffer chocking block of text, and it worked perfectly!!!

So I'll assume memmove is StringSafe ... Safe (as it are Working).


Thank You!

(I think I'll go outside now and see what sun light looks like... ;))

mwb1100:
So I'll assume memmove is StringSafe ... Safe (as it are Working).
-Stoic Joker (August 27, 2008, 11:32 AM)
--- End quote ---

Uh oh.... my example is not exactly safe.  For simplicity's sake there were a couple things left out that you may want to check:


* the example I gave does not ensure that there's enough buffer for the memmove() - if the string passed in to the CleanString() function happens to fill its allocated memory exactly, the memmove will overrun the buffer (which is exactly the type of problem that the SafeString routines were intended to prevent).  Since the buffer size is not passed in to CleanString() I'm not sure that you can grow the string in place safely.  You may need to change the CleanString() interface to include the buffer size and change the memmove() call to ensure that the buffer is not overrun and that the resulting expanded string is always null terminated.
* The memmove() is not Unicode aware.  The CleanString() function you presented is also not Unicode aware, but to make it at least slightly easier to port, the memmove() call should probably look like "memmove( p + 1, p, (_tcslen( p) + 1) * sizeof (*p))"

Stoic Joker:
LOL Yes I caught that part, By "Safe" I meant the functions didn't collide/conflict/refuse to compile.

The edit control the text comes from is input size limited to 500 characters and the buffer is 512 - I'm assuming nobody will use that many apostrophes...

If I have CleanString() stop before adding too many characters the SQL query/very next function will fail anyway...So it's kind of a Catch22. The SQL query string buffer is (defined in the main header file as) 2048 because SQL queries tend to be large & I hate having to define buffer sizes locally and then remember how big they were 10 pages later when I try to toss something at the function.

Dynamic buffer allocation is one of those things I never have figured out so resizing on the fly might take a while (Suggestions I'm open for...).

I wasn't thinking about the unicode part as this is a in-house only app, and nobody is going to by typing Chinese into it ... but that is a bad habbit I'm getting into.

Navigation

[0] Message Index

[#] Next page

Go to full version