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:
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.
Still need the \0 bit though :(
Could you put the table and column names in a configuration file and have a function to read that file and populate the array?
Nope, it's got to be in an issued executable, sadly.
If you can go STL and C++11 crazy...
http://pastebin.com/7WP7fCH8
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;
}
Pengy: Still need the \0 bit though :(
No you don't. You get those for free by enclosing something in qutoes.
ignore last comment, i'm talking bollocks as usual.
Post a Comment