Skip to content

Commit 1e26a6b

Browse files
authored
Merge pull request #20 from asdf-format/issue-16
Resolves #16: Improve I/O abstraction
2 parents 24c6f5b + 8656a48 commit 1e26a6b

32 files changed

+2679
-377
lines changed

.clang-format

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ AllowShortLoopsOnASingleLine: false
2222
ColumnLimit: 100
2323
MaxEmptyLinesToKeep: 2
2424

25-
PenaltyReturnTypeOnItsOwnLine: 100
25+
PenaltyReturnTypeOnItsOwnLine: 10000
2626
PenaltyBreakBeforeFirstCallParameter: 100
2727
PenaltyBreakString: 1000
2828

2929
BinPackArguments: false
3030
BinPackParameters: false
3131

3232
BreakBeforeBraces: Attach
33-
AlignAfterOpenBracket: DontAlign
33+
AlignAfterOpenBracket: AlwaysBreak
3434
AlignEscapedNewlines: DontAlign
3535
AlwaysBreakAfterDefinitionReturnType: None
3636
AlwaysBreakBeforeMultilineStrings: false

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CheckOptions:
1818
- key: readability-identifier-naming.VariableCase
1919
value: lower_case
2020
- key: readability-identifier-naming.GlobalConstantCase
21-
value: UPPER_CASE
21+
value: lower_case
2222
- key: readability-identifier-naming.MacroDefinitionCase
2323
value: UPPER_CASE
2424
- key: readability-identifier-naming.FunctionCase

.github/workflows/build.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
fail-fast: false
2121
matrix:
2222
os: [ubuntu-latest, macos-latest]
23+
cc: [gcc, clang]
24+
exclude:
25+
- os: macos-latest
26+
cc: gcc
2327

2428
steps:
2529
- name: Checkout code
@@ -38,6 +42,10 @@ jobs:
3842
build-essential \
3943
libfyaml-dev
4044
45+
if [ "${{ matrix.cc }}" = "clang" ]; then
46+
sudo apt-get install -y clang
47+
fi
48+
4149
- name: Install dependencies (macOS)
4250
if: runner.os == 'macOS'
4351
run: |
@@ -53,7 +61,7 @@ jobs:
5361
run: ./autogen.sh
5462

5563
- name: Configure
56-
run: ./configure
64+
run: ./configure CC=${{ matrix.cc }}
5765

5866
- name: Build
5967
run: make ${{ github.event.inputs.make_flags }}

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ AM_CFLAGS = -fvisibility=hidden
22
AM_CPPFLAGS = -I$(top_srcdir)/src # so that tests also find header files
33
ACLOCAL_AMFLAGS = -Im4
44

5-
src_files = src/block.c src/event.c src/info.c src/parse.c src/parse_util.c src/yaml.c
6-
src_headers = src/block.h src/event.h src/info.h src/parse.h src/parse_util.h src/yaml.h
5+
src_files = src/block.c src/event.c src/info.c src/parse.c src/parse_util.c src/stream.c src/yaml.c
6+
src_headers = src/block.h src/event.h src/info.h src/parse.h src/parse_util.h src/stream.h src/yaml.h
77

88
noinst_HEADERS = $(src_headers)
99

