Here is the function that isn't working:
char * insert_symtab(char *buf)
{
char *ret = (char *)malloc(strlen(buf) + 512);
char *tmp = NULL;
char *repl = NULL;
char obuf[32]; //for converting int to hex
Symtab_struct *cursym;
lineQueue *lq = readlines(buf);
int i;
strcpy(ret, "");
while (!lq->empty())
{
tmp = getlinefromqueue(lq);
for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
{
sprintf(obuf, "0x%04x", cursym->sym_location);
//printf("-->looking for %s\n", cursym->sym_name);
if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
{
printf("---->Found %s\n", cursym->sym_name);
strncpy (repl, obuf, strlen(cursym->sym_name));
}
}
strcat(ret, tmp);
strcat(ret, "\n");
}
return ret;
}
When run like this, I get the following output:
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
$start
And, if I go in and change
if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
to
if ((repl = strstr(tmp, "$start")) != NULL)
I get the following output:
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
---->Found $start
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
0x0000
As it should be. Attached are pastebin links to the whole project:
main.cpp: http://pastebin.com/AiDCbsCt
Symtab.h: http://pastebin.com/Kmcn6NzV
Symtab.cpp: http://pastebin.com/mxtPL1d2
Any ideas?