Skip to content

Commit e28b354

Browse files
authored
Merge pull request #225 from walkero-gr/rescan-mess
Trying to fix the mess after a new rescan
2 parents ab3b357 + 6e321ef commit e28b354

File tree

5 files changed

+144
-51
lines changed

5 files changed

+144
-51
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
## iGame VERSION_TAG - [RELEASE_DATE]
2+
### Changed
3+
- Now after the repository scans, any item that is not assigned to any genre gets the "Unknown" value by default
4+
5+
### Fixed
6+
- Fixed the generated genre list titles after a rescan of the repository
7+
- Fixed more memory leaks
8+
9+
## iGame 2.4.3 - [2023-09-01]
210
### Fixed
311
- Fixed memory leaks
412
- Fixed the lists getting wrong values when overflow

src/fsfuncs.c

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -666,48 +666,57 @@ void getIGameDataInfo(char *igameDataPath, slavesList *node)
666666
char *line = malloc(lineSize * sizeof(char));
667667
while (FGets(fpigamedata, line, lineSize) != NULL)
668668
{
669-
char **tmpTbl = my_split(line, "=");
670-
if (tmpTbl[1] != NULL)
669+
char **tokens = str_split(line, '=');
670+
if (tokens)
671671
{
672-
if (tmpTbl[1][strlen(tmpTbl[1]) - 1] == '\n')
672+
if (tokens[1] != NULL)
673673
{
674-
tmpTbl[1][strlen(tmpTbl[1]) - 1] = '\0';
675-
}
676-
else
677-
{
678-
tmpTbl[1][strlen(tmpTbl[1])] = '\0';
679-
}
674+
int tokenValueLen = strlen(tokens[1]);
675+
if (tokens[1][tokenValueLen - 1] == '\n')
676+
{
677+
tokens[1][tokenValueLen - 1] = '\0';
678+
}
679+
else
680+
{
681+
tokens[1][tokenValueLen] = '\0';
682+
}
680683

681-
if(current_settings->useIgameDataTitle && !strcmp(tmpTbl[0], "title"))
682-
{
683-
strncpy(node->title, tmpTbl[1], MAX_SLAVE_TITLE_SIZE);
684-
}
684+
if(current_settings->useIgameDataTitle && !strcmp(tokens[0], "title"))
685+
{
686+
strncpy(node->title, tokens[1], MAX_SLAVE_TITLE_SIZE);
687+
}
685688

686-
if(!strcmp(tmpTbl[0], "chipset"))
687-
{
688-
strncpy(node->chipset, tmpTbl[1], MAX_CHIPSET_SIZE);
689-
}
689+
if(!strcmp(tokens[0], "chipset"))
690+
{
691+
strncpy(node->chipset, tokens[1], MAX_CHIPSET_SIZE);
692+
}
690693

691-
if(!strcmp(tmpTbl[0], "genre"))
692-
{
693-
strncpy(node->genre, tmpTbl[1], MAX_GENRE_NAME_SIZE);
694-
}
694+
if(!strcmp(tokens[0], "genre"))
695+
{
696+
strncpy(node->genre, tokens[1], MAX_GENRE_NAME_SIZE);
697+
}
695698

696-
if(!strcmp(tmpTbl[0], "year") && isNumeric(tmpTbl[1]))
697-
{
698-
node->year=atoi(tmpTbl[1]);
699-
}
699+
if(!strcmp(tokens[0], "year") && isNumeric(tokens[1]))
700+
{
701+
node->year=atoi(tokens[1]);
702+
}
700703

701-
if(!strcmp(tmpTbl[0], "players") && isNumeric(tmpTbl[1]))
702-
{
703-
node->players=atoi(tmpTbl[1]);
704-
}
704+
if(!strcmp(tokens[0], "players") && isNumeric(tokens[1]))
705+
{
706+
node->players=atoi(tokens[1]);
707+
}
705708

706-
if(!strcmp(tmpTbl[0], "exe") && !isStringEmpty(tmpTbl[1]) && !strcasestr(tmpTbl[1], ".slave"))
709+
if(!strcmp(tokens[0], "exe") && !isStringEmpty(tokens[1]) && !strcasestr(tokens[1], ".slave"))
710+
{
711+
strncpy(node->path, tokens[1], MAX_PATH_SIZE);
712+
}
713+
}
714+
int i;
715+
for (i = 0; *(tokens + i); i++)
707716
{
708-
strncpy(node->path, tmpTbl[1], MAX_PATH_SIZE);
717+
free(*(tokens + i));
709718
}
710-
free(tmpTbl);
719+
free(tokens);
711720
}
712721
}
713722

src/funcs.c

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ static BOOL examineFolder(char *path)
788788
return FALSE;
789789
}
790790

