Skip to content

Commit

Permalink
Adjusted flags to make switch-case cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
iDestyKK committed Aug 7, 2022
1 parent 61360c3 commit 1bc8261
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
31 changes: 14 additions & 17 deletions c/lib/ards_util/io.c
Expand Up @@ -102,7 +102,10 @@ void file_read_cheats_and_folders(
}
else {
// If it's a folder and blank, just ignore it
if ((tmp_data.flag & 0x03) == 0x02 && tmp_data.num_entries == 0)
if (
(tmp_data.flag & 0x03) == AR_FLAG_FOLDER &&
tmp_data.num_entries == 0
)
continue;

// Add to vector and get a pointer
Expand All @@ -111,7 +114,7 @@ void file_read_cheats_and_folders(
}

// Act based on it being either a folder or cheat code
switch ((ar_flag_t) header->flag & 0xFF) {
switch ((ar_flag_t) header->flag & 0x03) {
case AR_FLAG_TERMINATE:
/*
* Go back 4 bytes and make previous call re-read so recursion
Expand All @@ -123,7 +126,6 @@ void file_read_cheats_and_folders(
return;

case AR_FLAG_CODE:
case AR_FLAG_MASTER:
// It's an AR code.
// This means (8 * num_entries) bytes are read
header->data = cn_vec_init(ar_line_t);
Expand All @@ -136,8 +138,7 @@ void file_read_cheats_and_folders(

break;

case AR_FLAG_FOLDER1:
case AR_FLAG_FOLDER2:
case AR_FLAG_FOLDER:
header->data = cn_vec_init(ar_data_t);
file_read_cheats_and_folders(
fp,
Expand All @@ -162,9 +163,8 @@ void file_read_names(FILE *fp, CN_VEC root) {
it->desc = file_read_string(fp);

if (it->data != NULL) {
switch ((ar_flag_t) it->flag & 0xFF) {
case AR_FLAG_FOLDER1:
case AR_FLAG_FOLDER2:
switch ((ar_flag_t) it->flag & 0x03) {
case AR_FLAG_FOLDER:
// AR Folders are recursive
file_read_names(fp, it->data);
break;
Expand Down Expand Up @@ -291,9 +291,8 @@ void ards_game_export_as_xml_rec(FILE *out, CN_VEC root, size_t depth) {

cn_vec_traverse(root, it) {
if (it->data != NULL) {
switch ((ar_flag_t) it->flag & 0xFF) {
switch ((ar_flag_t) it->flag & 0x03) {
case AR_FLAG_CODE:
case AR_FLAG_MASTER:
__tabs(out, depth + 2);
fprintf(out, "<cheat>\n");

Expand All @@ -311,7 +310,7 @@ void ards_game_export_as_xml_rec(FILE *out, CN_VEC root, size_t depth) {
fprintf(out, "<codes>");

// If a Master Code, put "master" before the code hex
if (((ar_flag_t) it->flag & 0xFF) == AR_FLAG_MASTER) {
if ((ar_flag_t) it->flag & AR_FLAG_MASTER) {
fprintf(
out,
"master%s",
Expand All @@ -338,8 +337,7 @@ void ards_game_export_as_xml_rec(FILE *out, CN_VEC root, size_t depth) {
fprintf(out, "</cheat>\n");
break;

case AR_FLAG_FOLDER1:
case AR_FLAG_FOLDER2:
case AR_FLAG_FOLDER:
__tabs(out, depth + 2);
fprintf(out, "<folder>\n");

Expand All @@ -353,7 +351,7 @@ void ards_game_export_as_xml_rec(FILE *out, CN_VEC root, size_t depth) {
}

// Radio Button Folder (only 1 code allowed on at once)
if (((ar_flag_t) it->flag & 0xFF) == AR_FLAG_FOLDER2) {
if ((ar_flag_t) it->flag & AR_FLAG_ONLYONE) {
__tabs(out, depth + 3);
fprintf(out, "<allowedon>1</allowedon>\n");
}
Expand Down Expand Up @@ -403,14 +401,13 @@ void library_obliterate(CN_VEC root) {
free(it->desc);

if (it->data != NULL) {
switch ((ar_flag_t) it->flag & 0xFF) {
switch ((ar_flag_t) it->flag & 0x03) {
case AR_FLAG_CODE:
// AR Code lists are just a list of ar_line_t
cn_vec_free(it->data);
break;

case AR_FLAG_FOLDER1:
case AR_FLAG_FOLDER2:
case AR_FLAG_FOLDER:
// AR Folders are recursive
library_obliterate(it->data);
break;
Expand Down
12 changes: 7 additions & 5 deletions c/lib/ards_util/io.h
Expand Up @@ -100,11 +100,13 @@ typedef struct AR_LINE_T {
*/

typedef enum AR_FLAG_T {
AR_FLAG_TERMINATE = 0x00,
AR_FLAG_CODE = 0x01,
AR_FLAG_FOLDER1 = 0x02,
AR_FLAG_FOLDER2 = 0x06,
AR_FLAG_MASTER = 0x11
AR_FLAG_TERMINATE = 0x0000, /* 0000 0000 0000 0000 */
AR_FLAG_CODE = 0x0001, /* 0000 0000 0000 0001 */
AR_FLAG_FOLDER = 0x0002, /* 0000 0000 0000 0010 */
AR_FLAG_ONLYONE = 0x0004, /* 0000 0000 0000 0100 */
AR_FLAG_ON_ALWAYS = 0x0008, /* 0000 0000 0000 1000 */
AR_FLAG_MASTER = 0x0010, /* 0000 0000 0001 0000 */
AR_FLAG_ON_DEFAULT = 0x8000 /* 1000 0000 0000 0000 */
} ar_flag_t;

/*
Expand Down
8 changes: 3 additions & 5 deletions c/src/ards_game_ls.c
Expand Up @@ -179,24 +179,22 @@ int verify_code_segment(
pos += 4;

// Act based on flag
switch (flag) {
switch (flag & 0x03) {
case AR_FLAG_CODE:
case AR_FLAG_MASTER:
// All codes are 8 bytes. So n * 8.
pos += 8 * num;
c_found++;

break;

case AR_FLAG_FOLDER1:
case AR_FLAG_FOLDER2:
case AR_FLAG_FOLDER:
// ARDS doesn't allow nested folders. Cheat and avoid recursion
for (j = 0; j < num; j++) {
// They better be codes or else...
in_flag = (ar_flag_t) (*(uint8_t *) &buf[pos ]);
in_num = *(uint16_t *) &buf[pos + 2] ;

if (in_flag != AR_FLAG_CODE && in_flag != AR_FLAG_MASTER) {
if ((in_flag & 0x03) != AR_FLAG_CODE) {
// Flag inside folder was not a code
*err_pos = pos;
return 2;
Expand Down

0 comments on commit 1bc8261

Please sign in to comment.