Permalink
Browse files

Start the project organization

  • Loading branch information...
1 parent fad6045 commit 2a1e2c44cbaffc49f4712009c20f6a8966575530 @jrbasso committed Jul 9, 2012
Showing with 79 additions and 70 deletions.
  1. +1 −1 Makefile.am
  2. +1 −69 src/gistit.c
  3. +67 −0 src/github.c
  4. +10 −0 src/github.h
View
2 Makefile.am
@@ -1,5 +1,5 @@
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
bin_PROGRAMS = gistit
-gistit_SOURCES = src/gistit.c
+gistit_SOURCES = src/gistit.c src/github.c
dist_noinst_SCRIPTS = autogen.sh
View
70 src/gistit.c
@@ -2,21 +2,12 @@
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
-#include <curl/curl.h>
#include "config.h"
+#include "github.h"
#define STDIN_BUFFER_SIZE 1024
-#define ENV_ACCESS_TOKEN_KEY "GISTIT_TOKEN"
-#define GITHUB_GIST_URL "https://api.github.com/gists?access_token=%s"
-#define GITHUB_GIST_URL_ANONYMOUS "https://api.github.com/gists"
-
-struct github_response {
- char *response_text;
- size_t length;
-};
-
struct filelist {
char *filepath;
char *filename;
@@ -93,65 +84,6 @@ char *default_name(const char *content)
return "default.txt";
}
-size_t github_response(void *ptr, size_t size, size_t nmemb, void *stream)
-{
- struct github_response *response = (struct github_response *)stream;
- size_t new_length = response->length + size * nmemb;
-
- response->response_text = realloc(response->response_text, new_length + 1);
- memcpy(response->response_text + response->length, ptr, size * nmemb);
- response->response_text[new_length] = '\0';
- response->length = new_length;
-
- return size * nmemb;
-}
-
-struct github_response *github_submit(json_t *content)
-{
- CURL *curl;
- CURLcode res;
- char url[100], *token;
- struct github_response *response;
- struct curl_slist *headers = NULL;
- long code;
-
- curl = curl_easy_init();
- if (curl) {
- response = (struct github_response *)malloc(1 * sizeof(struct github_response));
- response->response_text = (char *)malloc(1 * sizeof(char));
- response->response_text[0] = '\0';
- response->length = 0;
-
- headers = curl_slist_append(headers, "Content-Type: application/json");
-
- token = getenv(ENV_ACCESS_TOKEN_KEY);
- if (token != NULL) {
- sprintf(url, GITHUB_GIST_URL, token);
- } else {
- sprintf(url, GITHUB_GIST_URL_ANONYMOUS);
- }
-
- curl_easy_setopt(curl, CURLOPT_URL, url);
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_dumps(content, 0));
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, github_response);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
-
- res = curl_easy_perform(curl);
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
- curl_slist_free_all(headers);
- curl_easy_cleanup(curl);
- if (code != 201) {
- printf("Failed to post to GitHub\n- code %ld\n- response %s\n", code, response->response_text);
- free(response->response_text);
- free(response);
- response = NULL;
- }
- }
- return response;
-}
-
struct filelist *filelist_create(char *filepath)
{
struct filelist *list = (struct filelist *)malloc(sizeof(struct filelist));
View
67 src/github.c
@@ -0,0 +1,67 @@
+#include "github.h"
+#include <jansson.h>
+#include <curl/curl.h>
+
+#define GITHUB_GIST_URL "https://api.github.com/gists?access_token=%s"
+#define GITHUB_GIST_URL_ANONYMOUS "https://api.github.com/gists"
+
+#define ENV_ACCESS_TOKEN_KEY "GISTIT_TOKEN"
+
+struct github_response *github_submit(json_t *content)
+{
+ CURL *curl;
+ CURLcode res;
+ char url[100], *token;
+ struct github_response *response;
+ struct curl_slist *headers = NULL;
+ long code;
+
+ curl = curl_easy_init();
+ if (curl) {
+ response = (struct github_response *)malloc(1 * sizeof(struct github_response));
+ response->response_text = (char *)malloc(1 * sizeof(char));
+ response->response_text[0] = '\0';
+ response->length = 0;
+
+ headers = curl_slist_append(headers, "Content-Type: application/json");
+
+ token = getenv(ENV_ACCESS_TOKEN_KEY);
+ if (token != NULL) {
+ sprintf(url, GITHUB_GIST_URL, token);
+ } else {
+ sprintf(url, GITHUB_GIST_URL_ANONYMOUS);
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_POST, 1);
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_dumps(content, 0));
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, github_response);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
+
+ res = curl_easy_perform(curl);
+ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
+ curl_slist_free_all(headers);
+ curl_easy_cleanup(curl);
+ if (code != 201) {
+ printf("Failed to post to GitHub\n- code %ld\n- response %s\n", code, response->response_text);
+ free(response->response_text);
+ free(response);
+ response = NULL;
+ }
+ }
+ return response;
+}
+
+size_t github_response(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ struct github_response *response = (struct github_response *)stream;
+ size_t new_length = response->length + size * nmemb;
+
+ response->response_text = realloc(response->response_text, new_length + 1);
+ memcpy(response->response_text + response->length, ptr, size * nmemb);
+ response->response_text[new_length] = '\0';
+ response->length = new_length;
+
+ return size * nmemb;
+}
View
10 src/github.h
@@ -0,0 +1,10 @@
+#include <string.h>
+#include <jansson.h>
+
+struct github_response {
+ char *response_text;
+ size_t length;
+};
+
+struct github_response *github_submit(json_t *content);
+size_t github_response(void *ptr, size_t size, size_t nmemb, void *stream);

0 comments on commit 2a1e2c4

Please sign in to comment.