Skip to content

Commit

Permalink
Merge pull request #440 from ZettaScaleLabs/enforce_clang_format
Browse files Browse the repository at this point in the history
Enforce clang formatting
  • Loading branch information
milyin authored Jun 11, 2024
2 parents 3d95166 + f62719c commit 1c68cbb
Show file tree
Hide file tree
Showing 29 changed files with 889 additions and 1,047 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ on:
- cron: "0 6 * * 1-5"

jobs:
check_format:
name: Check codebase format with clang-format
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run clang-format dry-run
run: find include/ src/ tests/ examples/ -iname -o -iname "*.h" -o -iname "*.c" | xargs clang-format -n -Werror

build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions examples/z_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

#include "zenoh.h"

int main(int argc, char **argv) {
Expand Down
11 changes: 5 additions & 6 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,20 @@ int main(int argc, char **argv) {
z_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
z_move(opts)); // here, the closure is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;
for (bool has_more = z_try_recv(z_loan(handler), &reply); has_more; has_more = z_try_recv(z_loan(handler), &reply)) {
for (bool has_more = z_try_recv(z_loan(handler), &reply); has_more;
has_more = z_try_recv(z_loan(handler), &reply)) {
if (!z_check(reply)) {
z_sleep_ms(50);
continue;
}
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply));
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_view_string_t key_str;
z_owned_string_t payload_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_str);
z_bytes_decode_into_string(z_sample_payload(sample), &payload_string);
printf(">> Received ('%.*s': '%.*s')\n",
(int)z_string_len(z_loan(key_str)), z_string_data(z_loan(key_str)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))
);
printf(">> Received ('%.*s': '%.*s')\n", (int)z_string_len(z_loan(key_str)), z_string_data(z_loan(key_str)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
} else {
printf("Received an error\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(int argc, char** argv) {
data[i] = i % 10;
}
z_owned_bytes_t payload;

z_mutex_lock(z_loan_mut(mutex));
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
Expand Down
17 changes: 8 additions & 9 deletions examples/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef struct kv_pairs_t {
} kv_pairs_t;

bool create_attachment_iter(z_owned_bytes_t* kv_pair, void* context) {
kv_pairs_t *kvs = (kv_pairs_t*)(context);
kv_pairs_t* kvs = (kv_pairs_t*)(context);
z_owned_bytes_t k, v;
if (kvs->current_idx >= kvs->len) {
return false;
Expand All @@ -41,10 +41,9 @@ bool create_attachment_iter(z_owned_bytes_t* kv_pair, void* context) {
}
};


int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-pub";
char *value = "Pub from C!";
int main(int argc, char** argv) {
char* keyexpr = "demo/example/zenoh-c-pub";
char* value = "Pub from C!";

if (argc > 1) keyexpr = argv[1];
if (argc > 2) value = argv[2];
Expand Down Expand Up @@ -82,7 +81,7 @@ int main(int argc, char **argv) {

// allocate attachment data
kv_pair_t kvs[2];
kvs[0] = (kv_pair_t){ .key = "source", .value = "C" };
kvs[0] = (kv_pair_t){.key = "source", .value = "C"};
// allocate attachment and payload
z_owned_bytes_t attachment;
z_owned_bytes_t payload;
Expand All @@ -94,14 +93,14 @@ int main(int argc, char **argv) {

// add some other attachment value
sprintf(buf_ind, "%d", idx);
kvs[1] = (kv_pair_t){ .key = "index", .value = buf_ind };
kv_pairs_t ctx = (kv_pairs_t) { .data = kvs, .current_idx = 0, .len = 2 };
kvs[1] = (kv_pair_t){.key = "index", .value = buf_ind};
kv_pairs_t ctx = (kv_pairs_t){.data = kvs, .current_idx = 0, .len = 2};
z_bytes_encode_from_iter(&attachment, create_attachment_iter, (void*)&ctx);
options.attachment = &attachment;

sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);

z_bytes_encode_from_string(&payload, buf);
z_publisher_put(z_loan(pub), z_move(payload), &options);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char **argv) {
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);
z_owned_bytes_t payload;
z_bytes_encode_from_string(&payload, buf);

z_put(z_loan(s), z_loan(ke), z_move(payload), NULL);
}

Expand Down
9 changes: 4 additions & 5 deletions examples/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
#include <stdio.h>

#include "zenoh.h"

const char *kind_to_str(z_sample_kind_t kind) {
Expand All @@ -30,11 +31,9 @@ void handle_sample(const z_loaned_sample_t *sample) {
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t payload_value;
z_bytes_decode_into_string(z_sample_payload(sample), &payload_value);
printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n",
kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(keystr)), z_string_data(z_loan(keystr)),
(int)z_string_len(z_loan(payload_value)), z_string_data(z_loan(payload_value))
);
printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(keystr)), z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(payload_value)),
z_string_data(z_loan(payload_value)));
z_drop(z_move(payload_value));
}

