Mostly Bollogs, I'm afraid

But occasionally, a glimmer of truth.
If you find one, please let me know.



Wednesday 6 May 2015

Strings

I have this at the moment:

TCHAR *tTables[]={
L"SITE",
L"Site\0bigint\0Friendly\0varchar(64)\0",
L"USERS",
L"username\0varchar(80)\0pass\0varchar(80)\0secu\0tinyint\0",
L"\0"
};

There are loads more of them than that.

Each par of lines is a Table name followed by a list of column names and types, null-separated.

I want to make it more readable.

Is it doable?


8 comments:

Anonymous said...

Only have gcc here but can't you just concatenate the field strings?

char *tTables[] = {
"SITE",
"Site" "bigint" "Friendly" "varchar(64)...

Not much more readable but a bit better.

Uncle Marvo said...

Still need the \0 bit though :(

JamesW said...

Could you put the table and column names in a configuration file and have a function to read that file and populate the array?

Uncle Marvo said...

Nope, it's got to be in an issued executable, sadly.

JamesW said...

If you can go STL and C++11 crazy...
http://pastebin.com/7WP7fCH8

Uncle Marvo said...

Pretty much what I ended up doing :)

void maketable(TCHAR *tinTableName, ...)
{
// Allocate memory for tables
if (!tables)
tables=(cTable **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(cTable *)*2);
else
tables=(cTable **)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tables, sizeof(cTable *)*(iTables+2)*2);
va_list ap;
va_start(ap, tinTableName);
TCHAR *tTableName = tinTableName, *tColumnName, *tColumnType; // table name
TCHAR *tColumns=NULL;
int iColumsStringSize=0;
while (1)
{
tColumnName=va_arg(ap, TCHAR *);
if (!tColumnName || !*tColumnName)
break;
tColumnType=va_arg(ap, TCHAR *);
if (!tColumnType || !*tColumnType)
break;
// process column
iColumsStringSize+=(wcslen(tColumnName)+wcslen(tColumnType)+3)*sizeof(TCHAR); // two strings, two eroes, final zero
if (!tColumns)
tColumns=(TCHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, iColumsStringSize);
else
tColumns=(TCHAR *)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tColumns, iColumsStringSize);
// find the end of the string (two 0's)
TCHAR *tEOS=tColumns;
while (*tEOS!=0)
{
tEOS+=wcslen(tEOS)+1;
}
wcscat_s(tEOS, wcslen(tColumnName)+1, tColumnName);
tEOS+=wcslen(tColumnName)+1;
wcscat_s(tEOS, wcslen(tColumnType)+1, tColumnType);
tEOS+=wcslen(tColumnType)+1;
wcscat_s(tEOS, 1, L"\0");
}
tables[iTables]=new cTable(tTableName, tColumns);
HeapFree(GetProcessHeap(), 0, tColumns);
iTables++;
va_end(ap);
return;
}

Anonymous said...

Pengy: Still need the \0 bit though :(

No you don't. You get those for free by enclosing something in qutoes.

Anonymous said...

ignore last comment, i'm talking bollocks as usual.