From 7d956c533e9fd7d3bbb9a4a8b7f89f0fe54d4bc3 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 13 Apr 2025 22:43:46 +0200 Subject: first draft --- lib/.clang-format | 45 +++++++++++++++++++++++++++++++ lib/.clang-tidy | 19 +++++++++++++ lib/compile_flags.txt | 8 ++++++ lib/config.mk | 4 +++ lib/makefile | 20 ++++++++++++++ lib/src/csv.c | 74 +++++++++++++++++++++++++-------------------------- 6 files changed, 133 insertions(+), 37 deletions(-) create mode 100644 lib/.clang-format create mode 100644 lib/.clang-tidy create mode 100644 lib/compile_flags.txt create mode 100644 lib/config.mk (limited to 'lib') diff --git a/lib/.clang-format b/lib/.clang-format new file mode 100644 index 0000000..b32bad6 --- /dev/null +++ b/lib/.clang-format @@ -0,0 +1,45 @@ +--- +AccessModifierOffset: -4 +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignTrailingComments: 'true' +AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBraces: Stroustrup +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +ColumnLimit: '0' +CompactNamespaces: 'false' +Cpp11BracedListStyle: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*(precomp|pch|stdafx)' + Priority: -1 + - Regex: '^<.*>' + Priority: 1 + - Regex: '^".*"' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: 'false' +IndentPPDirectives: AfterHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +PointerAlignment: Right +SortIncludes: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInConditionalStatement: 'true' +SpacesInParentheses: 'false' +Standard: Auto +TabWidth: '4' +UseTab: ForIndentation +... diff --git a/lib/.clang-tidy b/lib/.clang-tidy new file mode 100644 index 0000000..0bceb5b --- /dev/null +++ b/lib/.clang-tidy @@ -0,0 +1,19 @@ +--- +Checks: "*, + -abseil-*, + -altera-*, + -android-*, + -fuchsia-*, + -google-*, + -llvm*, + -modernize-use-trailing-return-type, + -zircon-*, + -readability-else-after-return, + -readability-static-accessed-through-instance, + -readability-avoid-const-params-in-decls, + -cppcoreguidelines-non-private-member-variables-in-classes, + -misc-non-private-member-variables-in-classes, +" +WarningsAsErrors: '' +HeaderFilterRegex: '' +FormatStyle: none diff --git a/lib/compile_flags.txt b/lib/compile_flags.txt new file mode 100644 index 0000000..8b85c07 --- /dev/null +++ b/lib/compile_flags.txt @@ -0,0 +1,8 @@ +-Wall +-Wextra +-Wsign-compare +-Wsign-conversion +-pedantic +-std=c99 +-O2 +-Iinclude diff --git a/lib/config.mk b/lib/config.mk new file mode 100644 index 0000000..6b4ff86 --- /dev/null +++ b/lib/config.mk @@ -0,0 +1,4 @@ +BUILD_DIR=../build +ARFLAGS=rcs +CFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c99 -O2 -Iinclude -DNDEBUG + diff --git a/lib/makefile b/lib/makefile index e69de29..5c832ba 100644 --- a/lib/makefile +++ b/lib/makefile @@ -0,0 +1,20 @@ +include config.mk + +LIBRARY=$(BUILD_DIR)/libcsv.a +OBJS=$(BUILD_DIR)/csv.o + +$(LIBRARY): $(OBJS) + ar $(ARFLAGS) $@ $(OBJS) + +$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) + cc $(CFLAGS) -c $< -o $@ + +$(BUILD_DIR): + mkdir -p $@ + +compile_flags.txt: + echo "$(CFLAGS)" | tr ' ' '\n' > $@ + +.PHONY: clean +clean: + rm -f $(LIBRARY) $(OBJS) diff --git a/lib/src/csv.c b/lib/src/csv.c index e9c7464..ba78dbd 100644 --- a/lib/src/csv.c +++ b/lib/src/csv.c @@ -305,35 +305,35 @@ csv_read(csv_t *csv, FILE *in) csv_field_reset(&csv->csv_field); for ( int state = STATE_START_FIELD;; ) { - int ch; + int chr; switch ( state ) { case STATE_START_FIELD: csv_field_append(&csv->csv_field, csv->csv_string.pos, csv->csv_options); - ch = getc(in); - if ( ch == EOF ) { + chr = getc(in); + if ( chr == EOF ) { state = STATE_END_FILE; } - else if ( ch == '\r' ) { // test for CR.. - ch = getc(in); - if ( ch != '\n' ) { // ..LF - (void) ungetc(ch, in); + else if ( chr == '\r' ) { // test for CR.. + chr = getc(in); + if ( chr != '\n' ) { // ..LF + (void) ungetc(chr, in); } state = STATE_END_LINE; } - else if ( ch == '\n' ) { + else if ( chr == '\n' ) { state = STATE_END_LINE; } - else if ( ch == SEP ) { + else if ( chr == SEP ) { state = STATE_END_FIELD; } - else if ( ch == DELIM ) { + else if ( chr == DELIM ) { state = STATE_QUOTED_FIELD; } else { - if ( ch != '\0' ) { - csv_string_append(&csv->csv_string, ch, csv->csv_options); + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); } state = STATE_SIMPLE_FIELD; } @@ -341,39 +341,39 @@ csv_read(csv_t *csv, FILE *in) case STATE_QUOTED_FIELD: do { - ch = getc(in); - if ( ch == EOF ) { + chr = getc(in); + if ( chr == EOF ) { state = STATE_END_FILE; } - else if ( ch == DELIM ) { - ch = getc(in); - if ( ch == EOF ) { + else if ( chr == DELIM ) { + chr = getc(in); + if ( chr == EOF ) { state = STATE_END_FILE; } - else if ( ch == DELIM ) { + else if ( chr == DELIM ) { csv_string_append(&csv->csv_string, DELIM, csv->csv_options); } - else if ( ch == SEP ) { + else if ( chr == SEP ) { state = STATE_END_FIELD; } - else if ( ch == '\r' ) { - ch = getc(in); - if ( ch != '\n' ) { - (void) ungetc(ch, in); + else if ( chr == '\r' ) { + chr = getc(in); + if ( chr != '\n' ) { + (void) ungetc(chr, in); } state = STATE_END_LINE; } - else if ( ch == '\n' ) { + else if ( chr == '\n' ) { state = STATE_END_LINE; } else { csv_string_append(&csv->csv_string, DELIM, csv->csv_options); - (void) ungetc(ch, in); // we have read too far... Put the character back! + (void) ungetc(chr, in); // we have read too far... Put the character back! } } else { - if ( ch != '\0' ) { - csv_string_append(&csv->csv_string, ch, csv->csv_options); + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); } } } while ( state == STATE_QUOTED_FIELD ); @@ -381,26 +381,26 @@ csv_read(csv_t *csv, FILE *in) case STATE_SIMPLE_FIELD: do { - ch = getc(in); - if ( ch == EOF ) { + chr = getc(in); + if ( chr == EOF ) { state = STATE_END_FILE; } - else if ( ch == SEP ) { + else if ( chr == SEP ) { state = STATE_END_FIELD; } - else if ( ch == '\r' ) { - ch = getc(in); - if ( ch != '\n' ) { - (void) ungetc(ch, in); + else if ( chr == '\r' ) { + chr = getc(in); + if ( chr != '\n' ) { + (void) ungetc(chr, in); } state = STATE_END_LINE; } - else if ( ch == '\n' ) { + else if ( chr == '\n' ) { state = STATE_END_LINE; } else { - if ( ch != '\0' ) { - csv_string_append(&csv->csv_string, ch, csv->csv_options); + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); } } } while ( state == STATE_SIMPLE_FIELD ); -- cgit v1.3