configure.ac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,14 @@ AS_IF([test "x$asdf_build_tool" = "xyes"], [
102102

103103
AC_CONFIG_HEADERS([config.h]) # use config.h instead of passing -D in the command line
104104
AC_CONFIG_FILES([Makefile tests/Makefile])
105+
105106
AC_OUTPUT
107+
108+
# Report
109+
CC_VERSION=`$CC --version | head -n 1`
110+
FYAML_VERSION=`$PKG_CONFIG --modversion libfyaml`
111+
printf "\n==================== Configuration Summary ====================\n"
112+
printf " %18s %s\n" "$PACKAGE_NAME version:" "$VERSION"
113+
printf " %18s %s\n" "compiler version:" "$CC_VERSION"
114+
printf " %18s %s\n" "libfyaml version:" "$FYAML_VERSION"
115+
printf "===============================================================\n\n"

src/block.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
*/
66

77

8-
const unsigned char ASDF_BLOCK_MAGIC[] = {'\xd3', 'B', 'L', 'K', '\x00'};
8+
const unsigned char asdf_block_magic[] = {'\xd3', 'B', 'L', 'K'};

src/block.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include "config.h"
99
#endif
1010

11+
#include <stdbool.h>
1112
#include <stdint.h>
13+
#include <string.h>
1214
#include <sys/types.h>
1315

1416

@@ -22,12 +24,12 @@
2224
#define ASDF_BLOCK_FLAGS_OFFSET 0
2325
#define ASDF_BLOCK_COMPRESSION_OFFSET 4
2426
#define ASDF_BLOCK_ALLOCATED_SIZE_OFFSET 8
25-
#define ASDF_BLOCK_DATA_SIZE_OFFSET 16
26-
#define ASDF_BLOCK_USED_SIZE_OFFSET 24
27+
#define ASDF_BLOCK_USED_SIZE_OFFSET 16
28+
#define ASDF_BLOCK_DATA_SIZE_OFFSET 24
2729
#define ASDF_BLOCK_CHECKSUM_OFFSET 32
2830

2931

30-
extern const unsigned char ASDF_BLOCK_MAGIC[];
32+
extern const unsigned char asdf_block_magic[];
3133

3234

3335
typedef struct asdf_block_header {
@@ -65,3 +67,14 @@ typedef struct asdf_block_info {
6567
off_t header_pos;
6668
off_t data_pos;
6769
} asdf_block_info_t;
70+
71+
72+
/**
73+
* Returns `true` if the given buffer begins with the ASDF block magic
74+
*/
75+
static inline bool is_block_magic(const char *buf, size_t len) {
76+
if (len < ASDF_BLOCK_MAGIC_SIZE)
77+
return false;
78+
79+
return memcmp(buf, asdf_block_magic, (size_t)ASDF_BLOCK_MAGIC_SIZE) == 0;
80+
}

src/event.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
#include <assert.h>
55
#include <inttypes.h>
6+
#include <limits.h>
67
#include <stdio.h>
78
#include <string.h>
89

@@ -59,7 +60,7 @@ int asdf_event_iterate(asdf_parser_t *parser, asdf_event_t *event) {
5960

6061
const char *asdf_event_type_name(asdf_event_type_t event_type) {
6162
if (event_type >= 0 && event_type < ASDF_EVENT_TYPE_COUNT)
62-
return ASDF_EVENT_TYPE_NAMES[event_type];
63+
return asdf_event_type_names[event_type];
6364

6465
return "ASDF_UNKNOWN_EVENT";
6566
}
@@ -107,28 +108,51 @@ void asdf_event_print(const asdf_event_t *event, FILE *file, bool verbose) {
107108
}
108109

109110
case ASDF_TREE_START_EVENT:
110-
fprintf(file, " Tree start position: %zu (0x%zx)\n", event->payload.tree->start,
111-
event->payload.tree->start);
111+
fprintf(
112+
file,
113+
" Tree start position: %zu (0x%zx)\n",
114+
event->payload.tree->start,
115+
event->payload.tree->start);
112116
break;
113117

114118
case ASDF_TREE_END_EVENT:
115-
fprintf(file, " Tree end position: %zu (0x%zx)\n", event->payload.tree->end,
116-
event->payload.tree->end);
119+
fprintf(
120+
file,
121+
" Tree end position: %zu (0x%zx)\n",
122+
event->payload.tree->end,
123+
event->payload.tree->end);
124+
if (event->payload.tree->buf) {
125+
size_t tree_size = event->payload.tree->end - event->payload.tree->start - 1;
126+
fprintf(
127+
file,
128+
"%.*s\n",
129+
(int)(tree_size > INT_MAX ? INT_MAX : tree_size),
130+
event->payload.tree->buf);
131+
}
117132
break;
118133

119134
case ASDF_BLOCK_EVENT: {
120135
const asdf_block_info_t *block = event->payload.block;
121136
const asdf_block_header_t header = block->header;
122-
fprintf(file, " Header position: %" PRId64 " (0x%" PRIx64 ")\n",
123-
(int64_t)block->header_pos, (int64_t)block->header_pos);
124-
fprintf(file, " Data position: %" PRId64 " (0x%" PRIx64 ")\n",
125-
(int64_t)block->data_pos, (int64_t)block->data_pos);
126-
fprintf(file, " Allocated size: %" PRIu64 " (0x%" PRIx64 ")\n",
127-
header.allocated_size, header.allocated_size);
128-
fprintf(file, " Used size: %" PRIu64 " (0x%" PRIx64 ")\n", header.used_size,
129-
header.used_size);
130-
fprintf(file, " Data size: %" PRIu64 " (0x%" PRIx64 ")\n", header.data_size,
131-
header.data_size);
137+
fprintf(
138+
file,
139+
" Header position: %" PRId64 " (0x%" PRIx64 ")\n",
140+
(int64_t)block->header_pos,
141+
(int64_t)block->header_pos);
142+
fprintf(
143+
file,
144+
" Data position: %" PRId64 " (0x%" PRIx64 ")\n",
145+
(int64_t)block->data_pos,
146+
(int64_t)block->data_pos);
147+
fprintf(
148+
file,
149+
" Allocated size: %" PRIu64 " (0x%" PRIx64 ")\n",
150+
header.allocated_size,
151+
header.allocated_size);
152+
fprintf(
153+
file, " Used size: %" PRIu64 " (0x%" PRIx64 ")\n", header.used_size, header.used_size);
154+
fprintf(
155+
file, " Data size: %" PRIu64 " (0x%" PRIx64 ")\n", header.data_size, header.data_size);
132156

133157
if (header.compression[0] != '\0')
134158
fprintf(file, " Compression: %.4s\n", header.compression);
@@ -152,9 +176,6 @@ void asdf_event_destroy(asdf_parser_t *parser, asdf_event_t *event) {
152176
switch (event->type) {
153177
case ASDF_TREE_START_EVENT:
154178
case ASDF_TREE_END_EVENT:
155-
if (event->payload.tree)
156-
free(event->payload.tree->buf);
157-
158179
free(event->payload.tree);
159180
break;
160181
case ASDF_YAML_EVENT:

src/event.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef enum {
3737
} asdf_event_type_t;
3838

3939

40-
static const char *const ASDF_EVENT_TYPE_NAMES[] = {
40+
static const char *const asdf_event_type_names[] = {
4141
#define X(name) #name,
4242
ASDF_EVENT_TYPES(X)
4343
#undef X
@@ -52,10 +52,13 @@ typedef struct {
5252
typedef struct {
5353
size_t start;
5454
size_t end;
55-
char *buf;
55+
const char *buf;
5656
} asdf_tree_info_t;
5757

5858

59+
typedef struct fy_event asdf_yaml_event_t;
60+
61+
5962
typedef struct asdf_event {
6063
asdf_event_type_t type;
6164
union {

src/info.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,25 +449,24 @@ void print_block(FILE *file, const asdf_event_t *event, size_t block_idx) {
449449
}
450450

451451

452-
static const asdf_info_cfg_t ASDF_INFO_DEFAULT_CFG = {
452+
static const asdf_info_cfg_t asdf_info_default_cfg = {
453453
.filename = NULL, .print_tree = true, .print_blocks = false};
454454

455455

456456
int asdf_info(FILE *in_file, FILE *out_file, const asdf_info_cfg_t *cfg) {
457457
asdf_parser_t parser = {0};
458458

459459
if (!cfg)
460-
cfg = &ASDF_INFO_DEFAULT_CFG;
460+
cfg = &asdf_info_default_cfg;
461461

462462
// Current implementation needs YAML events unless no-tree
463463
asdf_parser_cfg_t parser_cfg = {
464-
.flags = cfg->print_tree ? ASDF_PARSER_OPT_EMIT_YAML_EVENTS : 0
465-
};
464+
.flags = cfg->print_tree ? ASDF_PARSER_OPT_EMIT_YAML_EVENTS : 0};
466465

467466
if (asdf_parser_init(&parser, &parser_cfg) != 0)
468467
return 1;
469468

470-
if (asdf_parser_set_input_file(&parser, in_file, cfg->filename) != 0) {
469+
if (asdf_parser_set_input_fp(&parser, in_file, cfg->filename) != 0) {
471470
asdf_parser_destroy(&parser);
472471
return 1;
473472
}
@@ -500,6 +499,14 @@ int asdf_info(FILE *in_file, FILE *out_file, const asdf_info_cfg_t *cfg) {
500499
}
501500
}
502501

502+
if (asdf_parser_has_error(&parser)) {
503+
const char *error = asdf_parser_get_error(&parser);
504+
// TODO: (#5) Better error formatting / probably go through logging system
505+
fprintf(stderr, "error: %s\n", error);
506+
asdf_parser_destroy(&parser);
507+
return 1;
508+
}
509+
503510
asdf_parser_destroy(&parser);
504511
return 0;
505512
}

0 commit comments

Comments
 (0)