reformat all files

This commit is contained in:
Maxim Slipenko 2023-11-29 14:00:52 +03:00
parent f69b73187e
commit 54fa7bb7d0
4 changed files with 31 additions and 29 deletions

View File

@ -10,7 +10,7 @@
#include "yadisk_api_internal.h" #include "yadisk_api_internal.h"
struct memory { struct memory {
char *response; char* response;
size_t size; size_t size;
}; };
@ -19,13 +19,13 @@ struct data {
}; };
static size_t cb(void *data, size_t size, size_t nmemb, void *clientp) static size_t cb(void* data, size_t size, size_t nmemb, void* clientp)
{ {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)clientp; struct memory* mem = (struct memory*)clientp;
char *ptr = realloc(mem->response, mem->size + realsize + 1); char* ptr = realloc(mem->response, mem->size + realsize + 1);
if(ptr == NULL) if (ptr == NULL)
return 0; /* out of memory! */ return 0; /* out of memory! */
mem->response = ptr; mem->response = ptr;
@ -36,18 +36,19 @@ static size_t cb(void *data, size_t size, size_t nmemb, void *clientp)
return realsize; return realsize;
} }
char* build_query_string(query_param *params, size_t num_params) { char* build_query_string(query_param* params, size_t num_params)
CURL *curl = curl_easy_init(); {
CURL* curl = curl_easy_init();
if (!curl) { if (!curl) {
return NULL; return NULL;
} }
char *query = NULL; char* query = NULL;
size_t query_len = 0; size_t query_len = 0;
for (size_t i = 0; i < num_params; ++i) { for (size_t i = 0; i < num_params; ++i) {
char *encoded_key = curl_easy_escape(curl, params[i].key, 0); char* encoded_key = curl_easy_escape(curl, params[i].key, 0);
char *encoded_value = curl_easy_escape(curl, params[i].value, 0); char* encoded_value = curl_easy_escape(curl, params[i].value, 0);
// Calculate additional space needed: key=value& (including null terminator) // Calculate additional space needed: key=value& (including null terminator)
size_t additional_len = strlen(encoded_key) + strlen(encoded_value) + 2; size_t additional_len = strlen(encoded_key) + strlen(encoded_value) + 2;
@ -73,23 +74,24 @@ char* build_query_string(query_param *params, size_t num_params) {
// Function to make an HTTP request using libcurl // Function to make an HTTP request using libcurl
int api_http_request( int api_http_request(
yadisk_api_client *client, yadisk_api_client* client,
const char* method, const char* method,
const char* path, const char* path,
query_param *params, query_param* params,
size_t num_params, size_t num_params,
char** output char** output
) { )
CURL *curl; {
CURL* curl;
CURLcode res; CURLcode res;
struct memory chunk = {0}; struct memory chunk = {0};
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { if (curl) {
char url[512]; char url[512];
if (num_params) { if (num_params) {
char *query_string = build_query_string(params, num_params); char* query_string = build_query_string(params, num_params);
snprintf(url, sizeof(url), "%s%s?%s", YANDEX_DISK_API_HOST, path, query_string); snprintf(url, sizeof(url), "%s%s?%s", YANDEX_DISK_API_HOST, path, query_string);
free(query_string); free(query_string);
} else { } else {
@ -97,7 +99,7 @@ int api_http_request(
} }
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);
// Set the HTTP method // Set the HTTP method
if (strcmp(method, "GET") == 0) { if (strcmp(method, "GET") == 0) {
@ -113,13 +115,13 @@ int api_http_request(
// Set the authorization header // Set the authorization header
char auth_header[256]; char auth_header[256];
snprintf(auth_header, sizeof(auth_header), "Authorization: OAuth %s", client->token); snprintf(auth_header, sizeof(auth_header), "Authorization: OAuth %s", client->token);
struct curl_slist *headers = NULL; struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, auth_header); headers = curl_slist_append(headers, auth_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Perform the request // Perform the request
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if(res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
free(chunk.response); free(chunk.response);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);

View File

@ -14,8 +14,7 @@
const char* created; \ const char* created; \
const char* modified; const char* modified;
struct yadisk_resource_item struct yadisk_resource_item {
{
YADISK_RESOURCE_ITEM_PROPERTIES YADISK_RESOURCE_ITEM_PROPERTIES
}; };

View File

@ -12,4 +12,4 @@ int yadisk_init()
void yadisk_cleanup() void yadisk_cleanup()
{ {
curl_global_cleanup(); curl_global_cleanup();
} }

View File

@ -5,7 +5,8 @@
#include <yadisk.h> #include <yadisk.h>
#include <stdio.h> #include <stdio.h>
int main() { int main()
{
char token[1024]; char token[1024];
printf("Enter OAuth token: "); printf("Enter OAuth token: ");
@ -13,8 +14,8 @@ int main() {
yadisk_init(); yadisk_init();
yadisk_api_client* client = &(yadisk_api_client){ yadisk_api_client* client = &(yadisk_api_client) {
.token = token .token = token
}; };
yadisk_disk_info yadisk_disk_info; yadisk_disk_info yadisk_disk_info;