aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2019-10-17 09:08:51 +0300
committerPetri Lehtinen <petri@digip.org>2019-10-21 08:55:01 +0300
commit79fe8c343572ea6a75c754a15bea8a94c7429265 (patch)
tree7caa8d66fdee16bb744847a010e902df0eeda122 /doc
parent7dc463ee4ee4d9b42ad4d914ac4caf734ecc56ae (diff)
downloadjansson-79fe8c343572ea6a75c754a15bea8a94c7429265.zip
jansson-79fe8c343572ea6a75c754a15bea8a94c7429265.tar.gz
jansson-79fe8c343572ea6a75c754a15bea8a94c7429265.tar.bz2
Run clang-format on *.c, *.h
Diffstat (limited to 'doc')
-rw-r--r--doc/github_commits.c94
1 files changed, 39 insertions, 55 deletions
diff --git a/doc/github_commits.c b/doc/github_commits.c
index a9a84d8..09923cd 100644
--- a/doc/github_commits.c
+++ b/doc/github_commits.c
@@ -8,37 +8,34 @@
#include <stdlib.h>
#include <string.h>
-#include <jansson.h>
#include <curl/curl.h>
+#include <jansson.h>
-#define BUFFER_SIZE (256 * 1024) /* 256 KB */
+#define BUFFER_SIZE (256 * 1024) /* 256 KB */
-#define URL_FORMAT "https://api.github.com/repos/%s/%s/commits"
-#define URL_SIZE 256
+#define URL_FORMAT "https://api.github.com/repos/%s/%s/commits"
+#define URL_SIZE 256
/* Return the offset of the first newline in text or the length of
text if there's no newline */
-static int newline_offset(const char *text)
-{
+static int newline_offset(const char *text) {
const char *newline = strchr(text, '\n');
- if(!newline)
+ if (!newline)
return strlen(text);
else
return (int)(newline - text);
}
-struct write_result
-{
+struct write_result {
char *data;
int pos;
};
-static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
-{
+static size_t write_response(void *ptr, size_t size, size_t nmemb,
+ void *stream) {
struct write_result *result = (struct write_result *)stream;
- if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
- {
+ if (result->pos + size * nmemb >= BUFFER_SIZE - 1) {
fprintf(stderr, "error: too small buffer\n");
return 0;
}
@@ -49,8 +46,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
return size * nmemb;
}
-static char *request(const char *url)
-{
+static char *request(const char *url) {
CURL *curl = NULL;
CURLcode status;
struct curl_slist *headers = NULL;
@@ -59,17 +55,14 @@ static char *request(const char *url)
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
- if(!curl)
+ if (!curl)
goto error;
data = malloc(BUFFER_SIZE);
- if(!data)
+ if (!data)
goto error;
- struct write_result write_result = {
- .data = data,
- .pos = 0
- };
+ struct write_result write_result = {.data = data, .pos = 0};
curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -81,16 +74,14 @@ static char *request(const char *url)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);
- if(status != 0)
- {
+ if (status != 0) {
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
goto error;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
- if(code != 200)
- {
+ if (code != 200) {
fprintf(stderr, "error: server responded with code %ld\n", code);
goto error;
}
@@ -105,18 +96,17 @@ static char *request(const char *url)
return data;
error:
- if(data)
+ if (data)
free(data);
- if(curl)
+ if (curl)
curl_easy_cleanup(curl);
- if(headers)
+ if (headers)
curl_slist_free_all(headers);
curl_global_cleanup();
return NULL;
}
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
size_t i;
char *text;
char url[URL_SIZE];
@@ -124,8 +114,7 @@ int main(int argc, char *argv[])
json_t *root;
json_error_t error;
- if(argc != 3)
- {
+ if (argc != 3) {
fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
return 2;
@@ -134,66 +123,61 @@ int main(int argc, char *argv[])
snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
text = request(url);
- if(!text)
+ if (!text)
return 1;
root = json_loads(text, 0, &error);
free(text);
- if(!root)
- {
+ if (!root) {
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return 1;
}
- if(!json_is_array(root))
- {
+ if (!json_is_array(root)) {
fprintf(stderr, "error: root is not an array\n");
json_decref(root);
return 1;
}
- for(i = 0; i < json_array_size(root); i++)
- {
+ for (i = 0; i < json_array_size(root); i++) {
json_t *data, *sha, *commit, *message;
const char *message_text;
data = json_array_get(root, i);
- if(!json_is_object(data))
- {
- fprintf(stderr, "error: commit data %d is not an object\n", (int)(i + 1));
+ if (!json_is_object(data)) {
+ fprintf(stderr, "error: commit data %d is not an object\n",
+ (int)(i + 1));
json_decref(root);
return 1;
}
sha = json_object_get(data, "sha");
- if(!json_is_string(sha))
- {
- fprintf(stderr, "error: commit %d: sha is not a string\n", (int)(i + 1));
+ if (!json_is_string(sha)) {
+ fprintf(stderr, "error: commit %d: sha is not a string\n",
+ (int)(i + 1));
return 1;
}
commit = json_object_get(data, "commit");
- if(!json_is_object(commit))
- {
- fprintf(stderr, "error: commit %d: commit is not an object\n", (int)(i + 1));
+ if (!json_is_object(commit)) {
+ fprintf(stderr, "error: commit %d: commit is not an object\n",
+ (int)(i + 1));
json_decref(root);
return 1;
}
message = json_object_get(commit, "message");
- if(!json_is_string(message))
- {
- fprintf(stderr, "error: commit %d: message is not a string\n", (int)(i + 1));
+ if (!json_is_string(message)) {
+ fprintf(stderr, "error: commit %d: message is not a string\n",
+ (int)(i + 1));
json_decref(root);
return 1;
}
message_text = json_string_value(message);
- printf("%.8s %.*s\n",
- json_string_value(sha),
- newline_offset(message_text),
- message_text);
+ printf("%.8s %.*s\n", json_string_value(sha),
+ newline_offset(message_text), message_text);
}
json_decref(root);