Expand Down
6 changes: 3 additions & 3 deletions examples/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ int main(int argc, char **argv) {

z_owned_bytes_t payload;
z_bytes_encode_from_string(&payload, value);

z_owned_bytes_t attachment, key, val;
z_bytes_encode_from_string(&key, "hello");
z_bytes_encode_from_string(&val, "there");
z_bytes_encode_from_pair(&attachment, z_move(key), z_move(val));

z_put_options_t options;
z_put_options_default(&options);
options.attachment = &attachment; // attachement is going to be consumed by z_put, so no need to drop it manually
options.attachment = &attachment; // attachement is going to be consumed by z_put, so no need to drop it manually

int res = z_put(z_loan(s), z_loan(ke), z_move(payload), &options);
if (res < 0) {
printf("Put failed...\n");
Expand Down
8 changes: 4 additions & 4 deletions examples/z_query_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
#include <stdio.h>

#include "zenoh.h"

const char *kind_to_str(z_sample_kind_t kind);
Expand All @@ -23,10 +24,9 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_sample_payload(sample), &payload_string);

printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))
);
printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}

Expand Down
19 changes: 9 additions & 10 deletions examples/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

#include "zenoh.h"

const char *keyexpr = "demo/example/zenoh-c-queryable";
Expand All @@ -26,24 +27,22 @@ void query_handler(const z_loaned_query_t *query, void *context) {
z_view_string_t params;
z_query_parameters(query, &params);

const z_loaned_bytes_t* payload = z_value_payload(z_query_value(query));
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);

