Skip to content

Commit

Permalink
INI opcode updates. More unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Oct 6, 2024
1 parent e03140e commit 42c60f7
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 47 deletions.
91 changes: 77 additions & 14 deletions cleo_plugins/IniFiles/IniFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,44 @@ class IniFiles
OPCODE_READ_PARAM_STRING(section);
OPCODE_READ_PARAM_STRING(key);

auto result = GetPrivateProfileInt(section, key, 0x80000000, path);
char buff[32];
if (GetPrivateProfileString(section, key, NULL, buff, sizeof(buff), path))
{
char* str;
int base;
if (StringStartsWith(buff, "0x", false)) // hex int
{
str = buff + 2;
base = 16;
}
else // decimal int
{
str = buff;
base = 10;
}

// parse
char* end;
int value = strtol(str, &end, base);
if (end != str) // at least one number character consumed
{
OPCODE_WRITE_PARAM_INT(value);
OPCODE_CONDITION_RESULT(true);
return OR_CONTINUE;
}
}

OPCODE_WRITE_PARAM_INT(result);
OPCODE_CONDITION_RESULT(result != 0x80000000);
// failed
if (IsLegacyScript(thread))
{
OPCODE_WRITE_PARAM_INT(0x80000000); // CLEO4 behavior
}
else
{
OPCODE_SKIP_PARAMS(1);
}

OPCODE_CONDITION_RESULT(false);
return OR_CONTINUE;
}

Expand All @@ -60,6 +94,11 @@ class IniFiles
char strValue[32];
_itoa(value, strValue, 10);
auto result = WritePrivateProfileString(section, key, strValue, path);

if (GetLastError() == ERROR_FILE_NOT_FOUND) // path points directory
{
result = false;
}

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
Expand All @@ -75,19 +114,33 @@ class IniFiles
OPCODE_READ_PARAM_STRING(section);
OPCODE_READ_PARAM_STRING(key);

auto value = 0.0f;
char strValue[32];
auto result = GetPrivateProfileString(section, key, NULL, strValue, sizeof(strValue), path);
if (result)
{
value = (float)atof(strValue);
OPCODE_WRITE_PARAM_FLOAT(value);
}
else
char buff[32];
if (GetPrivateProfileString(section, key, NULL, buff, sizeof(buff), path))
{
OPCODE_SKIP_PARAMS(1);
char *str, *end;
float value;
if (StringStartsWith(buff, "0x", false)) // hex int
{
str = buff + 2;
value = (float)strtol(str, &end, 16);
}
else // float
{
str = buff;
value = strtof(str, &end);
}

if (end != str) // at least one number character consumed
{
OPCODE_WRITE_PARAM_FLOAT(value);
OPCODE_CONDITION_RESULT(true);
return OR_CONTINUE;
}
}
OPCODE_CONDITION_RESULT(result);

// failed
OPCODE_SKIP_PARAMS(1);
OPCODE_CONDITION_RESULT(false);
return OR_CONTINUE;
}

Expand All @@ -105,6 +158,11 @@ class IniFiles
char strValue[32];
sprintf(strValue, "%g", value);
auto result = WritePrivateProfileString(section, key, strValue, path);

if (GetLastError() == ERROR_FILE_NOT_FOUND) // path points directory
{
result = false;
}

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
Expand Down Expand Up @@ -147,6 +205,11 @@ class IniFiles

auto result = WritePrivateProfileString(section, key, strValue, path);

if (GetLastError() == ERROR_FILE_NOT_FOUND) // path points directory
{
result = false;
}

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
}
Expand Down
73 changes: 63 additions & 10 deletions tests/cleo_tests/IniFiles/0AF0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,98 @@ function tests
it("should fail on not-existing file", test1)
it("should fail on invalid file", test2)
it("should fail on not existing value", test3)
it("should fail on invalid type", test4)
it("should read value", test5)
it("should fail on invalid data", test4)
it("should read int from int data", test5)
it("should read int from negative int data", test6)
it("should read int from hex int data", test7)
it("should read int from float data", test8)
it("should read int from negative float data", test9)
it("should read int from mixed data", test10)

return

:setup
delete_file {path} Test_Path
write_int_to_ini_file {value} 42 {path} Test_Path {section} "test" {key} "test_int"
write_float_to_ini_file {value} 50.0 {path} Test_Path {section} "test" {key} "test_float"
write_string_to_ini_file {value} "value_one" {path} Test_Path {section} "test" {key} "test_string"
write_int_to_ini_file {value} 42 {path} Test_Path {section} "test" {key} "test_int"
write_int_to_ini_file {value} -42 {path} Test_Path {section} "test" {key} "test_int_neg"
write_string_to_ini_file {value} "0x42" {path} Test_Path {section} "test" {key} "test_int_hex"
write_float_to_ini_file {value} 50.0 {path} Test_Path {section} "test" {key} "test_float"
write_float_to_ini_file {value} -50.0 {path} Test_Path {section} "test" {key} "test_float_neg"
write_string_to_ini_file {value} "value_one" {path} Test_Path {section} "test" {key} "test_string"
write_string_to_ini_file {value} " 12.3four " {path} Test_Path {section} "test" {key} "test_mixed"
return

:cleanup
delete_file {path} Test_Path
return

