diff --git a/.clang-format b/.clang-format index 8c75585..770a6eb 100644 --- a/.clang-format +++ b/.clang-format @@ -1,71 +1,161 @@ --- Language: Cpp -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -#AlignArrayOfStructures: Right -AlignConsecutiveMacros: AcrossEmptyLinesAndComments -AlignEscapedNewlinesLeft: false +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: DontAlign +AlignOperands: DontAlign AlignTrailingComments: false -AllowShortFunctionsOnASingleLine: false -AllowShortIfStatementsOnASingleLine: false +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortLambdasOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never AllowShortLoopsOnASingleLine: false -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: false -BinPackArguments: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: + - __capability +BinPackArguments: false BinPackParameters: false -BitFieldColonSpacing: Both -BreakBeforeBinaryOperators: false +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeConceptDeclarations: true BreakBeforeBraces: Attach -BreakBeforeTernaryOperators: false -BreakConstructorInitializers: AfterColon -#BreakInheritanceList: AfterComma -ColumnLimit: 0 -CommentPragmas: '^ IWYU pragma:' -ConstructorInitializerAllOnOneLineOrOnePerLine: true +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 150 +CommentPragmas: "^ IWYU pragma:" +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 +ContinuationIndentWidth: 8 Cpp11BracedListStyle: true +DeriveLineEnding: true DerivePointerAlignment: false -DerivePointerBinding: false +DisableFormat: false +EmptyLineBeforeAccessModifier: LogicalBlock ExperimentalAutoDetectBinPacking: false -IndentCaseBlocks: true -IndentCaseLabels: true -IndentGotoLabels: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +StatementAttributeLikeMacros: + - Q_EMIT +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: ".*" + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: "(Test)?$" +IncludeIsMainSourceRegex: "" +IndentCaseLabels: True +IndentCaseBlocks: False +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false IndentWidth: 4 -MaxEmptyLinesToKeep: 2 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: "" +MacroBlockEnd: "" +MaxEmptyLinesToKeep: 1 NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 PointerAlignment: Left -#ReferenceAlignment: Pointer -#SortIncludes: CaseInsensitive +QualifierAlignment: Left +ReflowComments: true +SortIncludes: true +SortJavaStaticImport: Before SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: true -SpaceAroundPointerQualifiers: Default +SpaceAfterTemplateKeyword: false SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false -SpaceBeforeCpp11BracedList: true +SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true -SpaceBeforeParens: Never -SpaceBeforeRangeBasedForLoopColon: false -SpaceBeforeSquareBrackets: false +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true SpaceInEmptyBlock: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 -#SpacesInAngles: Never +SpacesInAngles: false SpacesInConditionalStatement: false -SpacesInContainerLiterals: false +SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both Standard: Latest +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION TabWidth: 8 UseCRLF: false UseTab: Never -... +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME diff --git a/include/tmj.h b/include/tmj.h index 0d15f71..a2c4e89 100644 --- a/include/tmj.h +++ b/include/tmj.h @@ -26,7 +26,7 @@ typedef struct Property { char* propertytype; char* type; - union{ + union { char* value_string; int value_int; double value_float; @@ -49,7 +49,7 @@ typedef struct Chunk { bool data_is_str; size_t data_count; - union{ + union { char* data_str; unsigned int* data_uint; }; @@ -58,7 +58,7 @@ typedef struct Chunk { /** * https://doc.mapeditor.org/en/stable/reference/json-map-format/#point */ -typedef struct Point{ +typedef struct Point { double x; double y; } Point; @@ -66,7 +66,7 @@ typedef struct Point{ /** * https://doc.mapeditor.org/en/stable/reference/json-map-format/#text */ -typedef struct Text{ +typedef struct Text { bool bold; bool italic; bool kerning; @@ -106,11 +106,11 @@ typedef struct Object { bool is_polygon; - union{ + union { size_t polygon_point_count; size_t polyline_point_count; }; - union{ + union { Point* polygon; Point* polyline; }; @@ -142,16 +142,16 @@ typedef struct Layer { bool data_is_str; size_t data_count; - union{ + union { unsigned int* data_uint; char* data_str; - }; //tilelayer only + }; // tilelayer only int height; // tilelayer only int id; int startx; int starty; - int width; //tilelayer only + int width; // tilelayer only int x; int y; @@ -404,7 +404,6 @@ typedef struct ObjectTemplate { Object* object; } ObjectTemplate; - /** * @defgroup tmj TMJ * @@ -494,7 +493,7 @@ void tmj_tileset_free(Tileset* tileset); /** * @ingroup tmj */ -typedef enum TMJ_LOG_PRIORITY{ +typedef enum TMJ_LOG_PRIORITY { TMJ_LOG_DEBUG, TMJ_LOG_INFO, TMJ_LOG_WARNING, @@ -513,7 +512,6 @@ typedef enum TMJ_LOG_PRIORITY{ */ void tmj_log_regcb(bool debug, void (*callback)(tmj_log_priority, const char*)); - ///** // * @defgroup util Util // * @@ -569,7 +567,7 @@ extern const char* const TMJ_VERSION; * https://doc.mapeditor.org/en/stable/reference/global-tile-ids/ for more * information. On failure, returns NULL. */ -uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size); +uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size); #ifdef __cplusplus } diff --git a/src/decode.c b/src/decode.c index e0bf98c..bb9e5fd 100644 --- a/src/decode.c +++ b/src/decode.c @@ -7,10 +7,10 @@ #ifdef LIBTMJ_ZSTD -uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* decompressed_size){ +uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* decompressed_size) { logmsg(TMJ_LOG_DEBUG, "Decode (zstd): Decompressing buffer of size %zu", data_size); - if(data == NULL){ + if (data == NULL) { logmsg(TMJ_LOG_ERR, "Decode (zstd): Cannot decompress NULL buffer"); return NULL; @@ -18,13 +18,13 @@ uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* deco size_t ret_size = ZSTD_getFrameContentSize(data, data_size); - if(ret_size == ZSTD_CONTENTSIZE_ERROR){ + if (ret_size == ZSTD_CONTENTSIZE_ERROR) { logmsg(TMJ_LOG_ERR, "Decode (zstd): Unable to decompress non-zstd buffer"); return NULL; } - if(ret_size == ZSTD_CONTENTSIZE_UNKNOWN){ + if (ret_size == ZSTD_CONTENTSIZE_UNKNOWN) { logmsg(TMJ_LOG_ERR, "Decode (zstd): Unable to determine uncompressed size of compressed data"); return NULL; @@ -32,7 +32,7 @@ uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* deco void* ret = malloc(ret_size); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Decode (zstd): Unable to allocate buffer for decompressed data, the system is out of memory"); return NULL; @@ -42,7 +42,7 @@ uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* deco logmsg(TMJ_LOG_DEBUG, "Decode (zstd): Decompressed byte total: %zu", dsize); - if(ZSTD_isError(dsize)){ + if (ZSTD_isError(dsize)) { logmsg(TMJ_LOG_ERR, "Decode (zstd): Decompression error: %s", ZSTD_getErrorName(dsize)); free(ret); @@ -59,10 +59,10 @@ uint8_t* tmj_zstd_decompress(const uint8_t* data, size_t data_size, size_t* deco #ifdef LIBTMJ_ZLIB -uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* decompressed_size){ +uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* decompressed_size) { logmsg(TMJ_LOG_DEBUG, "Decode (zlib): Decompressing buffer of size %zu", data_size); - if(data == NULL){ + if (data == NULL) { logmsg(TMJ_LOG_ERR, "Decode (zlib): Cannot decompress NULL buffer"); return NULL; @@ -72,7 +72,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco uint8_t* out = malloc(INFLATE_BLOCK_SIZE); - if(out == NULL){ + if (out == NULL) { logmsg(TMJ_LOG_ERR, "Decode (zlib): Unable to allocate buffer for decompressed data, the system is out of memory"); return NULL; @@ -93,7 +93,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco // 15 + 32 for zlib and gzip decoding with automatic header detection, according to the manual int ret = inflateInit2(&stream, 15 + 32); - switch(ret){ + switch (ret) { case Z_OK: logmsg(TMJ_LOG_DEBUG, "Decode (zlib): inflate initialization OK"); break; @@ -109,7 +109,9 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco goto fail_zlib; case Z_STREAM_ERROR: - logmsg(TMJ_LOG_ERR, "Decode (zlib): Unable to initialize inflate, invalid parameter(s) to inflate initialization routine"); + logmsg(TMJ_LOG_ERR, + "Decode (zlib): Unable to initialize inflate, invalid parameter(s) to inflate initialization " + "routine"); goto fail_zlib; @@ -127,11 +129,11 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco // Unsure if this is actually necessary, or if we can rely on Z_BUF_ERROR to tell us when to resize int stat = inflate(&stream, Z_NO_FLUSH); - while(stat != Z_STREAM_END){ - switch(stat){ + while (stat != Z_STREAM_END) { + switch (stat) { case Z_BUF_ERROR: // If we hit Z_BUF_ERROR with buffer space left, something's fucked - if(stream.avail_out != 0){ + if (stream.avail_out != 0) { logmsg(TMJ_LOG_ERR, "Decode (zlib): No progress possible"); return NULL; @@ -142,7 +144,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco logmsg(TMJ_LOG_DEBUG, "Decode (zlib): inflate OK"); out = realloc(out, INFLATE_BLOCK_SIZE * realloc_scale); - if(out == NULL){ + if (out == NULL) { logmsg(TMJ_LOG_ERR, "Decode (zlib): Unable to grow inflate output buffer, the system is out memory"); return NULL; @@ -185,7 +187,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco logmsg(TMJ_LOG_DEBUG, "Decode (zlib): Completed inflate, %zd bytes written to output buffer", stream.total_out); - if(inflateEnd(&stream) != Z_OK){ + if (inflateEnd(&stream) != Z_OK) { logmsg(TMJ_LOG_ERR, "Decode (zlib): Completed inflate, but could not clean up; stream state was inconsistent"); goto fail_zlib; @@ -198,7 +200,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco fail_zlib: free(out); - if(stream.msg){ + if (stream.msg) { logmsg(TMJ_LOG_ERR, "Decode (zlib): zlib error: '%s'", stream.msg); } @@ -209,27 +211,28 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco // Thanks to John's article for explaining Base64: https://nachtimwald.com/2017/11/18/base64-encode-and-decode-in-c/ +// clang-format off const char b64_encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const char b64_decode_table[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, - 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, - 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255 -}; +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, +255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, +15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, +27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, +47, 48, 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +255, 255, 255, 255, 255, 255, 255, 255, 255, }; +// clang-format on // Not used, but included here for reference, to explain how the decode table was generated -//void b64_generate_decode_table(){ +// void b64_generate_decode_table(){ // memset(&b64_decode_table, -1, 256); // // for(size_t i = 0; i < 256; i++){ @@ -240,8 +243,8 @@ const char b64_decode_table[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, /** * Calculates the size of the data decoded from the given base64 string. */ -size_t b64_decode_size(const char* data){ - if(data == NULL){ +size_t b64_decode_size(const char* data) { + if (data == NULL) { return 0; } @@ -250,10 +253,10 @@ size_t b64_decode_size(const char* data){ size_t ret = len / 4 * 3; // Check to see if the last 2 characters are padding bytes - if(data[len-1] == '='){ + if (data[len - 1] == '=') { ret--; - if(data[len-2] == '='){ + if (data[len - 2] == '=') { ret--; } } @@ -261,12 +264,12 @@ size_t b64_decode_size(const char* data){ return ret; } -bool b64_is_valid_char(char c){ - if(isalnum(c)){ +bool b64_is_valid_char(char c) { + if (isalnum(c)) { return true; } - if(c == '+' || c == '/' || c == '='){ + if (c == '+' || c == '/' || c == '=') { return true; } @@ -277,12 +280,12 @@ bool b64_is_valid_char(char c){ /** * Unimplemented, but may be useful to implement in the future. */ -char* tmj_b64_encode(uint8_t* data){ +char* tmj_b64_encode(uint8_t* data) { return NULL; } -uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size){ - if(data == NULL){ +uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size) { + if (data == NULL) { logmsg(TMJ_LOG_ERR, "Decode (b64): Unable to decode null input"); return NULL; @@ -290,7 +293,7 @@ uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size){ size_t len = strlen(data); - if(len % 4 != 0){ + if (len % 4 != 0) { logmsg(TMJ_LOG_ERR, "Decode (b64): Invalid Base64 string, input length is not a multiple of 4"); return NULL; @@ -299,15 +302,15 @@ uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size){ size_t dSize = b64_decode_size(data); uint8_t* out = malloc(dSize); - if(out == NULL){ + if (out == NULL) { logmsg(TMJ_LOG_ERR, "Decode (b64): Unable to allocate output buffer, the system is out of memory"); return NULL; } // Validate the input - for(size_t i = 0; i < len; i++){ - if(!b64_is_valid_char(data[i])){ + for (size_t i = 0; i < len; i++) { + if (!b64_is_valid_char(data[i])) { logmsg(TMJ_LOG_ERR, "Decode (b64): Invalid Base64 character, '%c'", data[i]); free(out); @@ -316,20 +319,20 @@ uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size){ } } - for(size_t i = 0, j = 0; i < len; i+=4, j+=3){ + for (size_t i = 0, j = 0; i < len; i += 4, j += 3) { // Pack decoded 6-bit values into an integer uint32_t p = b64_decode_table[data[i]]; - p = (p << 6) | b64_decode_table[data[i+1]]; - p = data[i+2] == '=' ? p << 6 : (p << 6) | b64_decode_table[data[i+2]]; - p = data[i+3] == '=' ? p << 6 : (p << 6) | b64_decode_table[data[i+3]]; + p = (p << 6) | b64_decode_table[data[i + 1]]; + p = data[i + 2] == '=' ? p << 6 : (p << 6) | b64_decode_table[data[i + 2]]; + p = data[i + 3] == '=' ? p << 6 : (p << 6) | b64_decode_table[data[i + 3]]; // Reinterpret into 8-bit bytes out[j] = (p >> 16) & 0xFF; - if(data[i+2] != '='){ - out[j+1] = (p >> 8) & 0xFF; + if (data[i + 2] != '=') { + out[j + 1] = (p >> 8) & 0xFF; } - if(data[i+3] != '='){ - out[j+2] = p & 0xFF; + if (data[i + 3] != '=') { + out[j + 2] = p & 0xFF; } } diff --git a/src/decode.h b/src/decode.h index dabc0a1..ddcc649 100644 --- a/src/decode.h +++ b/src/decode.h @@ -64,5 +64,4 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco */ uint8_t* tmj_b64_decode(const char* data, size_t* decoded_size); - #endif diff --git a/src/log.c b/src/log.c index a666ffe..6bc6751 100644 --- a/src/log.c +++ b/src/log.c @@ -15,19 +15,19 @@ void (*log_callback)(tmj_log_priority, const char*) = NULL; char logmsg_buf[LOGMSG_BUFSIZE]; -void tmj_log_regcb(bool debug, void (*callback)(tmj_log_priority, const char*)){ +void tmj_log_regcb(bool debug, void (*callback)(tmj_log_priority, const char*)) { log_debug = debug; log_callback = callback; } -void logmsg(tmj_log_priority priority, char* msg, ...){ +void logmsg(tmj_log_priority priority, char* msg, ...) { // Don't bother logging if there's no callback registered - if(log_callback == NULL){ + if (log_callback == NULL) { return; } // Don't log debug messages if we have debugging turned off - if(priority == TMJ_LOG_DEBUG && !log_debug){ + if (priority == TMJ_LOG_DEBUG && !log_debug) { return; } diff --git a/src/log.h b/src/log.h index f7c6f44..da193be 100644 --- a/src/log.h +++ b/src/log.h @@ -46,8 +46,7 @@ void logmsg(tmj_log_priority priority, char* msg, ...); // * @param msg A printf-style format string for the message to be logged // * @param ... Format string arguments for the previous argument. See printf() for detail. // */ -//void log_event(int loglevel, int line, char* file, char* func, char* msg, ...); - +// void log_event(int loglevel, int line, char* file, char* func, char* msg, ...); ///** // * @ingroup logging @@ -57,6 +56,6 @@ void logmsg(tmj_log_priority priority, char* msg, ...); // * The expected arguments are identical to that of log_event(). See the // * documentation for log_event() for detail. // */ -//#define logmsg(loglevel, ...) log_event(loglevel, __LINE__, __FILE__, __func__, msg, __VA_ARGS__); +// #define logmsg(loglevel, ...) log_event(loglevel, __LINE__, __FILE__, __func__, msg, __VA_ARGS__); #endif diff --git a/src/map.c b/src/map.c index 31cb57e..8e2c2d3 100644 --- a/src/map.c +++ b/src/map.c @@ -3,19 +3,19 @@ #include #include "log.h" -#include "tmj.h" #include "tileset.h" +#include "tmj.h" /** * @file */ -Property* unpack_properties(json_t* properties){ - if(properties == NULL){ +Property* unpack_properties(json_t* properties) { + if (properties == NULL) { return NULL; } - if(!json_is_array(properties)){ + if (!json_is_array(properties)) { logmsg(TMJ_LOG_ERR, "'properties' must be an array"); return NULL; @@ -27,7 +27,7 @@ Property* unpack_properties(json_t* properties){ Property* ret = calloc(property_count, sizeof(Property)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack properties, the system is out of memory"); return NULL; @@ -38,18 +38,21 @@ Property* unpack_properties(json_t* properties){ size_t idx = 0; json_t* property = NULL; - json_array_foreach(properties, idx, property){ + json_array_foreach(properties, idx, property) { int unpk = json_unpack_ex(property, - &error, - 0, - "{s:s, s?s, s?s, s:o}", - "name", &ret[idx].name, - "type", &ret[idx].type, - "propertytype", &ret[idx].propertytype, - "value", &value - ); - - if(unpk == -1){ + &error, + 0, + "{s:s, s?s, s?s, s:o}", + "name", + &ret[idx].name, + "type", + &ret[idx].type, + "propertytype", + &ret[idx].propertytype, + "value", + &value); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack properties, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -58,10 +61,10 @@ Property* unpack_properties(json_t* properties){ } // note: string is default type, so missing field means string - if(ret[idx].type == NULL || strcmp(ret[idx].type, "string") == 0){ + if (ret[idx].type == NULL || strcmp(ret[idx].type, "string") == 0) { unpk = json_unpack_ex(value, &error, 0, "s", &ret[idx].value_string); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack string value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -70,10 +73,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "int") == 0){ + if (strcmp(ret[idx].type, "int") == 0) { unpk = json_unpack_ex(value, &error, 0, "i", &ret[idx].value_int); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack integer value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -82,10 +85,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "float") == 0){ + if (strcmp(ret[idx].type, "float") == 0) { unpk = json_unpack_ex(value, &error, 0, "i", &ret[idx].value_float); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack float value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -94,10 +97,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "bool") == 0){ + if (strcmp(ret[idx].type, "bool") == 0) { unpk = json_unpack_ex(value, &error, 0, "b", &ret[idx].value_bool); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack bool value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -106,10 +109,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "color") == 0){ + if (strcmp(ret[idx].type, "color") == 0) { unpk = json_unpack_ex(value, &error, 0, "s", &ret[idx].value_color); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack color value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -118,10 +121,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "file") == 0){ + if (strcmp(ret[idx].type, "file") == 0) { unpk = json_unpack_ex(value, &error, 0, "s", &ret[idx].value_file); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack file value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -130,10 +133,10 @@ Property* unpack_properties(json_t* properties){ } } - if(strcmp(ret[idx].type, "object") == 0){ + if (strcmp(ret[idx].type, "object") == 0) { unpk = json_unpack_ex(value, &error, 0, "i", &ret[idx].value_object); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack object value from property, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -149,13 +152,13 @@ Property* unpack_properties(json_t* properties){ /** * Unpacks an array of points. The returned array must be freed by the caller. */ -Point* unpack_points(json_t* points){ - if(points == NULL){ +Point* unpack_points(json_t* points) { + if (points == NULL) { return NULL; } - if(!json_is_array(points)){ - logmsg(TMJ_LOG_ERR, "'points' must be an array"); + if (!json_is_array(points)) { + logmsg(TMJ_LOG_ERR, "'polygon' or 'polyline' must be an array"); return NULL; } @@ -166,7 +169,7 @@ Point* unpack_points(json_t* points){ Point* ret = calloc(point_count, sizeof(Point)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack points, the system is out of memory"); return NULL; @@ -175,16 +178,10 @@ Point* unpack_points(json_t* points){ size_t idx; json_t* point; - json_array_foreach(points, idx, point){ - int unpk = json_unpack_ex(point, - &error, - 0, - "{s:F, s:F}", - "x", &ret[idx].x, - "y", &ret[idx].y - ); + json_array_foreach(points, idx, point) { + int unpk = json_unpack_ex(point, &error, 0, "{s:F, s:F}", "x", &ret[idx].x, "y", &ret[idx].y); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack points, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -199,8 +196,8 @@ Point* unpack_points(json_t* points){ /** * Unpacks a text object. The returned object must be freed by the caller. */ -Text* unpack_text(json_t* text){ - if(text == NULL){ +Text* unpack_text(json_t* text) { + if (text == NULL) { return NULL; } @@ -208,34 +205,45 @@ Text* unpack_text(json_t* text){ Text* ret = calloc(1, sizeof(Text)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack text, the system is out of memory"); return NULL; } int unpk = json_unpack_ex(text, - &error, - 0, - "{" - "s?b, s?b, s?b, s?b, s?b, s?b," - "s?s, s?s?, s?s, s:s, s?s," - "s?i," - "}", - "bold", &ret->bold, - "italic", &ret->italic, - "kerning", &ret->kerning, - "strikeout", &ret->strikeout, - "underline", &ret->underline, - "wrap", &ret->wrap, - "color", &ret->color, - "fontfamily", &ret->fontfamily, - "halign", &ret->halign, - "text", &ret->text, - "valign", &ret->valign, - "pixelsize", &ret->pixelsize - ); - if(unpk == -1){ + &error, + 0, + "{" + "s?b, s?b, s?b, s?b, s?b, s?b," + "s?s, s?s?, s?s, s:s, s?s," + "s?i," + "}", + "bold", + &ret->bold, + "italic", + &ret->italic, + "kerning", + &ret->kerning, + "strikeout", + &ret->strikeout, + "underline", + &ret->underline, + "wrap", + &ret->wrap, + "color", + &ret->color, + "fontfamily", + &ret->fontfamily, + "halign", + &ret->halign, + "text", + &ret->text, + "valign", + &ret->valign, + "pixelsize", + &ret->pixelsize); + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack text, %s at line %d column %d", error.text, error.line, error.column); free(ret); @@ -246,12 +254,12 @@ Text* unpack_text(json_t* text){ return ret; } -Object* unpack_objects(json_t* objects){ - if(objects == NULL){ +Object* unpack_objects(json_t* objects) { + if (objects == NULL) { return NULL; } - if(!json_is_array(objects)){ + if (!json_is_array(objects)) { logmsg(TMJ_LOG_ERR, "'objects' must be an array"); } @@ -261,7 +269,7 @@ Object* unpack_objects(json_t* objects){ Object* ret = calloc(object_count, sizeof(Object)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack objects, the system is out of memory"); return NULL; @@ -270,51 +278,67 @@ Object* unpack_objects(json_t* objects){ size_t idx = 0; json_t* object = NULL; - json_array_foreach(objects, idx, object){ + json_array_foreach(objects, idx, object) { json_t* properties = NULL; json_t* text = NULL; json_t* polygon = NULL; json_t* polyline = NULL; - //Unpack scalar values + // Unpack scalar values int unpk = json_unpack_ex(object, - &error, - 0, - "{" - "s?b, s?b, s:b," - "s:s, s?s, s?s," - "s?i, s:i," - "s:F, s:F, s:F, s:F, s:F," - "s?o" - "}", - "ellipse", &ret[idx].ellipse, - "point", &ret[idx].point, - "visible", &ret[idx].visible, - "name", &ret[idx].name, - "template", &ret[idx].template, - "type", &ret[idx].type, - "gid", &ret[idx].gid, - "id", &ret[idx].id, - "height", &ret[idx].height, - "rotation", &ret[idx].rotation, - "width", &ret[idx].width, - "x", &ret[idx].x, - "y", &ret[idx].y, - "properties", &properties, - "text", &text, - "polygon", &polygon, - "polyline", &polyline - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s?b, s?b, s:b," + "s:s, s?s, s?s," + "s?i, s:i," + "s:F, s:F, s:F, s:F, s:F," + "s?o" + "}", + "ellipse", + &ret[idx].ellipse, + "point", + &ret[idx].point, + "visible", + &ret[idx].visible, + "name", + &ret[idx].name, + "template", + &ret[idx].template, + "type", + &ret[idx].type, + "gid", + &ret[idx].gid, + "id", + &ret[idx].id, + "height", + &ret[idx].height, + "rotation", + &ret[idx].rotation, + "width", + &ret[idx].width, + "x", + &ret[idx].x, + "y", + &ret[idx].y, + "properties", + &properties, + "text", + &text, + "polygon", + &polygon, + "polyline", + &polyline); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack object, %s at line %d column %d", error.text, error.line, error.column); return NULL; } - //Unpack properties - if(properties != NULL){ - if(!json_is_array(properties)){ + // Unpack properties + if (properties != NULL) { + if (!json_is_array(properties)) { logmsg(TMJ_LOG_ERR, "'properties' must be an array"); goto fail_properties; @@ -322,7 +346,7 @@ Object* unpack_objects(json_t* objects){ ret[idx].properties = unpack_properties(properties); - if(ret[idx].properties == NULL){ + if (ret[idx].properties == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack object properties"); goto fail_properties; @@ -332,10 +356,10 @@ Object* unpack_objects(json_t* objects){ } // Unpack text - if(text != NULL){ + if (text != NULL) { ret[idx].text = unpack_text(text); - if(ret[idx].text == NULL){ + if (ret[idx].text == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack object text"); goto fail_text; @@ -343,21 +367,15 @@ Object* unpack_objects(json_t* objects){ } // If this object is any of the below items, we don't need to unpack anything else - if(ret[idx].ellipse || ret[idx].point || ret[idx].gid != 0 || ret[idx].text != NULL){ + if (ret[idx].ellipse || ret[idx].point || ret[idx].gid != 0 || ret[idx].text != NULL) { continue; } // Unpack Polygon - if(polygon != NULL){ - if(!json_is_array(polygon)){ - logmsg(TMJ_LOG_ERR, "'polygon' must be an array"); - - goto fail_polygon; - } - + if (polygon != NULL) { ret[idx].polygon = unpack_points(polygon); - - if(ret[idx].polygon == NULL){ + + if (ret[idx].polygon == NULL) { goto fail_polygon; } @@ -368,16 +386,10 @@ Object* unpack_objects(json_t* objects){ } // Unpack Polyline - if(polyline != NULL){ - if(!json_is_array(polyline)){ - logmsg(TMJ_LOG_ERR, "'polyline' must be an array"); - - goto fail_polyline; - } - + if (polyline != NULL) { ret[idx].polyline = unpack_points(polyline); - if(ret[idx].polyline == NULL){ + if (ret[idx].polyline == NULL) { goto fail_polyline; } @@ -389,12 +401,12 @@ Object* unpack_objects(json_t* objects){ fail_polygon: fail_polyline: - for(size_t i = 0; i < object_count; i++){ + for (size_t i = 0; i < object_count; i++) { free(ret[i].text); } fail_text: - for(size_t i = 0; i < object_count; i++){ + for (size_t i = 0; i < object_count; i++) { free(ret[i].properties); } @@ -408,8 +420,8 @@ Object* unpack_objects(json_t* objects){ * Helper function to free Objects. May cause undefined behavior if the objects * were modified by the caller of map_load(). */ -void free_objects(Object* objects, size_t object_count){ - for(size_t i = 0; i < object_count; i++){ +void free_objects(Object* objects, size_t object_count) { + for (size_t i = 0; i < object_count; i++) { // We don't bother freeing polyline, because polygon and polyline are a union free(objects[i].polygon); free(objects[i].text); @@ -419,12 +431,12 @@ void free_objects(Object* objects, size_t object_count){ free(objects); } -Chunk* unpack_chunks(json_t* chunks){ - if(chunks == NULL){ +Chunk* unpack_chunks(json_t* chunks) { + if (chunks == NULL) { return NULL; } - if(!json_is_array(chunks)){ + if (!json_is_array(chunks)) { logmsg(TMJ_LOG_ERR, "Could not unpack layer chunks, 'chunks' must be an array"); return NULL; @@ -436,7 +448,7 @@ Chunk* unpack_chunks(json_t* chunks){ Chunk* ret = calloc(chunk_count, sizeof(Chunk)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack chunks, the system is out of memory"); return NULL; @@ -445,46 +457,49 @@ Chunk* unpack_chunks(json_t* chunks){ size_t idx; json_t* chunk; - json_array_foreach(chunks, idx, chunk){ + json_array_foreach(chunks, idx, chunk) { json_t* data = NULL; int unpk = json_unpack_ex(chunk, - &error, - 0, - "{" - "s:i, s:i, s:i, s:i," - "s:o," - "}", - "height", &ret[idx].height, - "width", &ret[idx].width, - "x", &ret[idx].x, - "y", &ret[idx].y, - "data", &data - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s:i, s:i, s:i, s:i," + "s:o," + "}", + "height", + &ret[idx].height, + "width", + &ret[idx].width, + "x", + &ret[idx].x, + "y", + &ret[idx].y, + "data", + &data); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack chunk, %s at line %d column %d", error.text, error.line, error.column); goto fail_chunk; } - if(json_is_string(data)){ + if (json_is_string(data)) { unpk = json_unpack_ex(data, &error, 0, "s", &ret[idx].data_str); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack chunk data, %s at line %d column %d", error.text, error.line, error.column); goto fail_chunk; } ret[idx].data_is_str = true; - } - else if(json_is_array(data)){ + } else if (json_is_array(data)) { size_t datum_count = json_array_size(data); ret[idx].data_uint = calloc(datum_count, sizeof(unsigned int)); - if(ret[idx].data_uint == NULL){ + if (ret[idx].data_uint == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack chunk data, the system is out memory"); goto fail_chunk; @@ -493,17 +508,16 @@ Chunk* unpack_chunks(json_t* chunks){ size_t idx2; json_t* datum; - json_array_foreach(data, idx2, datum){ + json_array_foreach(data, idx2, datum) { unpk = json_unpack_ex(datum, &error, 0, "i", &ret[idx].data_uint[idx2]); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack chunk datum, %s at line %d column %d", error.text, error.line, error.column); goto fail_data; } } - } - else{ + } else { logmsg(TMJ_LOG_ERR, "Unable to unpack chunk, chunk data must be a string or an array of uint"); goto fail_chunk; @@ -513,8 +527,8 @@ Chunk* unpack_chunks(json_t* chunks){ return ret; fail_data: - for(size_t i = 0; i < chunk_count; i++){ - if(!ret[i].data_is_str){ + for (size_t i = 0; i < chunk_count; i++) { + if (!ret[i].data_is_str) { free(ret[i].data_uint); } } @@ -526,9 +540,9 @@ Chunk* unpack_chunks(json_t* chunks){ } // Helper function for freeing chunks, since they contain dynamically-allocated arrays -void free_chunks(Chunk* chunks, size_t chunk_count){ - for(size_t i = 0; i < chunk_count; i++){ - if(!chunks[i].data_is_str){ +void free_chunks(Chunk* chunks, size_t chunk_count) { + for (size_t i = 0; i < chunk_count; i++) { + if (!chunks[i].data_is_str) { free(chunks[i].data_uint); } } @@ -539,8 +553,8 @@ void free_chunks(Chunk* chunks, size_t chunk_count){ /** * Loads map layers recursively */ -Layer* unpack_layers(json_t* layers){ - if(!json_is_array(layers)){ +Layer* unpack_layers(json_t* layers) { + if (!json_is_array(layers)) { logmsg(TMJ_LOG_ERR, "Could not unpack layer, 'layers' must be an array"); return NULL; @@ -548,7 +562,7 @@ Layer* unpack_layers(json_t* layers){ size_t layer_count = json_array_size(layers); - if(layer_count < 1){ + if (layer_count < 1) { logmsg(TMJ_LOG_ERR, "Unable to load layers, 'layers' array must have at least one element"); return NULL; @@ -556,7 +570,7 @@ Layer* unpack_layers(json_t* layers){ Layer* ret = calloc(layer_count, sizeof(Layer)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to load layers, the system is out of memory"); return NULL; @@ -566,11 +580,11 @@ Layer* unpack_layers(json_t* layers){ json_t* layer; json_error_t error; - json_array_foreach(layers, idx, layer){ + json_array_foreach(layers, idx, layer) { // Unpack id so we can log errors with it int unpk = json_unpack_ex(layer, &error, 0, "{s:i}", "id", &ret[idx].id); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer ID, %s at line %d column %d", error.text, error.line, error.column); } @@ -579,7 +593,7 @@ Layer* unpack_layers(json_t* layers){ // Unpack type unpk = json_unpack_ex(layer, &error, 0, "{s:s}", "type", &ret[idx].type); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; @@ -587,86 +601,98 @@ Layer* unpack_layers(json_t* layers){ // Unpack scalar values unpk = json_unpack_ex(layer, - &error, - 0, - "{" - "s?b, s:b," - "s?s, s:s, s?s," - "s?i, s?i, s:i, s:i," - "s?F, s?F, s:F, s?F, s?F" - "}", - "locked", &ret[idx].locked, - "visible", &ret[idx].visible, - "class", &ret[idx].class, - "name", &ret[idx].name, - "tintcolor", &ret[idx].tintcolor, - "startx", &ret[idx].startx, - "starty", &ret[idx].starty, - "x", &ret[idx].x, - "y", &ret[idx].y, - "offsetx", &ret[idx].offsetx, - "offsety", &ret[idx].offsety, - "opacity", &ret[idx].opacity, - "parallaxx", &ret[idx].parallaxx, - "parallaxy", &ret[idx].parallaxy - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s?b, s:b," + "s?s, s:s, s?s," + "s?i, s?i, s:i, s:i," + "s?F, s?F, s:F, s?F, s?F" + "}", + "locked", + &ret[idx].locked, + "visible", + &ret[idx].visible, + "class", + &ret[idx].class, + "name", + &ret[idx].name, + "tintcolor", + &ret[idx].tintcolor, + "startx", + &ret[idx].startx, + "starty", + &ret[idx].starty, + "x", + &ret[idx].x, + "y", + &ret[idx].y, + "offsetx", + &ret[idx].offsetx, + "offsety", + &ret[idx].offsety, + "opacity", + &ret[idx].opacity, + "parallaxx", + &ret[idx].parallaxx, + "parallaxy", + &ret[idx].parallaxy); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; } // Unpack conditional scalar values - if(strcmp(ret[idx].type, "imagelayer") == 0){ + if (strcmp(ret[idx].type, "imagelayer") == 0) { unpk = json_unpack_ex(layer, - &error, - 0, - "{" - "s:b, s:b," - "s:s, s?s" - "}", - "repeatx", &ret[idx].repeatx, - "repeaty", &ret[idx].repeaty, - "image", &ret[idx].image, - "transparentcolor", &ret[idx].transparentcolor - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s:b, s:b," + "s:s, s?s" + "}", + "repeatx", + &ret[idx].repeatx, + "repeaty", + &ret[idx].repeaty, + "image", + &ret[idx].image, + "transparentcolor", + &ret[idx].transparentcolor); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; } - } - else if(strcmp(ret[idx].type, "tilelayer") == 0){ + } else if (strcmp(ret[idx].type, "tilelayer") == 0) { unpk = json_unpack_ex(layer, - &error, - 0, - "{" - "s?s, s?s," - "s:i, s:i," - "}", - "compression", &ret[idx].compression, - "encoding", &ret[idx].encoding, - "height", &ret[idx].height, - "width", &ret[idx].width - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s?s, s?s," + "s:i, s:i," + "}", + "compression", + &ret[idx].compression, + "encoding", + &ret[idx].encoding, + "height", + &ret[idx].height, + "width", + &ret[idx].width); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; } - } - else if(strcmp(ret[idx].type, "objectgroup") == 0){ - unpk = json_unpack_ex(layer, - &error, - 0, - "{s?s}", - "draworder", &ret[idx].draworder - ); + } else if (strcmp(ret[idx].type, "objectgroup") == 0) { + unpk = json_unpack_ex(layer, &error, 0, "{s?s}", "draworder", &ret[idx].draworder); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; @@ -674,36 +700,35 @@ Layer* unpack_layers(json_t* layers){ } // Unpack data - if(strcmp(ret[idx].type, "tilelayer") == 0 ){ + if (strcmp(ret[idx].type, "tilelayer") == 0) { json_t* data = NULL; unpk = json_unpack_ex(layer, &error, 0, "{s:o}", "data", &data); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; } - if(json_is_string(data)){ + if (json_is_string(data)) { ret[idx].data_is_str = true; unpk = json_unpack_ex(layer, &error, 0, "{s:s}", "data", &ret[idx].data_str); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack layer[%d], %s at line %d column %d", ret[idx].id, error.text, error.line, error.column); goto fail_layer; } - } - else if(json_is_array(data)){ + } else if (json_is_array(data)) { ret[idx].data_is_str = false; ret[idx].data_count = json_array_size(data); ret[idx].data_uint = calloc(ret[idx].data_count, sizeof(unsigned int)); - if(ret[idx].data_uint == NULL){ + if (ret[idx].data_uint == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->data, the system is out of memory", ret[idx].id); goto fail_layer; @@ -713,15 +738,14 @@ Layer* unpack_layers(json_t* layers){ size_t idx2; - json_array_foreach(data, idx2, datum){ + json_array_foreach(data, idx2, datum) { unpk = json_unpack_ex(datum, &error, 0, "i", &ret[idx].data_uint[idx2]); - if(unpk == -1){ + if (unpk == -1) { goto fail_data; } } - } - else{ + } else { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->data, data must be a string or an array", ret[idx].id); goto fail_layer; @@ -733,14 +757,14 @@ Layer* unpack_layers(json_t* layers){ unpk = json_unpack_ex(layer, &error, 0, "{s?o}", "properties", &properties); - if(unpk == -1){ + if (unpk == -1) { goto fail_data; } - if(properties != NULL){ + if (properties != NULL) { ret[idx].properties = unpack_properties(properties); - if(ret[idx].properties == NULL){ + if (ret[idx].properties == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->properties", ret[idx].id); goto fail_data; @@ -748,19 +772,19 @@ Layer* unpack_layers(json_t* layers){ } // Unpack chunks - if(strcmp(ret[idx].type, "tilelayer") == 0 ){ + if (strcmp(ret[idx].type, "tilelayer") == 0) { json_t* chunks = NULL; unpk = json_unpack_ex(layer, &error, 0, "{s?o}", "chunks", &chunks); - if(unpk == -1){ + if (unpk == -1) { goto fail_properties; } - if(chunks != NULL){ + if (chunks != NULL) { ret[idx].chunks = unpack_chunks(chunks); - if(ret[idx].chunks == NULL){ + if (ret[idx].chunks == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->chunks", ret[idx].id); goto fail_properties; @@ -769,19 +793,19 @@ Layer* unpack_layers(json_t* layers){ } // Unpack objects - if(strcmp(ret[idx].type, "objectgroup") == 0 ){ + if (strcmp(ret[idx].type, "objectgroup") == 0) { json_t* objects = NULL; unpk = json_unpack_ex(layer, &error, 0, "{s:o}", "objects", &objects); - if(unpk == -1){ + if (unpk == -1) { goto fail_chunks; } - if(objects != NULL){ + if (objects != NULL) { ret[idx].objects = unpack_objects(objects); - if(ret[idx].objects == NULL){ + if (ret[idx].objects == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->objects", ret[idx].id); goto fail_chunks; @@ -792,19 +816,19 @@ Layer* unpack_layers(json_t* layers){ } // Unpack nested layers - if(strcmp(ret[idx].type, "group") == 0 ){ + if (strcmp(ret[idx].type, "group") == 0) { json_t* nested_layers = NULL; unpk = json_unpack_ex(layer, &error, 0, "{s:o}", "layers", &nested_layers); - if(unpk == -1){ + if (unpk == -1) { goto fail_objects; } - if(json_is_array(nested_layers) && json_array_size(nested_layers) > 0){ + if (json_is_array(nested_layers) && json_array_size(nested_layers) > 0) { ret[idx].layers = unpack_layers(nested_layers); - if(ret[idx].layers == NULL){ + if (ret[idx].layers == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack layer[%d]->layers", ret[idx].id); goto fail_objects; @@ -812,32 +836,32 @@ Layer* unpack_layers(json_t* layers){ } } } - + return ret; fail_objects: - for(size_t i = 0; i < layer_count; i++){ + for (size_t i = 0; i < layer_count; i++) { free_objects(ret[i].objects, ret[i].object_count); } fail_chunks: - for(size_t i = 0; i < layer_count; i++){ + for (size_t i = 0; i < layer_count; i++) { free_chunks(ret[i].chunks, ret[i].chunk_count); } fail_properties: - for(size_t i = 0; i < layer_count; i++){ + for (size_t i = 0; i < layer_count; i++) { free(ret[i].properties); } fail_data: - for(size_t i = 0; i < layer_count; i++){ - if(!ret[i].data_is_str){ + for (size_t i = 0; i < layer_count; i++) { + if (!ret[i].data_is_str) { free(ret[i].data_uint); } } fail_layer: - free(ret); + free(ret); return NULL; } @@ -847,8 +871,8 @@ Layer* unpack_layers(json_t* layers){ * undefined behavior if the layer objects were modified by the caller of * map_load(). */ -void layers_free(Layer* layers, size_t layer_count){ - for(size_t i = 0; i < layer_count; i++){ +void layers_free(Layer* layers, size_t layer_count) { + for (size_t i = 0; i < layer_count; i++) { free_objects(layers[i].objects, layers[i].object_count); free_chunks(layers[i].chunks, layers[i].chunk_count); free(layers[i].properties); @@ -860,12 +884,12 @@ void layers_free(Layer* layers, size_t layer_count){ free(layers); } -Map* map_load_json(json_t* root, const char* path){ +Map* map_load_json(json_t* root, const char* path) { json_error_t error; Map* map = calloc(1, sizeof(Map)); - if(map == NULL){ + if (map == NULL) { logmsg(TMJ_LOG_ERR, "Could not load map '%s', the system is out of memory", path); goto fail_map; @@ -876,13 +900,13 @@ Map* map_load_json(json_t* root, const char* path){ // Verify type (i.e, check that this is a map and not a tileset or something) int unpk = json_unpack_ex(root, &error, 0, "{s:s}", "type", &map->type); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->type, %s at line %d, column %d", path, error.text, error.line, error.column); goto fail_map; } - if(strcmp(map->type, "map") != 0){ + if (strcmp(map->type, "map") != 0) { logmsg(TMJ_LOG_ERR, "File at path '%s' is of type '%s' and is not a map file", path, map->type); goto fail_map; @@ -894,63 +918,75 @@ Map* map_load_json(json_t* root, const char* path){ // Unpack scalar values unpk = json_unpack_ex(root, - &error, - 0, - "{" - "s:b," - "s?s, s?s, s:s, s:s, s:s, s:s," - "s:i, s:i, s:i, s:i, s:i, s:i, s:i," - "s?F, s?F," - "s:o, s:o, s?o" - "}", - "infinite", &map->infinite, - "backgroundcolor", &map->backgroundcolor, - "class", &map->class, - "orientation", &map->orientation, - "renderorder", &map->renderorder, - "tiledversion", &map->tiledversion, - "version", &map->version, - "compressionlevel", &map->compressionlevel, - "height", &map->height, - "nextlayerid", &map->nextlayerid, - "nextobjectid", &map->nextobjectid, - "tileheight", &map->tileheight, - "tilewidth", &map->tilewidth, - "width", &map->width, - "parallaxoriginx", &map->parallaxoriginx, - "parallaxoriginy", &map->parallaxoriginy, - "tilesets", &tilesets, - "layers", &layers, - "properties", &properties - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s:b," + "s?s, s?s, s:s, s:s, s:s, s:s," + "s:i, s:i, s:i, s:i, s:i, s:i, s:i," + "s?F, s?F," + "s:o, s:o, s?o" + "}", + "infinite", + &map->infinite, + "backgroundcolor", + &map->backgroundcolor, + "class", + &map->class, + "orientation", + &map->orientation, + "renderorder", + &map->renderorder, + "tiledversion", + &map->tiledversion, + "version", + &map->version, + "compressionlevel", + &map->compressionlevel, + "height", + &map->height, + "nextlayerid", + &map->nextlayerid, + "nextobjectid", + &map->nextobjectid, + "tileheight", + &map->tileheight, + "tilewidth", + &map->tilewidth, + "width", + &map->width, + "parallaxoriginx", + &map->parallaxoriginx, + "parallaxoriginy", + &map->parallaxoriginy, + "tilesets", + &tilesets, + "layers", + &layers, + "properties", + &properties); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column); goto fail_map; } // Unpack conditional scalar values - if(strcmp(map->orientation, "staggered") == 0 || strcmp(map->orientation, "hexagonal") == 0){ - unpk = json_unpack_ex(root, - &error, - 0, - "{s:s, s:s}", - "staggeraxis", &map->staggeraxis, - "staggerindex", &map->staggerindex - ); - - if(unpk == -1){ + if (strcmp(map->orientation, "staggered") == 0 || strcmp(map->orientation, "hexagonal") == 0) { + unpk = json_unpack_ex(root, &error, 0, "{s:s, s:s}", "staggeraxis", &map->staggeraxis, "staggerindex", &map->staggerindex); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column); goto fail_map; } } - if(strcmp(map->orientation, "hexagonal") == 0){ + if (strcmp(map->orientation, "hexagonal") == 0) { unpk = json_unpack_ex(root, &error, 0, "{s:i}", "hexsidelength", &map->hexsidelength); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column); goto fail_map; @@ -958,10 +994,10 @@ Map* map_load_json(json_t* root, const char* path){ } // Unpack properties - if(properties != NULL){ + if (properties != NULL) { map->properties = unpack_properties(properties); - if(map->properties == NULL){ + if (map->properties == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->properties", path); goto fail_map; @@ -971,7 +1007,7 @@ Map* map_load_json(json_t* root, const char* path){ // Unpack layers map->layers = unpack_layers(layers); - if(map->layers == NULL){ + if (map->layers == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->layers", path); goto fail_properties; @@ -980,7 +1016,7 @@ Map* map_load_json(json_t* root, const char* path){ map->layer_count = json_array_size(layers); // Unpack tilesets - if(!json_is_array(tilesets)){ + if (!json_is_array(tilesets)) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->tilesets, tilesets must be an array of Tilesets", path); goto fail_layers; @@ -990,7 +1026,7 @@ Map* map_load_json(json_t* root, const char* path){ map->tilesets = calloc(tileset_count, sizeof(Tileset)); - if(map->tilesets == NULL){ + if (map->tilesets == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->tilesets, the system is out of memory", path); goto fail_layers; @@ -999,13 +1035,13 @@ Map* map_load_json(json_t* root, const char* path){ size_t idx; json_t* tileset = NULL; - json_array_foreach(tilesets, idx, tileset){ + json_array_foreach(tilesets, idx, tileset) { char* source = NULL; int firstgid = 0; unpk = json_unpack_ex(tileset, &error, 0, "{s?s, s?i}", "source", &source, "firstgid", &firstgid); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->tilesets, %s at line %d column %d", path, error.text, error.line, error.column); free(map->tilesets); @@ -1014,13 +1050,13 @@ Map* map_load_json(json_t* root, const char* path){ } // The tileset is not included in the map object, save the firstgid and source - if(source){ + if (source) { map->tilesets[idx].firstgid = firstgid; map->tilesets[idx].source = source; } // The tileset is embedded in the map, unpack it - else{ - if(unpack_tileset(tileset, &map->tilesets[idx]) != 0){ + else { + if (unpack_tileset(tileset, &map->tilesets[idx]) != 0) { logmsg(TMJ_LOG_ERR, "Unable to unpack map[%s]->tilesets, could not unpack embedded tileset", path); goto fail_tilesets; @@ -1049,17 +1085,17 @@ Map* map_load_json(json_t* root, const char* path){ return NULL; } -Map* tmj_map_loadf(const char* path, bool check_extension){ +Map* tmj_map_loadf(const char* path, bool check_extension) { char* ext = strrchr(path, '.'); - if(check_extension){ - if(ext == NULL){ + if (check_extension) { + if (ext == NULL) { logmsg(TMJ_LOG_ERR, "Map filename '%s' has no extension", path); return NULL; } - if(strcmp(ext, ".tmj") != 0 && strcmp(ext, ".json") != 0){ + if (strcmp(ext, ".tmj") != 0 && strcmp(ext, ".json") != 0) { logmsg(TMJ_LOG_ERR, "Map filename '%s' has unknown extension, '%s'", path, ext); logmsg(TMJ_LOG_ERR, "Map filename '%s' must have '.tmj' or '.json' extension to be loaded", path); @@ -1072,7 +1108,7 @@ Map* tmj_map_loadf(const char* path, bool check_extension){ json_error_t error; json_t* root = json_load_file(path, JSON_REJECT_DUPLICATES, &error); - if(root == NULL){ + if (root == NULL) { logmsg(TMJ_LOG_ERR, "Could not load map %s, %s at line %d column %d", path, error.text, error.line, error.column); return NULL; @@ -1081,12 +1117,12 @@ Map* tmj_map_loadf(const char* path, bool check_extension){ return map_load_json(root, path); } -Map* tmj_map_load(const char* map, const char* name){ +Map* tmj_map_load(const char* map, const char* name) { json_error_t error; json_t* root = json_loads(map, JSON_REJECT_DUPLICATES, &error); - if(root == NULL){ + if (root == NULL) { logmsg(TMJ_LOG_ERR, "Could not load map %s, %s at line %d column %d", name, error.text, error.line, error.column); return NULL; @@ -1095,8 +1131,8 @@ Map* tmj_map_load(const char* map, const char* name){ return map_load_json(root, name); } -void tmj_map_free(Map* map){ - if(!map){ +void tmj_map_free(Map* map) { + if (!map) { return; } @@ -1108,4 +1144,3 @@ void tmj_map_free(Map* map){ free(map); } - diff --git a/src/tileset.c b/src/tileset.c index 67c1f9b..d2e1f0a 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -10,10 +10,10 @@ * @file */ -int unpack_tileset(json_t* tileset, Tileset* ret){ +int unpack_tileset(json_t* tileset, Tileset* ret) { logmsg(TMJ_LOG_DEBUG, "Unpacking tileset"); - if(tileset == NULL){ + if (tileset == NULL) { return -1; } @@ -28,68 +28,88 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ // Unpack scalar values int unpk = json_unpack_ex(tileset, - &error, - 0, - "{" - "s?s, s?s, s?s, s:s, s:s, s?s, s?s, s:s, s?s, s?s, s:s, s:s," - "s:i, s?i, s:i, s:i, s:i, s:i, s:i, s:i, s:i," - "s?o, s?o, s?o, s?o, s?o, s?o" - "}", - "backgroundcolor", &ret->backgroundcolor, - "class", &ret->class, - "fillmode", &ret->fillmode, - "image", &ret->image, - "name", &ret->name, - "objectalignment", &ret->objectalignment, - "source", &ret->source, - "tiledversion", &ret->tiledversion, - "tilerendersize", &ret->tilerendersize, - "transparentcolor", &ret->transparentcolor, - "type", &ret->type, - "version", &ret->version, - "columns", &ret->columns, - "firstgid", &ret->firstgid, - "imageheight", &ret->imageheight, - "imagewidth", &ret->imagewidth, - "margin", &ret->margin, - "spacing", &ret->spacing, - "tilecount", &ret->tilecount, - "tileheight", &ret->tileheight, - "tilewidth", &ret->tilewidth, - "grid", &grid, - "tileoffset", &tileoffset, - "transformations", &transformations, - "properties", &properties, - "terrains", &terrains, - "tiles", &tiles - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s?s, s?s, s?s, s:s, s:s, s?s, s?s, s:s, s?s, s?s, s:s, s:s," + "s:i, s?i, s:i, s:i, s:i, s:i, s:i, s:i, s:i," + "s?o, s?o, s?o, s?o, s?o, s?o" + "}", + "backgroundcolor", + &ret->backgroundcolor, + "class", + &ret->class, + "fillmode", + &ret->fillmode, + "image", + &ret->image, + "name", + &ret->name, + "objectalignment", + &ret->objectalignment, + "source", + &ret->source, + "tiledversion", + &ret->tiledversion, + "tilerendersize", + &ret->tilerendersize, + "transparentcolor", + &ret->transparentcolor, + "type", + &ret->type, + "version", + &ret->version, + "columns", + &ret->columns, + "firstgid", + &ret->firstgid, + "imageheight", + &ret->imageheight, + "imagewidth", + &ret->imagewidth, + "margin", + &ret->margin, + "spacing", + &ret->spacing, + "tilecount", + &ret->tilecount, + "tileheight", + &ret->tileheight, + "tilewidth", + &ret->tilewidth, + "grid", + &grid, + "tileoffset", + &tileoffset, + "transformations", + &transformations, + "properties", + &properties, + "terrains", + &terrains, + "tiles", + &tiles); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset, %s at line %d column %d", error.text, error.line, error.column); return -1; } // Unpack Grid - if(grid){ + if (grid) { ret->grid = calloc(1, sizeof(Grid)); - if(ret->grid == NULL){ + if (ret->grid == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->grid, the system is out of memory", ret->name); return -1; } - unpk = json_unpack_ex(grid, - &error, - 0, - "{s:i, s:s, s:i}", - "height", &ret->grid->height, - "orientation", &ret->grid->orientation, - "width", &ret->grid->width - ); + unpk = json_unpack_ex( + grid, &error, 0, "{s:i, s:s, s:i}", "height", &ret->grid->height, "orientation", &ret->grid->orientation, "width", &ret->grid->width); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->grid, %s at line %d column %d", ret->name, error.text, error.line, error.column); goto fail_grid; @@ -97,24 +117,18 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack TileOffset - if(tileoffset){ + if (tileoffset) { ret->tileoffset = calloc(1, sizeof(TileOffset)); - if(ret->tileoffset == NULL){ + if (ret->tileoffset == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tileoffset, the system is out of memory", ret->name); goto fail_grid; } - unpk = json_unpack_ex(tileoffset, - &error, - 0, - "{s:i, s:i}", - "x", &ret->tileoffset->x, - "y", &ret->tileoffset->y - ); + unpk = json_unpack_ex(tileoffset, &error, 0, "{s:i, s:i}", "x", &ret->tileoffset->x, "y", &ret->tileoffset->y); - if(unpk == -1){ + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tileoffset, %s at line %d column %d", ret->name, error.text, error.line, error.column); goto fail_tileoffset; @@ -122,35 +136,43 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Transformations - if(transformations){ + if (transformations) { ret->transformations = calloc(1, sizeof(Transformations)); - if(ret->transformations == NULL){ + if (ret->transformations == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->transformations, the system is out of memory", ret->name); goto fail_tileoffset; } unpk = json_unpack_ex(transformations, - &error, - 0, - "{s:b, s:b, s:b, s:b}", - "hflip", &ret->transformations->hflip, - "vfilp", &ret->transformations->vflip, - "rotate", &ret->transformations->rotate, - "preferuntransformed", &ret->transformations->preferuntransformed - ); - - if(unpk == -1){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->transformations, %s at line %d column %d", ret->name, error.text, error.line, error.column); + &error, + 0, + "{s:b, s:b, s:b, s:b}", + "hflip", + &ret->transformations->hflip, + "vfilp", + &ret->transformations->vflip, + "rotate", + &ret->transformations->rotate, + "preferuntransformed", + &ret->transformations->preferuntransformed); + + if (unpk == -1) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->transformations, %s at line %d column %d", + ret->name, + error.text, + error.line, + error.column); goto fail_transformations; } } // Unpack Properties - if(properties){ - if((ret->properties = unpack_properties(properties)) == NULL){ + if (properties) { + if ((ret->properties = unpack_properties(properties)) == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->properties", ret->name); goto fail_transformations; @@ -160,8 +182,8 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Terrains - if(terrains){ - if(!json_is_array(terrains)){ + if (terrains) { + if (!json_is_array(terrains)) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->terrains, terrains must be an array of Terrains", ret->name); goto fail_properties; @@ -171,7 +193,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ ret->terrains = calloc(ret->terrain_count, sizeof(Terrain)); - if(ret->terrains == NULL){ + if (ret->terrains == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->terrains, the system is out of memory", ret->name); goto fail_properties; @@ -180,26 +202,33 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ size_t idx; json_t* terrain; - json_array_foreach(terrains, idx, terrain){ + json_array_foreach(terrains, idx, terrain) { properties = NULL; unpk = json_unpack_ex(terrain, - &error, - 0, - "{s:s, s:i, s?o}", - "name", &ret->terrains[idx].name, - "tile", &ret->terrains[idx].tile, - "properties", &properties - ); - - if(unpk == -1){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->terrains, %s at line %d column %d", ret->name, error.text, error.line, error.column); + &error, + 0, + "{s:s, s:i, s?o}", + "name", + &ret->terrains[idx].name, + "tile", + &ret->terrains[idx].tile, + "properties", + &properties); + + if (unpk == -1) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->terrains, %s at line %d column %d", + ret->name, + error.text, + error.line, + error.column); goto fail_terrains; } - if(properties){ - if((ret->terrains[idx].properties = unpack_properties(properties)) == NULL){ + if (properties) { + if ((ret->terrains[idx].properties = unpack_properties(properties)) == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->terrain[%s]->properties", ret->name, ret->terrains[idx].name); goto fail_terrains; @@ -211,8 +240,8 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Tiles - if(tiles){ - if(!json_is_array(tiles)){ + if (tiles) { + if (!json_is_array(tiles)) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles, tiles must be an array of Tiles", ret->name); goto fail_terrains; @@ -222,7 +251,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ ret->tiles = calloc(ret->tile_count, sizeof(Tile)); - if(ret->tiles == NULL){ + if (ret->tiles == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles, the system is out of memory", ret->name); goto fail_tiles; @@ -231,7 +260,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ size_t idx = 0; json_t* tile = NULL; - json_array_foreach(tiles, idx, tile){ + json_array_foreach(tiles, idx, tile) { // Unpack Tile scalar values json_t* animation = NULL; json_t* objectgroup = NULL; @@ -239,42 +268,58 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ json_t* terrain = NULL; unpk = json_unpack_ex(tile, - &error, - 0, - "{" - "s?s, s?s," - "s:i, s?i, s?i, s?i, s?i, s?i, s?i," - "s?F," - "s?o, s?o, s?o, s?o" - "}", - "image", &ret->tiles[idx].image, - "type", &ret->tiles[idx].type, - "id", &ret->tiles[idx].id, - "imageheight", &ret->tiles[idx].imageheight, - "imagewidth", &ret->tiles[idx].imagewidth, - "x", &ret->tiles[idx].x, - "y", &ret->tiles[idx].y, - "width", &ret->tiles[idx].width, - "height", &ret->tiles[idx].height, - "probability", &ret->tiles[idx].probability, - "animation", &animation, - "objectgroup", &objectgroup, - "properties", &properties, - "terrain", &terrain - ); - - if(unpk == -1){ + &error, + 0, + "{" + "s?s, s?s," + "s:i, s?i, s?i, s?i, s?i, s?i, s?i," + "s?F," + "s?o, s?o, s?o, s?o" + "}", + "image", + &ret->tiles[idx].image, + "type", + &ret->tiles[idx].type, + "id", + &ret->tiles[idx].id, + "imageheight", + &ret->tiles[idx].imageheight, + "imagewidth", + &ret->tiles[idx].imagewidth, + "x", + &ret->tiles[idx].x, + "y", + &ret->tiles[idx].y, + "width", + &ret->tiles[idx].width, + "height", + &ret->tiles[idx].height, + "probability", + &ret->tiles[idx].probability, + "animation", + &animation, + "objectgroup", + &objectgroup, + "properties", + &properties, + "terrain", + &terrain); + + if (unpk == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles, %s at line %d column %d", ret->name, error.text, error.line, error.column); goto fail_tiles; } // Unpack Tile objectgroup - if(objectgroup){ + if (objectgroup) { ret->tiles[idx].objectgroup = calloc(1, sizeof(Layer)); - if(ret->tiles[idx].objectgroup == NULL){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, the system is out of memory", ret->name, ret->tiles[idx].id); + if (ret->tiles[idx].objectgroup == NULL) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, the system is out of memory", + ret->name, + ret->tiles[idx].id); goto fail_tiles; } @@ -283,44 +328,66 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ json_t* layer_properties = NULL; unpk = json_unpack_ex(objectgroup, - &error, - 0, - "{" - "s?b, s:b," - "s?s, s?s, s:s, s?s, s:s," - "s?i, s?i, s?i," - "s?F, s?F, s:F, s?F, s?F," - "s?o, s?o" - "}", - "locked", &ret->tiles[idx].objectgroup->locked, - "visible", &ret->tiles[idx].objectgroup->visible, - "class", &ret->tiles[idx].objectgroup->class, - "draworder", &ret->tiles[idx].objectgroup->draworder, - "name", &ret->tiles[idx].objectgroup->name, - "tintcolor", &ret->tiles[idx].objectgroup->tintcolor, - "type", &ret->tiles[idx].objectgroup->type, - "id", &ret->tiles[idx].objectgroup->id, - "x", &ret->tiles[idx].objectgroup->x, - "y", &ret->tiles[idx].objectgroup->y, - "offsetx", &ret->tiles[idx].objectgroup->offsetx, - "offsety", &ret->tiles[idx].objectgroup->offsety, - "opacity", &ret->tiles[idx].objectgroup->opacity, - "parallaxx", &ret->tiles[idx].objectgroup->parallaxx, - "parallaxy", &ret->tiles[idx].objectgroup->parallaxy, - "objects", &objects, - "properties", &layer_properties - ); - - if(unpk == -1){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, %s at line %d column %d", ret->name, ret->tiles[idx].id, error.text, error.line, error.column); + &error, + 0, + "{" + "s?b, s:b," + "s?s, s?s, s:s, s?s, s:s," + "s?i, s?i, s?i," + "s?F, s?F, s:F, s?F, s?F," + "s?o, s?o" + "}", + "locked", + &ret->tiles[idx].objectgroup->locked, + "visible", + &ret->tiles[idx].objectgroup->visible, + "class", + &ret->tiles[idx].objectgroup->class, + "draworder", + &ret->tiles[idx].objectgroup->draworder, + "name", + &ret->tiles[idx].objectgroup->name, + "tintcolor", + &ret->tiles[idx].objectgroup->tintcolor, + "type", + &ret->tiles[idx].objectgroup->type, + "id", + &ret->tiles[idx].objectgroup->id, + "x", + &ret->tiles[idx].objectgroup->x, + "y", + &ret->tiles[idx].objectgroup->y, + "offsetx", + &ret->tiles[idx].objectgroup->offsetx, + "offsety", + &ret->tiles[idx].objectgroup->offsety, + "opacity", + &ret->tiles[idx].objectgroup->opacity, + "parallaxx", + &ret->tiles[idx].objectgroup->parallaxx, + "parallaxy", + &ret->tiles[idx].objectgroup->parallaxy, + "objects", + &objects, + "properties", + &layer_properties); + + if (unpk == -1) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, %s at line %d column %d", + ret->name, + ret->tiles[idx].id, + error.text, + error.line, + error.column); goto fail_tiles; } - if(objects){ + if (objects) { ret->tiles[idx].objectgroup->objects = unpack_objects(objects); - if(ret->tiles[idx].objectgroup->objects == NULL){ + if (ret->tiles[idx].objectgroup->objects == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->objectgroup->objects", ret->name, ret->tiles[idx].id); goto fail_tiles; @@ -329,10 +396,10 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ ret->tiles[idx].objectgroup->object_count = json_array_size(objects); } - if(layer_properties){ + if (layer_properties) { ret->tiles[idx].objectgroup->properties = unpack_properties(layer_properties); - if(ret->tiles[idx].objectgroup->properties == NULL){ + if (ret->tiles[idx].objectgroup->properties == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->objectgroup->properties", ret->name, ret->tiles[idx].id); goto fail_tiles; @@ -343,17 +410,23 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Tile animation - if(animation){ - if(!json_is_array(animation)){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->animation, animation must be an array of Frames", ret->name, ret->tiles[idx].id); + if (animation) { + if (!json_is_array(animation)) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->animation, animation must be an array of Frames", + ret->name, + ret->tiles[idx].id); goto fail_tiles; } ret->tiles[idx].animation = calloc(json_array_size(animation), sizeof(Frame)); - if(ret->tiles[idx].animation == NULL){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->animation, the system is out of memory", ret->name, ret->tiles[idx].id); + if (ret->tiles[idx].animation == NULL) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->animation, the system is out of memory", + ret->name, + ret->tiles[idx].id); goto fail_tiles; } @@ -361,19 +434,26 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ size_t idx2 = 0; json_t* frame = NULL; - json_array_foreach(animation, idx2, frame){ + json_array_foreach(animation, idx2, frame) { unpk = json_unpack_ex(frame, - &error, - 0, - "{" - "s:i, s:i" - "}", - "duration", &ret->tiles[idx].animation[idx2].duration, - "tileid", &ret->tiles[idx].animation[idx2].tileid - ); - - if(unpk == -1){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->animation, %s at line %d column %d", ret->name, ret->tiles[idx].id, error.text, error.line, error.column); + &error, + 0, + "{" + "s:i, s:i" + "}", + "duration", + &ret->tiles[idx].animation[idx2].duration, + "tileid", + &ret->tiles[idx].animation[idx2].tileid); + + if (unpk == -1) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->animation, %s at line %d column %d", + ret->name, + ret->tiles[idx].id, + error.text, + error.line, + error.column); goto fail_tiles; } @@ -383,10 +463,10 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Tile properties - if(properties){ + if (properties) { ret->tiles[idx].properties = unpack_properties(properties); - if(ret->tiles[idx].properties == NULL){ + if (ret->tiles[idx].properties == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->properties", ret->name, ret->tiles[idx].id); goto fail_tiles; @@ -396,9 +476,13 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ } // Unpack Tile terrain - if(terrain){ - if(!json_is_array(terrain)){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of terrain indexes", ret->name, ret->tiles[idx].id); + if (terrain) { + if (!json_is_array(terrain)) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of terrain " + "indexes", + ret->name, + ret->tiles[idx].id); goto fail_tiles; } @@ -406,9 +490,11 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ size_t idx2 = 0; json_t* terrain_idx = NULL; - json_array_foreach(terrain, idx2, terrain_idx){ - if(!json_is_integer(terrain_idx)){ - logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of integers"); + json_array_foreach(terrain, idx2, terrain_idx) { + if (!json_is_integer(terrain_idx)) { + logmsg(TMJ_LOG_ERR, + "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of " + "integers"); goto fail_tiles; } @@ -422,9 +508,9 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ return 0; fail_tiles: - for(size_t i = 0; i < ret->tile_count; i++){ + for (size_t i = 0; i < ret->tile_count; i++) { free(ret->tiles[i].animation); - if(ret->tiles[i].objectgroup != NULL){ + if (ret->tiles[i].objectgroup != NULL) { free(ret->tiles[i].objectgroup->properties); free_objects(ret->tiles[i].objectgroup->objects, ret->tiles[i].objectgroup->object_count); } @@ -435,7 +521,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ free(ret->tiles); fail_terrains: - for(size_t i = 0; i < ret->terrain_count; i++){ + for (size_t i = 0; i < ret->terrain_count; i++) { free(ret->terrains[i].properties); } @@ -459,13 +545,13 @@ int unpack_tileset(json_t* tileset, Tileset* ret){ /** * Helper function for freeing tilesets embedded in maps */ -void tilesets_free(Tileset* tilesets, size_t tileset_count){ - for(size_t i = 0; i < tileset_count; i++){ +void tilesets_free(Tileset* tilesets, size_t tileset_count) { + for (size_t i = 0; i < tileset_count; i++) { // Free tiles - if(tilesets[i].tiles){ - for(size_t j = 0; j < tilesets[i].tile_count; j++){ + if (tilesets[i].tiles) { + for (size_t j = 0; j < tilesets[i].tile_count; j++) { free(tilesets[i].tiles[j].animation); - if(tilesets[i].tiles[j].objectgroup != NULL){ + if (tilesets[i].tiles[j].objectgroup != NULL) { free(tilesets[i].tiles[j].objectgroup->properties); free_objects(tilesets[i].tiles[j].objectgroup->objects, tilesets[i].tiles[j].objectgroup->object_count); } @@ -477,8 +563,8 @@ void tilesets_free(Tileset* tilesets, size_t tileset_count){ } // Free terrains - if(tilesets[i].terrains){ - for(size_t j = 0; j < tilesets[i].terrain_count; j++){ + if (tilesets[i].terrains) { + for (size_t j = 0; j < tilesets[i].terrain_count; j++) { free(tilesets[i].terrains[j].properties); } @@ -497,19 +583,19 @@ void tilesets_free(Tileset* tilesets, size_t tileset_count){ free(tilesets); } -Tileset* tmj_tileset_loadf(const char* path, bool check_extension){ +Tileset* tmj_tileset_loadf(const char* path, bool check_extension) { logmsg(TMJ_LOG_DEBUG, "Loading JSON tileset file '%s'", path); char* ext = strrchr(path, '.'); - if(check_extension){ - if(ext == NULL){ + if (check_extension) { + if (ext == NULL) { logmsg(TMJ_LOG_ERR, "Tileset filename '%s' has no extension", path); return NULL; } - if(strcmp(ext, ".tsj") != 0 && strcmp(ext, ".json") != 0){ + if (strcmp(ext, ".tsj") != 0 && strcmp(ext, ".json") != 0) { logmsg(TMJ_LOG_ERR, "Tileset filename '%s' has unknown extension, '%s'", path, ext); logmsg(TMJ_LOG_ERR, "Tileset filename '%s' must have '.tsj' or '.json' extension to be loaded", path); @@ -520,7 +606,7 @@ Tileset* tmj_tileset_loadf(const char* path, bool check_extension){ json_error_t error; json_t* root = json_load_file(path, JSON_REJECT_DUPLICATES, &error); - if(root == NULL){ + if (root == NULL) { logmsg(TMJ_LOG_ERR, "Could not load tileset '%s', %s at line %d column %d", path, error.text, error.line, error.column); return NULL; @@ -528,13 +614,13 @@ Tileset* tmj_tileset_loadf(const char* path, bool check_extension){ Tileset* ret = calloc(1, sizeof(Tileset)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to load tileset[%s], the system is out of memory", path); return NULL; } - if(unpack_tileset(root, ret) == -1){ + if (unpack_tileset(root, ret) == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]", path); free(ret); @@ -547,13 +633,13 @@ Tileset* tmj_tileset_loadf(const char* path, bool check_extension){ return ret; } -Tileset* tmj_tileset_load(const char* tileset){ +Tileset* tmj_tileset_load(const char* tileset) { logmsg(TMJ_LOG_DEBUG, "Loading JSON tileset from string"); json_error_t error; json_t* root = json_loads(tileset, JSON_REJECT_DUPLICATES, &error); - if(root == NULL){ + if (root == NULL) { logmsg(TMJ_LOG_ERR, "Could not load tileset, %s at line %d column %d", error.text, error.line, error.column); return NULL; @@ -561,13 +647,13 @@ Tileset* tmj_tileset_load(const char* tileset){ Tileset* ret = calloc(1, sizeof(Tileset)); - if(ret == NULL){ + if (ret == NULL) { logmsg(TMJ_LOG_ERR, "Unable to load tileset, the system is out of memory"); return NULL; } - if(unpack_tileset(root, ret) == -1){ + if (unpack_tileset(root, ret) == -1) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset"); free(ret); @@ -580,6 +666,6 @@ Tileset* tmj_tileset_load(const char* tileset){ return ret; } -void tmj_tileset_free(Tileset* tileset){ +void tmj_tileset_free(Tileset* tileset) { tilesets_free(tileset, 1); } diff --git a/src/util.c b/src/util.c index c9e153e..c27664a 100644 --- a/src/util.c +++ b/src/util.c @@ -12,8 +12,8 @@ const unsigned int TMJ_VERSION_MINOR = LIBTMJ_VERSION_MINOR; const unsigned int TMJ_VERSION_PATCH = LIBTMJ_VERSION_PATCH; const char* const TMJ_VERSION = LIBTMJ_VERSION; -uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size){ - if(strcmp(encoding, "base64") != 0){ +uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size) { + if (strcmp(encoding, "base64") != 0) { logmsg(TMJ_LOG_ERR, "Layer data in csv format; decode it yourself"); return NULL; @@ -22,7 +22,7 @@ uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* c size_t dsize = 0; uint8_t* dat = tmj_b64_decode(data, &dsize); - if(dat == NULL){ + if (dat == NULL) { logmsg(TMJ_LOG_ERR, "Unable to base64 decode layer data"); return NULL; @@ -31,7 +31,7 @@ uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* c size_t dsize2 = 0; uint8_t* dat2 = NULL; - if(strcmp(compression, "zlib") == 0 || strcmp(compression, "gzip") == 0){ + if (strcmp(compression, "zlib") == 0 || strcmp(compression, "gzip") == 0) { #ifndef LIBTMJ_ZLIB logmsg(TMJ_LOG_ERR, "Layer data encoded with %s, but libtmj was not compiled with %s support", compression, compression); @@ -43,7 +43,7 @@ uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* c #endif } - if(strcmp(compression, "zstd") == 0){ + if (strcmp(compression, "zstd") == 0) { #ifndef LIBTMJ_ZSTD logmsg(TMJ_LOG_ERR, "Layer data encoded with zstd, but libtmj was not compiled with zstd support"); @@ -54,7 +54,7 @@ uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* c #endif } - if(dat2 == NULL){ + if (dat2 == NULL) { logmsg(TMJ_LOG_ERR, "Unable to decompress %s encoded layer data", compression); free(dat); diff --git a/test/decode_tests.c b/test/decode_tests.c index 7ec8d62..ec066f2 100644 --- a/test/decode_tests.c +++ b/test/decode_tests.c @@ -5,8 +5,8 @@ #include "Unity/src/unity.h" -void log_cb(tmj_log_priority priority, const char* msg){ - switch(priority){ +void log_cb(tmj_log_priority priority, const char* msg) { + switch (priority) { case TMJ_LOG_DEBUG: printf("DEBUG: %s\n", msg); break; @@ -25,12 +25,12 @@ void log_cb(tmj_log_priority priority, const char* msg){ } } -void setUp(void){ +void setUp(void) { tmj_log_regcb(true, log_cb); } -void tearDown(void){} +void tearDown(void) {} -void test_b64_decode(void){ +void test_b64_decode(void) { const char* msg = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nAA=="; const char* msg2 = "VGhpcyBpcyBhbm90aGVyIHRlc3Qgc3RyaW5nIQA="; @@ -52,7 +52,7 @@ void test_b64_decode(void){ } #ifdef LIBTMJ_ZLIB -void test_zlib_decode(void){ +void test_zlib_decode(void) { const char* msg_zlib = "eJwLycgsVgCixLz8kozUIoWS1OISheKSosy8dEUGAKBMCl4="; const char* msg_gzip = "H4sIAAAAAAAAAwvJyCxWAKLEvPySjNQihZLU4hKF4pKizLx0RQYAPCsnLx0AAAA="; @@ -74,7 +74,7 @@ void test_zlib_decode(void){ free(out); - TEST_ASSERT_EQUAL_INT(29, decompressed_size_zlib); //28 characters + 1 trailing NULL + TEST_ASSERT_EQUAL_INT(29, decompressed_size_zlib); // 28 characters + 1 trailing NULL TEST_ASSERT_EQUAL_INT(29, decompressed_size_gzip); TEST_ASSERT_EQUAL_STRING("This is another test string!", msg_zlib_decompressed); TEST_ASSERT_EQUAL_STRING("This is another test string!", msg_gzip_decompressed); @@ -85,7 +85,7 @@ void test_zlib_decode(void){ #endif #ifdef LIBTMJ_ZSTD -void test_zstd_decode(void){ +void test_zstd_decode(void) { const char* msg = "KLUv/SQd6QAAVGhpcyBpcyBhbm90aGVyIHRlc3Qgc3RyaW5nIQDZN/8V"; size_t dSize = 0; @@ -103,7 +103,7 @@ void test_zstd_decode(void){ } #endif -int main(void){ +int main(void) { UNITY_BEGIN(); RUN_TEST(test_b64_decode); #ifdef LIBTMJ_ZLIB diff --git a/test/map_tests.c b/test/map_tests.c index 60c2c68..2710322 100644 --- a/test/map_tests.c +++ b/test/map_tests.c @@ -4,8 +4,8 @@ #include "Unity/src/unity.h" -void log_cb(tmj_log_priority priority, const char* msg){ - switch(priority){ +void log_cb(tmj_log_priority priority, const char* msg) { + switch (priority) { case TMJ_LOG_DEBUG: printf("DEBUG: %s\n", msg); break; @@ -24,11 +24,11 @@ void log_cb(tmj_log_priority priority, const char* msg){ } } -void setUp(void){ +void setUp(void) { tmj_log_regcb(true, log_cb); } -void tearDown(void){} +void tearDown(void) {} char* testmap_path = "example/overworld.tmj"; char* testmap_path2 = "example/testmap.tmj"; @@ -37,7 +37,7 @@ Map* mf = NULL; Map* mf2 = NULL; Map* ms = NULL; -void test_map_loadf(void){ +void test_map_loadf(void) { mf = tmj_map_loadf(testmap_path, true); mf2 = tmj_map_loadf(testmap_path2, true); TEST_ASSERT_NOT_NULL(mf); @@ -45,10 +45,10 @@ void test_map_loadf(void){ TEST_ASSERT_EQUAL_size_t(4, mf->layer_count); TEST_ASSERT_EQUAL_STRING("tilelayer", mf->layers[0].type); TEST_ASSERT_EQUAL_STRING("bar", mf2->properties[0].value_string); - TEST_ASSERT_EQUAL_INT(1, mf2->properties[1].value_int); + TEST_ASSERT_EQUAL_INT(1, mf2->properties[1].value_object); } -void test_map_load(void){ +void test_map_load(void) { FILE* f = fopen(testmap_path, "rb"); fseek(f, 0, SEEK_END); @@ -69,13 +69,13 @@ void test_map_load(void){ free(s); } -void test_map_free(void){ +void test_map_free(void) { tmj_map_free(mf); tmj_map_free(mf2); tmj_map_free(ms); } -int main(void){ +int main(void) { UNITY_BEGIN(); RUN_TEST(test_map_loadf); RUN_TEST(test_map_load); diff --git a/test/tileset_tests.c b/test/tileset_tests.c index 0f94f26..3a1abb8 100644 --- a/test/tileset_tests.c +++ b/test/tileset_tests.c @@ -4,8 +4,8 @@ #include "Unity/src/unity.h" -void log_cb(tmj_log_priority priority, const char* msg){ - switch(priority){ +void log_cb(tmj_log_priority priority, const char* msg) { + switch (priority) { case TMJ_LOG_DEBUG: printf("DEBUG: %s\n", msg); break; @@ -24,23 +24,23 @@ void log_cb(tmj_log_priority priority, const char* msg){ } } -void setUp(void){ +void setUp(void) { tmj_log_regcb(true, log_cb); } -void tearDown(void){} +void tearDown(void) {} char* tileset_path = "example/overworld.tsj"; Tileset* tf = NULL; Tileset* ts = NULL; -void test_tileset_loadf(void){ +void test_tileset_loadf(void) { tf = tmj_tileset_loadf(tileset_path, true); TEST_ASSERT_NOT_NULL(tf); } -void test_tileset_load(void){ +void test_tileset_load(void) { FILE* f = fopen(tileset_path, "rb"); fseek(f, 0, SEEK_END); @@ -59,12 +59,12 @@ void test_tileset_load(void){ free(s); } -void test_tileset_free(void){ +void test_tileset_free(void) { tmj_tileset_free(tf); tmj_tileset_free(ts); } -int main(void){ +int main(void) { UNITY_BEGIN(); RUN_TEST(test_tileset_loadf); RUN_TEST(test_tileset_load); diff --git a/test/util_tests.c b/test/util_tests.c index 85cb249..8610e7c 100644 --- a/test/util_tests.c +++ b/test/util_tests.c @@ -2,8 +2,8 @@ #include "Unity/src/unity.h" -void log_cb(tmj_log_priority priority, const char* msg){ - switch(priority){ +void log_cb(tmj_log_priority priority, const char* msg) { + switch (priority) { case TMJ_LOG_DEBUG: printf("DEBUG: %s\n", msg); break; @@ -22,20 +22,20 @@ void log_cb(tmj_log_priority priority, const char* msg){ } } -void setUp(){ +void setUp() { tmj_log_regcb(true, log_cb); } -void tearDown(){} +void tearDown() {} -void test_version(){ +void test_version() { TEST_ASSERT_EQUAL_UINT(TMJ_VERSION_MAJOR, 0); TEST_ASSERT_EQUAL_UINT(TMJ_VERSION_MINOR, 1); TEST_ASSERT_EQUAL_UINT(TMJ_VERSION_PATCH, 7); TEST_ASSERT_EQUAL_STRING(TMJ_VERSION, "0.1.0"); } -int main(){ +int main() { UNITY_BEGIN(); RUN_TEST(test_version); return UNITY_END();