printf(">> [Queryable ] Received Query '%.*s?%.*s' with value '%.*s'\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(params)), z_string_data(z_loan(params)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
printf(">> [Queryable ] Received Query '%.*s?%.*s' with value '%.*s'\n", (int)z_string_len(z_loan(key_string)),
z_string_data(z_loan(key_string)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
} else {
printf(">> [Queryable ] Received Query '%.*s?%.*s'\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(params)), z_string_data(z_loan(params)));
printf(">> [Queryable ] Received Query '%.*s?%.*s'\n", (int)z_string_len(z_loan(key_string)),
z_string_data(z_loan(key_string)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params)));
}
z_query_reply_options_t options;
z_query_reply_options_default(&options);

z_owned_bytes_t reply_payload;
z_bytes_encode_from_string(&reply_payload, value);

Expand Down Expand Up @@ -83,7 +82,7 @@ int main(int argc, char **argv) {

printf("Declaring Queryable on '%s'...\n", keyexpr);
z_owned_closure_query_t callback;
z_closure(&callback, query_handler, NULL, (void*)keyexpr);
z_closure(&callback, query_handler, NULL, (void *)keyexpr);
z_owned_queryable_t qable;

if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) {
Expand Down
24 changes: 11 additions & 13 deletions examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdio.h>
#include <string.h>
#include <zenoh_macros.h>

#include "zenoh.h"

const char *keyexpr = "demo/example/zenoh-c-queryable";
Expand Down Expand Up @@ -42,7 +43,7 @@ int main(int argc, char **argv) {
printf("Unable to open session!\n");
exit(-1);
}

if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) {
printf("%s is not a valid key expression", keyexpr);
exit(-1);
Expand All @@ -54,7 +55,7 @@ int main(int argc, char **argv) {
z_fifo_channel_query_new(&closure, &handler, 16);
z_owned_closure_query_t callback;
z_owned_queryable_t qable;

if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) {
printf("Unable to create queryable.\n");
exit(-1);
Expand All @@ -63,29 +64,26 @@ int main(int argc, char **argv) {
printf("^C to quit...\n");
z_owned_query_t oquery;
for (z_recv(z_loan(handler), &oquery); z_check(oquery); z_recv(z_loan(handler), &oquery)) {
const z_loaned_query_t* query = z_loan(oquery);
const z_loaned_query_t *query = z_loan(oquery);
z_view_string_t key_string;
z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string);

z_view_string_t params;
z_query_parameters(query, &params);

const z_loaned_bytes_t* payload = z_value_payload(z_query_value(query));
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);

printf(">> [Queryable ] Received Query '%.*s?%.*s' with value '%.*s'\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(params)), z_string_data(z_loan(params)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))
);
printf(">> [Queryable ] Received Query '%.*s?%.*s' with value '%.*s'\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(params)), z_string_data(z_loan(params)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
} else {
printf(">> [Queryable ] Received Query '%.*s?%.*s'\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(params)), z_string_data(z_loan(params))
);
printf(">> [Queryable ] Received Query '%.*s?%.*s'\n", (int)z_string_len(z_loan(key_string)),
z_string_data(z_loan(key_string)), (int)z_string_len(z_loan(params)), z_string_data(z_loan(params)));
}
z_query_reply_options_t options;
z_query_reply_options_default(&options);
Expand Down
3 changes: 2 additions & 1 deletion examples/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>

#include <stdio.h>

#include "zenoh.h"

void fprintpid(FILE *stream, z_id_t pid) {
Expand Down Expand Up @@ -52,7 +53,7 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
fprintf(stream, "]");
}

void fprinthello(FILE *stream, const z_loaned_hello_t* hello) {
void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { pid: ");
fprintpid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
Expand Down
6 changes: 3 additions & 3 deletions examples/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
#include <stdio.h>

#include "zenoh.h"

const char *kind_to_str(z_sample_kind_t kind);
Expand All @@ -24,9 +25,8 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_bytes_decode_into_string(z_sample_payload(sample), &payload_string);

printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))
);
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}

Expand Down
11 changes: 5 additions & 6 deletions examples/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
#include <stdint.h>
#include <stdio.h>

#include "zenoh.h"

const char *kind_to_str(z_sample_kind_t kind);
Expand All @@ -25,11 +26,10 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_bytes_decode_into_string(z_sample_payload(sample), &payload_string);

printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)),
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))
);
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)),
(int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string)));

const z_loaned_bytes_t* attachment = z_sample_attachment(sample);
const z_loaned_bytes_t *attachment = z_sample_attachment(sample);
// checks if attachment exists
if (attachment != NULL) {
// reads full attachment
Expand All @@ -44,8 +44,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_bytes_decode_into_string(z_loan(v), &value);

printf(" attachment: %.*s: '%.*s'\n", (int)z_string_len(z_loan(key)), z_string_data(z_loan(key)),
(int)z_string_len(z_loan(value)), z_string_data(z_loan(value))
);
(int)z_string_len(z_loan(value)), z_string_data(z_loan(value)));
z_drop(z_move(kv));
z_drop(z_move(k));
z_drop(z_move(v));
Expand Down
11 changes: 5 additions & 6 deletions examples/z_sub_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
#include <stdio.h>

#include "zenoh.h"

void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_view_string_t key_string;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string);
switch (z_sample_kind(sample)) {
case Z_SAMPLE_KIND_PUT:
printf(">> [LivelinessSubscriber] New alive token ('%.*s')\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string))
);
printf(">> [LivelinessSubscriber] New alive token ('%.*s')\n", (int)z_string_len(z_loan(key_string)),
z_string_data(z_loan(key_string)));
break;
case Z_SAMPLE_KIND_DELETE:
printf(">> [LivelinessSubscriber] Dropped token ('%.*s')\n",
(int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string))
);
printf(">> [LivelinessSubscriber] Dropped token ('%.*s')\n", (int)z_string_len(z_loan(key_string)),
z_string_data(z_loan(key_string)));
break;
}
}
Expand Down
Loading

0 comments on commit 1c68cbb

Please sign in to comment.