Wednesday, July 16, 2008

[VC++] How to Convert from CString to const char * and back

Dealing with VC++, this is only one of the painful conversions you could have to perform if you're working with CString and you need to pass down to some function (or whatever) a const char *.

Code-snippet follows (UNICODE on):

CString myString = "bollocks";

char stoneageBuffer[100];
//initialize your stoneage buffer
memset(stoneageBuffer,0,sizeof(stoneageBuffer));
sprintf(stoneageBuffer,"%S",myString);

//...

//back to CString
myString = stoneageBuffer;


Note that %S (and not %s) formatting type switches to the opposite type of character set (UNICODE if MBCS build and MBCS if UNICODE build).

Sunday, July 13, 2008

[.NET] How to Validate strings with Regular Expressions

Hi fellas,

a tiny code-snippet to show how to use regular expressions to validate strings in .NET:


//include this
using System.Text.RegularExpressions;
//...
string myRegEx = @"^ScuffiaIsOneBigFatFaggot$";
string myStringToValidate;
//...
//fill string to validate from input or whatever
//...
if (Regex.IsMatch(myRegEx, myStringToValidate))
Console.WriteLine("String is valid!");
else
Console.WriteLine("String is rubbish");
//...


It's worth to underline that Regular expressions are case-sensitive, even in VB. I hate writing Regular Expressions, but Scuffia is pretty good at it so when I can't find stuff on google I always bug him when I need to validate some data. For example a couple of weeks ago I needed to validate an email and I asked him for help.
He sent this regular expression straightaway:


^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))
([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$


Writing regular expressions is a mess but reading one if someone else wrote it is definitely something you don't wanna even think about: gotta have blind faith sometimes in this job - just remember to throw some bones to them SQA people (they're paid to break what us developers put together after all).

Thanx Scuffia, the email validation reg ex is working pretty well.

Wednesday, July 9, 2008

[eVC++] Can't include iostream

Problem: in Embedded VC++ 3/4 you can't seem to include iostream

Solution: Embedded VC++ 3/4 does not support iostream - use stdio.h instead

This got me stuck for a while - it's just a good example of the limitation imposed by development on Windows CE: cout is way too fancy for eVC++ ... you'll have to live with the good old printf.

Monday, May 19, 2008

Crystal Reports - Request canceled by user

Problem: You get this error message when programmatically printing a Crystal Reports Report "Error in file [filepath].rpt - Request canceled by user"

Solution: This happens because your default printer is not available - make sure it is online and available.

OK, this post sucks, but eventually it will be useful to someone in trouble with Crystal Reports. By the way Crystal Reports sucks too - if you wanna read about the reasons why chek this post out http://dotnetbutchering.blogspot.com/2008/02/net-vc-7-good-reasons-to-hate-crystal.html .

Tuesday, May 13, 2008

Worst Cover Ever in History of Programming - Wendy Sarret ROCKS


Today I was digging into the library at work and I found something really weird between tons of old books covered with dust: VISUAL C++ DATABASE PROGRAMMING TUTORIAL (book ref: http://www.amazon.co.uk/Visual-Database-Programming-Tutorial-Programmer/dp/1861002416 ). I couldn't help myself but start showing it to everybody in the office and having big laughs all around the place. A guy asked me "who's that kid? he looks sick" - that's not a kid, that's Wendy Sarret. I am gonna keep the book at my desk so I can watch it every time I feel blue because of some COM object.

No doubt this is the lamest cover EVER in programming history. And You just can't stop watching the damn thing (look at that HUGE keyboard - wtf is that? a bodyboard?).
What about the book? No idea, I can't get past the cover.