Add a way to seek a SECTION by name without creating one

This commit is contained in:
ISSOtm
2019-09-08 23:40:16 +02:00
parent 54ed050ecf
commit 74f43d4e09
2 changed files with 32 additions and 21 deletions

View File

@@ -29,6 +29,7 @@ extern char *tzObjectname;
void out_PrepPass2(void);
void out_SetFileName(char *s);
struct Section *out_FindSectionByName(const char *pzName);
void out_NewSection(char *pzName, uint32_t secttype);
void out_NewAbsSection(char *pzName, uint32_t secttype, int32_t org,
int32_t bank);

View File

@@ -599,48 +599,52 @@ void out_SetFileName(char *s)
printf("Output filename %s\n", s);
}
struct Section *out_FindSectionByName(const char *pzName)
{
struct Section *pSect = pSectionList;
while (pSect) {
if (strcmp(pzName, pSect->pzName) == 0)
return pSect;
pSect = pSect->pNext;
}
return NULL;
}
/*
* Find a section by name and type. If it doesn't exist, create it
*/
struct Section *out_FindSection(char *pzName, uint32_t secttype, int32_t org,
int32_t bank, int32_t alignment)
{
struct Section *pSect, **ppSect;
struct Section *pSect = out_FindSectionByName(pzName);
ppSect = &pSectionList;
pSect = pSectionList;
while (pSect) {
if (strcmp(pzName, pSect->pzName) == 0) {
if (pSect) {
if (secttype == pSect->nType
&& ((uint32_t)org) == pSect->nOrg
&& ((uint32_t)bank) == pSect->nBank
&& ((uint32_t)alignment == pSect->nAlign)) {
return pSect;
}
fatalerror("Section already exists but with a different type");
}
ppSect = &(pSect->pNext);
pSect = pSect->pNext;
}
pSect = malloc(sizeof(struct Section));
*ppSect = pSect;
if (pSect == NULL)
fatalerror("Not enough memory for section");
pSect->pzName = malloc(strlen(pzName) + 1);
pSect->pzName = strdup(pzName);
if (pSect->pzName == NULL)
fatalerror("Not enough memory for sectionname");
strcpy(pSect->pzName, pzName);
pSect->nType = secttype;
pSect->nPC = 0;
pSect->nOrg = org;
pSect->nBank = bank;
pSect->nAlign = alignment;
pSect->pNext = NULL;
pSect->pNext = pSectionList;
pSect->pPatches = NULL;
/* It is only needed to allocate memory for ROM sections. */
@@ -655,7 +659,13 @@ struct Section *out_FindSection(char *pzName, uint32_t secttype, int32_t org,
pSect->tData = NULL;
}
return (pSect);
/*
* Add the new section to the list
* at the beginning because order doesn't matter
*/
pSectionList = pSect;
return pSect;
}
/*