function test1
int value = read_int_from_ini_file {path} "not_a_file.ini" {section} "test" {key} "test_int"
int value = 555
value = read_int_from_ini_file {path} "not_a_file.ini" {section} "test" {key} "test_float"
assert_result_false()
assert_eq(value, 555)
end

function test2
int value = read_int_from_ini_file {path} "cleo.asi" {section} "test" {key} "test_int"
int value = 555
value = read_int_from_ini_file {path} "cleo.asi" {section} "test" {key} "test_int"
assert_result_false()
assert_eq(value, 555)
end

function test3
int value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "invalid_key"
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "invalid_key"
assert_result_false()
assert_eq(value, 555)
end

function test4
int value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_string"
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_string"
assert_result_false()
assert_eq(value, 555)
end

function test5
int value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_int"
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_int"
assert_result_true()
assert_eq(value, 42)
end

function test6
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_int_neg"
assert_result_true()
assert_eq(value, -42)
end

function test7
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_int_hex"
assert_result_true()
assert_eq(value, 0x42)
end

function test8
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_float"
assert_result_true()
assert_eq(value, 50)
end

function test9
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_float_neg"
assert_result_true()
assert_eq(value, -50)
end

function test10
int value = 555
value = read_int_from_ini_file {path} Test_Path {section} "test" {key} "test_mixed"
assert_result_true()
assert_eq(value, 12)
end
end
2 changes: 1 addition & 1 deletion tests/cleo_tests/IniFiles/0AF1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function tests
it("should fail to overwrite file", test1)
it("should fail to overwrite directory", test2)
it("should create new file", test3)
it("should append to existing file", test4)
it("should add to existing file", test4)
it("should overwrite value", test5)
return

Expand Down
75 changes: 64 additions & 11 deletions tests/cleo_tests/IniFiles/0AF2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,98 @@ function tests
it("should fail on not-existing file", test1)
it("should fail on invalid file", test2)
it("should fail on not existing value", test3)
it("should fail on invalid type", test4)
it("should read value", test5)
it("should fail on invalid data", test4)
it("should read float from int data", test5)
it("should read float from negative int data", test6)
it("should read float from hex int data", test7)
it("should read float from float data", test8)
it("should read float from negative float data", test9)
it("should read float from mixed data", test10)

return

:setup
delete_file {path} Test_Path
write_int_to_ini_file {value} 42 {path} Test_Path {section} "test" {key} "test_int"
write_float_to_ini_file {value} 50.0 {path} Test_Path {section} "test" {key} "test_float"
write_string_to_ini_file {value} "value_one" {path} Test_Path {section} "test" {key} "test_string"
write_int_to_ini_file {value} 42 {path} Test_Path {section} "test" {key} "test_int"
write_int_to_ini_file {value} -42 {path} Test_Path {section} "test" {key} "test_int_neg"
write_string_to_ini_file {value} "0x42" {path} Test_Path {section} "test" {key} "test_int_hex"
write_float_to_ini_file {value} 50.0 {path} Test_Path {section} "test" {key} "test_float"
write_float_to_ini_file {value} -50.0 {path} Test_Path {section} "test" {key} "test_float_neg"
write_string_to_ini_file {value} "value_one" {path} Test_Path {section} "test" {key} "test_string"
write_string_to_ini_file {value} " 12.3four " {path} Test_Path {section} "test" {key} "test_mixed"
return

:cleanup
delete_file {path} Test_Path
return

function test1
float value = read_float_from_ini_file {path} "not_a_file.ini" {section} "test" {key} "test_float"
float value = 555.0
value = read_float_from_ini_file {path} "not_a_file.ini" {section} "test" {key} "test_float"
assert_result_false()
assert_eq(value, 555.0)
end

function test2
float value = read_float_from_ini_file {path} "cleo.asi" {section} "test" {key} "test_float"
float value = 555.0
value = read_float_from_ini_file {path} "cleo.asi" {section} "test" {key} "test_int"
assert_result_false()
assert_eq(value, 555.0)
end

function test3
float value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "invalid_key"
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "invalid_key"
assert_result_false()
assert_eq(value, 555.0)
end

function test4
float value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_string"
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_string"
assert_result_false()
assert_eq(value, 555.0)
end

function test5
float value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_float"
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_int"
assert_result_true()
assert_eqf(value, 50.0)
assert_eq(value, 42.0)
end

function test6
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_int_neg"
assert_result_true()
assert_eq(value, -42.0)
end

function test7
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_int_hex"
assert_result_true()
assert_eq(value, 66.0) // 0x42
end

function test8
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_float"
assert_result_true()
assert_eq(value, 50.0)
end

function test9
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_float_neg"
assert_result_true()
assert_eq(value, -50.0)
end

function test10
float value = 555.0
value = read_float_from_ini_file {path} Test_Path {section} "test" {key} "test_mixed"
assert_result_true()
assert_eq(value, 12.3)
end
end
2 changes: 1 addition & 1 deletion tests/cleo_tests/IniFiles/0AF3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function tests
it("should fail to overwrite file", test1)
it("should fail to overwrite directory", test2)
it("should create new file", test3)
it("should append to existing file", test4)
it("should add to existing file", test4)
it("should overwrite value", test5)
return

Expand Down
Loading

0 comments on commit 42c60f7

Please sign in to comment.