betaur

[WIP] A better AUR helper written in C

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
#include <aur/request.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

betaur_searchby_t betaur_str_to_searchby(size_t searchby_len, const char* searchby) {
#define X(ENUM, STR) \
	if (searchby == #STR) \
		return ENUM;

	BETAUR_SEARCHBY_MAP(X)
#undef X
	return BETAUR_INVALID;
}

const char* betaur_searchby_to_str(betaur_searchby_t by) {
	switch(by) {
#define X(ENUM, STR) \
		case ENUM: \
			return #STR; \
			break;

		BETAUR_SEARCHBY_MAP(X)
#undef X
		default:
			return "";
	}
}


bool betaur_http_search(betaur_searchby_t by, size_t arg_len, char* arg) {
	// TODO stub: implement
#define BETAUR_SEARCH_API_STR "/rpc/v5/search/%.*s?by=%.*s"
#define BETAUR_SEARCH_API_LEN (sizeof(BETAUR_SEARCH_API_STR) - 8)
	char* final_string;
	const char* bystr = betaur_searchby_to_str(by);
	size_t bystr_len = strlen(bystr);
	size_t final_string_len = BETAUR_SEARCH_API_LEN + bystr_len + arg_len;
	final_string = (char*) malloc(final_string_len);

	if( snprintf(final_string, final_string_len, BETAUR_SEARCH_API_STR, bystr_len, bystr, arg_len, arg) != final_string_len - 1) {
		printf("FATAL: ERrro string not matching: %d\n", final_string_len);
		return false;
		// TODO throw error
	}
	final_string[final_string_len] = '\0';

	// TODO call curl

	free(final_string);
	return false;
}

#undef BETAUR_SEARCHBY_MAP