Skip to content

Commit

Permalink
Merge pull request #42 from bettio/clang-format
Browse files Browse the repository at this point in the history
Clang format
  • Loading branch information
rbino authored Oct 13, 2020
2 parents 175ab02 + 610cd45 commit f011d59
Show file tree
Hide file tree
Showing 16 changed files with 567 additions and 353 deletions.
112 changes: 112 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
# Change to IndentPPDirectives: BeforeHash
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

88 changes: 47 additions & 41 deletions astarte_bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#include <astarte_bson_types.h>

#include <endian.h>
#include <stdio.h>
#include <string.h>
#include <endian.h>

#include <esp_log.h>

Expand All @@ -19,16 +19,20 @@
static uint32_t read_uint32(const void *u)
{
const unsigned char *b = (const unsigned char *) u;
return le32toh(((uint32_t) b[0]) | (((uint32_t) b[1]) << 8) | (((uint32_t) b[2]) << 16) | (((uint32_t) b[3]) << 24));
return le32toh(((uint32_t) b[0]) | (((uint32_t) b[1]) << 8) | (((uint32_t) b[2]) << 16)
| (((uint32_t) b[3]) << 24));
}

static uint64_t read_uint64(const void *u)
{
const unsigned char *b = (const unsigned char *) u;
return le64toh((uint64_t) b[0] | ((uint64_t) b[1] << 8) | ((uint64_t) b[2] << 16) | ((uint64_t) b[3] << 24) | ((uint64_t) b[4] << 32) | ((uint64_t) b[5] << 40) | ((uint64_t) b[6] << 48) | ((uint64_t) b[7] << 56));
return le64toh((uint64_t) b[0] | ((uint64_t) b[1] << 8) | ((uint64_t) b[2] << 16)
| ((uint64_t) b[3] << 24) | ((uint64_t) b[4] << 32) | ((uint64_t) b[5] << 40)
| ((uint64_t) b[6] << 48) | ((uint64_t) b[7] << 56));
}

static unsigned int astarte_bson_next_item_offset(unsigned int offset, unsigned int keyLen, const void *document)
static unsigned int astarte_bson_next_item_offset(
unsigned int offset, unsigned int keyLen, const void *document)
{
const char *docBytes = (const char *) document;
uint8_t elementType = (uint8_t) docBytes[offset];
Expand All @@ -40,37 +44,37 @@ static unsigned int astarte_bson_next_item_offset(unsigned int offset, unsigned
case BSON_TYPE_STRING: {
uint32_t stringLen = read_uint32(docBytes + offset);
offset += stringLen + 4;
break;
}
break;

case BSON_TYPE_DOCUMENT: {
uint32_t docLen = read_uint32(docBytes + offset);
offset += docLen;
break;
}
break;

case BSON_TYPE_BINARY: {
uint32_t binLen = read_uint32(docBytes + offset);
offset += 4 + 1 + binLen; /* int32 (len) + byte (subtype) + binLen */
break;
}
break;

case BSON_TYPE_INT32: {
offset += sizeof(int32_t);
offset += sizeof(int32_t);
break;
}
break;

case BSON_TYPE_DOUBLE:
case BSON_TYPE_DATETIME:
case BSON_TYPE_INT64: {
offset += sizeof(int64_t);
break;
}
break;

case BSON_TYPE_BOOLEAN: {
offset += 1;
break;
}
break;

default: {
ESP_LOGW(TAG, "unrecognized BSON type: %i", (int) elementType);
Expand All @@ -90,21 +94,21 @@ const void *astarte_bson_key_lookup(const char *key, const void *document, uint8

unsigned int offset = 4;
while (offset + 1 < docLen) {
uint8_t elementType = (uint8_t) docBytes[offset];
int keyLen = strnlen(docBytes + offset + 1, docLen - offset);

if (!strncmp(key, docBytes + offset + 1, docLen - offset)) {
if (type) {
*type = elementType;
}
return (void *) (docBytes + offset + 1 + keyLen + 1);
}

unsigned int newOffset = astarte_bson_next_item_offset(offset, keyLen, document);
if (!newOffset) {
return NULL;
}
offset = newOffset;
uint8_t elementType = (uint8_t) docBytes[offset];
int keyLen = strnlen(docBytes + offset + 1, docLen - offset);

if (!strncmp(key, docBytes + offset + 1, docLen - offset)) {
if (type) {
*type = elementType;
}
return (void *) (docBytes + offset + 1 + keyLen + 1);
}

unsigned int newOffset = astarte_bson_next_item_offset(offset, keyLen, document);
if (!newOffset) {
return NULL;
}
offset = newOffset;
}

return NULL;
Expand Down Expand Up @@ -198,7 +202,8 @@ int64_t astarte_bson_value_to_int64(const void *valuePtr)

double astarte_bson_value_to_double(const void *valuePtr)
{
union data64 {
union data64
{
uint64_t u64value;
double dvalue;
} v;
Expand Down Expand Up @@ -228,7 +233,8 @@ int astarte_bson_check_validity(const void *document, unsigned int fileSize)
}

if (docLen > fileSize) {
ESP_LOGW(TAG, "BSON document is bigger than data: data: %ui document: %i", fileSize, (int) docLen);
ESP_LOGW(TAG, "BSON document is bigger than data: data: %ui document: %i", fileSize,
(int) docLen);
return 0;
}

Expand All @@ -239,19 +245,19 @@ int astarte_bson_check_validity(const void *document, unsigned int fileSize)

offset = 4;
switch (docBytes[offset]) {
case BSON_TYPE_DOUBLE:
case BSON_TYPE_STRING:
case BSON_TYPE_DOCUMENT:
case BSON_TYPE_BINARY:
case BSON_TYPE_BOOLEAN:
case BSON_TYPE_DATETIME:
case BSON_TYPE_INT32:
case BSON_TYPE_INT64:
break;

default:
ESP_LOGW(TAG, "Unrecognized BSON document first type\n");
return 0;
case BSON_TYPE_DOUBLE:
case BSON_TYPE_STRING:
case BSON_TYPE_DOCUMENT:
case BSON_TYPE_BINARY:
case BSON_TYPE_BOOLEAN:
case BSON_TYPE_DATETIME:
case BSON_TYPE_INT32:
case BSON_TYPE_INT64:
break;

default:
ESP_LOGW(TAG, "Unrecognized BSON document first type\n");
return 0;
}

return 1;
Expand Down
Loading

0 comments on commit f011d59

Please sign in to comment.