791-
// TODO: Clear that if possible
792791
char *igameDataPath = malloc(sizeof(char) * MAX_PATH_SIZE);
793792
if(!isPathFolder(path))
794793
{
@@ -815,6 +814,8 @@ static BOOL examineFolder(char *path)
815814
node->players = 0;
816815

817816
getIGameDataInfo(igameDataPath, node);
817+
free(igameDataPath);
818+
818819
strncpy(buf, node->path, bufSize);
819820
if(!isPathFolder(path))
820821
{
@@ -825,7 +826,8 @@ static BOOL examineFolder(char *path)
825826
snprintf(node->path, sizeof(char) * MAX_PATH_SIZE, "%s/%s", path, buf);
826827
}
827828

828-
// Check the node->path and skip the save if the buf is empty, it is a slave or the file does not exist. Free the node before move on.
829+
// Check the node->path and skip the save if the buf is empty, it is a slave that aleady exists in the slaves list
830+
// or there is no executable file defined. Free the node before move on.
829831
if (
830832
isStringEmpty(buf) || !check_path_exists(node->path) ||
831833
(slavesListSearchByPath(node->path, sizeof(char) * MAX_PATH_SIZE) != NULL)
@@ -870,6 +872,7 @@ static BOOL examineFolder(char *path)
870872
node->instance = 0;
871873
node->title[0] = '\0';
872874
node->genre[0] = '\0';
875+
sprintf(node->genre,"Unknown");
873876
node->user_title[0] = '\0';
874877
node->chipset[0] = '\0';
875878
node->times_played = 0;
@@ -915,33 +918,58 @@ static BOOL examineFolder(char *path)
915918
{
916919
if (current_settings->useIgameDataTitle)
917920
{
918-
slavesList *node = malloc(sizeof(slavesList));
919-
if(node == NULL)
921+
char *igameDataPath = malloc(sizeof(char) * MAX_PATH_SIZE);
922+
if(igameDataPath == NULL)
920923
{
921924
msg_box((const char*)GetMBString(MSG_NotEnoughMemory));
922-
FreeVec(FIblock);
923-
UnLock(lock);
924-
free(buf);
925925
return FALSE;
926926
}
927927

928-
char *igameDataPath = malloc(sizeof(char) * MAX_PATH_SIZE);
929-
snprintf(igameDataPath, sizeof(char) * MAX_PATH_SIZE, "%s/%s", existingNode->path, DEFAULT_IGAMEDATA_FILE);
928+
getParentPath(existingNode->path, igameDataPath, sizeof(char) * MAX_PATH_SIZE);
929+
snprintf(igameDataPath, sizeof(char) * MAX_PATH_SIZE, "%s/%s", igameDataPath, DEFAULT_IGAMEDATA_FILE); // cppcheck-suppress sprintfOverlappingData
930930
if (check_path_exists(igameDataPath))
931931
{
932-
getIGameDataInfo(igameDataPath, node);
933-
}
934-
free(igameDataPath);
932+
slavesList *node = malloc(sizeof(slavesList));
933+
if(node == NULL)
934+
{
935+
msg_box((const char*)GetMBString(MSG_NotEnoughMemory));
936+
FreeVec(FIblock);
937+
UnLock(lock);
938+
free(buf);
939+
return FALSE;
940+
}
941+
942+
node->instance = 0;
943+
node->title[0] = '\0';
944+
node->genre[0] = '\0';
945+
sprintf(node->genre,"Unknown");
946+
node->user_title[0] = '\0';
947+
node->chipset[0] = '\0';
948+
node->times_played = 0;
949+
node->favourite = 0;
950+
node->last_played = 0;
951+
node->exists = 1;
952+
node->hidden = 0;
953+
node->deleted = 0;
954+
node->year = 0;
955+
node->players = 0;
935956

936-
if (!isStringEmpty(node->genre))
937-
strncpy(existingNode->genre, node->genre, MAX_GENRE_NAME_SIZE);
957+
getIGameDataInfo(igameDataPath, node);
938958

939-
if (!isStringEmpty(node->chipset))
940-
strncpy(existingNode->chipset, node->chipset, MAX_CHIPSET_SIZE);
959+
if (!isStringEmpty(node->genre))
960+
{
961+
strncpy(existingNode->genre, node->genre, MAX_GENRE_NAME_SIZE);
962+
addGenreInList(node->genre);
963+
}
964+
if (!isStringEmpty(node->chipset))
965+
{
966+
strncpy(existingNode->chipset, node->chipset, MAX_CHIPSET_SIZE);
967+
addChipsetInList(node->chipset);
968+
}
969+
free(node);
970+
}
941971

942-
addGenreInList(node->genre);
943-
addChipsetInList(node->chipset);
944-
free(node);
972+
free(igameDataPath);
945973
}
946974

947975
// Generate title and add in the list

src/strfuncs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,53 @@ char** my_split(char* str, char* spl)
148148
return ret;
149149
}
150150

151+
char **str_split(char *a_str, const char a_delim)
152+
{
153+
char** result = 0;
154+
size_t count = 0;
155+
char* tmp = a_str;
156+
char* last_comma = 0;
157+
char delim[2];
158+
delim[0] = a_delim;
159+
delim[1] = 0;
160+
161+
/* Count how many elements will be extracted. */
162+
while (*tmp)
163+
{
164+
if (a_delim == *tmp)
165+
{
166+
count++;
167+
last_comma = tmp;
168+
}
169+
tmp++;
170+
}
171+
172+
/* Add space for trailing token. */
173+
count += last_comma < (a_str + strlen(a_str) - 1);
174+
175+
/* Add space for terminating null string so caller
176+
knows where the list of returned strings ends. */
177+
count++;
178+
179+
result = malloc(sizeof(char*) * count);
180+
181+
if (result)
182+
{
183+
size_t idx = 0;
184+
char* token = strtok(a_str, delim);
185+
186+
while (token)
187+
{
188+
// assert(idx < count);
189+
*(result + idx++) = strdup(token);
190+
token = strtok(0, delim);
191+
}
192+
// assert(idx == count - 1);
193+
*(result + idx) = 0;
194+
}
195+
196+
return result;
197+
}
151198

152199
int get_delimiter_position(const char* str)
153200
{

src/strfuncs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ char *strcasestr(const char *, const char *);
2727
char* strdup(const char *);
2828
void string_to_lower(char *);
2929
char** my_split(char *, char *);
30+
char **str_split(char *, const char);
3031
int get_delimiter_position(const char *);
3132
void add_spaces_to_string(const char *, char *, int);
3233
STRPTR substring(STRPTR, int, int);

0 commit comments

Comments
 (0)