source: src/router/jansson/doc/github_commits.c @ 17909

Last change on this file since 17909 was 17909, checked in by BrainSlayer, 18 months ago

replace with new version

File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3 *
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8#include <stdlib.h>
9#include <string.h>
10
11#include <jansson.h>
12#include <curl/curl.h>
13
14#define BUFFER_SIZE  (256 * 1024)  /* 256 KB */
15
16#define URL_FORMAT   "http://github.com/api/v2/json/commits/list/%s/%s/master"
17#define URL_SIZE     256
18
19/* Return the offset of the first newline in text or the length of
20   text if there's no newline */
21static int newline_offset(const char *text)
22{
23    const char *newline = strchr(text, '\n');
24    if(!newline)
25        return strlen(text);
26    else
27        return (int)(newline - text);
28}
29
30struct write_result
31{
32    char *data;
33    int pos;
34};
35
36static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
37{
38    struct write_result *result = (struct write_result *)stream;
39
40    if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
41    {
42        fprintf(stderr, "error: too small buffer\n");
43        return 0;
44    }
45
46    memcpy(result->data + result->pos, ptr, size * nmemb);
47    result->pos += size * nmemb;
48
49    return size * nmemb;
50}
51
52static char *request(const char *url)
53{
54    CURL *curl;
55    CURLcode status;
56    char *data;
57    long code;
58
59    curl = curl_easy_init();
60    data = malloc(BUFFER_SIZE);
61    if(!curl || !data)
62        return NULL;
63
64    struct write_result write_result = {
65        .data = data,
66        .pos = 0
67    };
68
69    curl_easy_setopt(curl, CURLOPT_URL, url);
70    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
71    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
72
73    status = curl_easy_perform(curl);
74    if(status != 0)
75    {
76        fprintf(stderr, "error: unable to request data from %s:\n", url);
77        fprintf(stderr, "%s\n", curl_easy_strerror(status));
78        return NULL;
79    }
80
81    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
82    if(code != 200)
83    {
84        fprintf(stderr, "error: server responded with code %ld\n", code);
85        return NULL;
86    }
87
88    curl_easy_cleanup(curl);
89    curl_global_cleanup();
90
91    /* zero-terminate the result */
92    data[write_result.pos] = '\0';
93
94    return data;
95}
96
97int main(int argc, char *argv[])
98{
99    size_t i;
100    char *text;
101    char url[URL_SIZE];
102
103    json_t *root;
104    json_error_t error;
105    json_t *commits;
106
107    if(argc != 3)
108    {
109        fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
110        fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
111        return 2;
112    }
113
114    snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);
115
116    text = request(url);
117    if(!text)
118        return 1;
119
120    root = json_loads(text, 0, &error);
121    free(text);
122
123    if(!root)
124    {
125        fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
126        return 1;
127    }
128
129    commits = json_object_get(root, "commits");
130    if(!json_is_array(commits))
131    {
132        fprintf(stderr, "error: commits is not an array\n");
133        return 1;
134    }
135
136    for(i = 0; i < json_array_size(commits); i++)
137    {
138        json_t *commit, *id, *message;
139        const char *message_text;
140
141        commit = json_array_get(commits, i);
142        if(!json_is_object(commit))
143        {
144            fprintf(stderr, "error: commit %d is not an object\n", i + 1);
145            return 1;
146        }
147
148        id = json_object_get(commit, "id");
149        if(!json_is_string(id))
150        {
151            fprintf(stderr, "error: commit %d: id is not a string\n", i + 1);
152            return 1;
153        }
154
155        message = json_object_get(commit, "message");
156        if(!json_is_string(message))
157        {
158            fprintf(stderr, "error: commit %d: message is not a string\n", i + 1);
159            return 1;
160        }
161
162        message_text = json_string_value(message);
163        printf("%.8s %.*s\n",
164               json_string_value(id),
165               newline_offset(message_text),
166               message_text);
167    }
168
169    json_decref(root);
170    return 0;
171}
Note: See TracBrowser for help on using the repository browser.