refactor: improve code using cppcheck
This commit is contained in:
@@ -8,9 +8,8 @@
|
||||
|
||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
||||
|
||||
void parse_command(tgbot_s *bot, tgbot_update_s *update);
|
||||
void parse_command(const tgbot_s *bot, const tgbot_update_s *update);
|
||||
void sighandler(int signum);
|
||||
void callback_parser(tgbot_s *bot, tgbot_cbquery_s *query);
|
||||
|
||||
bool run = true;
|
||||
tgbot_s *bot;
|
||||
@@ -40,7 +39,7 @@ void sighandler(int signum) {
|
||||
run = false;
|
||||
}
|
||||
|
||||
void parse_command(tgbot_s *bot, tgbot_update_s *update) {
|
||||
void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||
tgbot_rc ret;
|
||||
|
||||
if (strcmp("/start", update->text) == 0) {
|
||||
@@ -64,7 +63,6 @@ int main(void) {
|
||||
/* Find "your" way to free the resources */
|
||||
signal(SIGINT, sighandler);
|
||||
|
||||
tgbot_rc ret;
|
||||
char token[256];
|
||||
|
||||
FILE *fp = fopen(".token", "r");
|
||||
@@ -72,7 +70,7 @@ int main(void) {
|
||||
fprintf(stderr, "No .token file found!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fscanf(fp, "%s", token);
|
||||
fscanf(fp, "%255s", token);
|
||||
fprintf(stdout, "Token: %s\n", token);
|
||||
fclose(fp);
|
||||
|
||||
@@ -102,7 +100,7 @@ int main(void) {
|
||||
|
||||
/* Main loop */
|
||||
while (run) {
|
||||
ret = tgbot_get_update(bot, &update, callback_handler);
|
||||
tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler);
|
||||
if (ret != TGBOT_OK) {
|
||||
fprintf(stderr, "tgbot_get_updates()\n");
|
||||
continue;
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
||||
|
||||
/* Methods */
|
||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me);
|
||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
|
||||
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
tgbot_inlinekeyboard_s *reply_markup);
|
||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji);
|
||||
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji);
|
||||
|
||||
/* Updating Methods */
|
||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||
tgbot_inlinekeyboard_s *keyboard);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*
|
||||
* @return Bot pointer.
|
||||
*/
|
||||
tgbot_s *tgbot_new(char *token);
|
||||
tgbot_s *tgbot_new(const char *token);
|
||||
|
||||
/**
|
||||
* @brief Cleans the memory.
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
[constants]
|
||||
vcpkg_base_path = 'C:/vcpkg/'
|
||||
vcpkg_base_install_dir = 'C:/vcpkg/installed/'
|
||||
|
||||
vcpkg_target_triplet = 'x64-windows'
|
||||
vcpkg_host_triplet = 'x64-windows'
|
||||
|
||||
vcpkg_installed_dir = vcpkg_base_install_dir + vcpkg_target_triplet + '/'
|
||||
vcpkg_host_installed_dir = vcpkg_base_install_dir + vcpkg_host_triplet + '/'
|
||||
vcpkg_toolchain_file = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
|
||||
|
||||
[properties]
|
||||
cmake_toolchain_file = vcpkg_base_path + 'scripts/buildsystems/vcpkg.cmake'
|
||||
|
||||
[binaries]
|
||||
vcpkg = [ vcpkg_base_path + 'vcpkg.exe']
|
||||
pkgconfig = [ vcpkg_installed_dir + 'tools/pkgconf/pkgconf.exe']
|
||||
|
||||
[cmake]
|
||||
VCPKG_TARGET_TRIPLET = vcpkg_target_triplet
|
||||
VCPKG_HOST_TRIPLET = vcpkg_host_triplet
|
||||
VCPKG_CHAINLOAD_TOOLCHAIN_FILE = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
|
||||
_VCPKG_INSTALLED_DIR = vcpkg_installed_dir
|
||||
VCPKG_CRT_LINKAGE = 'dynamic'
|
||||
|
||||
[built-in options]
|
||||
pkg_config_path = [ vcpkg_installed_dir + 'lib/pkgconfig;' + vcpkg_installed_dir + 'share/pkgconfig']
|
||||
[constants]
|
||||
vcpkg_base_path = 'C:/vcpkg/'
|
||||
vcpkg_base_install_dir = 'C:/vcpkg/installed/'
|
||||
|
||||
vcpkg_target_triplet = 'x64-windows'
|
||||
vcpkg_host_triplet = 'x64-windows'
|
||||
|
||||
vcpkg_installed_dir = vcpkg_base_install_dir + vcpkg_target_triplet + '/'
|
||||
vcpkg_host_installed_dir = vcpkg_base_install_dir + vcpkg_host_triplet + '/'
|
||||
vcpkg_toolchain_file = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
|
||||
|
||||
[properties]
|
||||
cmake_toolchain_file = vcpkg_base_path + 'scripts/buildsystems/vcpkg.cmake'
|
||||
|
||||
[binaries]
|
||||
vcpkg = [ vcpkg_base_path + 'vcpkg.exe']
|
||||
pkgconfig = [ vcpkg_installed_dir + 'tools/pkgconf/pkgconf.exe']
|
||||
|
||||
[cmake]
|
||||
VCPKG_TARGET_TRIPLET = vcpkg_target_triplet
|
||||
VCPKG_HOST_TRIPLET = vcpkg_host_triplet
|
||||
VCPKG_CHAINLOAD_TOOLCHAIN_FILE = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
|
||||
_VCPKG_INSTALLED_DIR = vcpkg_installed_dir
|
||||
VCPKG_CRT_LINKAGE = 'dynamic'
|
||||
|
||||
[built-in options]
|
||||
pkg_config_path = [ vcpkg_installed_dir + 'lib/pkgconfig;' + vcpkg_installed_dir + 'share/pkgconfig']
|
||||
cmake_prefix_path = [ vcpkg_installed_dir ]
|
||||
@@ -85,7 +85,7 @@ static tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_o
|
||||
return TGBOT_OK;
|
||||
}
|
||||
|
||||
static tgbot_rc tgbot_execute_method(tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) {
|
||||
static tgbot_rc tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) {
|
||||
char url[URL_LEN] = {0};
|
||||
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
||||
|
||||
@@ -128,14 +128,14 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
||||
free(mb->data);
|
||||
free(mb);
|
||||
|
||||
json_object *ok = json_object_object_get(json, "ok");
|
||||
const json_object *ok = json_object_object_get(json, "ok");
|
||||
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
||||
json_object_put(json);
|
||||
|
||||
return TGBOT_TELEGRAM_OK_ERROR;
|
||||
}
|
||||
|
||||
json_object *results = json_object_object_get(json, "result");
|
||||
const json_object *results = json_object_object_get(json, "result");
|
||||
size_t results_len = json_object_array_length(results);
|
||||
|
||||
if (results_len == 0) {
|
||||
@@ -146,7 +146,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
||||
|
||||
/* Check if it is a Message or a CallbackQuery*/
|
||||
json_object *result = json_object_array_get_idx(results, 0);
|
||||
json_object *message = json_object_object_get(result, "message");
|
||||
const json_object *message = json_object_object_get(result, "message");
|
||||
if (message) {
|
||||
tgbot_parse_message(bot, update, result);
|
||||
} else if (cbq_handler != NULL) {
|
||||
@@ -159,7 +159,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
||||
return TGBOT_OK;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
||||
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
|
||||
char url[URL_LEN];
|
||||
snprintf(url, sizeof(url), "%sgetMe", bot->api);
|
||||
|
||||
@@ -176,25 +176,26 @@ tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
||||
free(mb->data);
|
||||
free(mb);
|
||||
|
||||
json_object *ok = json_object_object_get(json, "ok");
|
||||
const json_object *ok = json_object_object_get(json, "ok");
|
||||
if (!json_object_get_boolean(ok)) {
|
||||
json_object_put(json);
|
||||
|
||||
return TGBOT_GETME_ERROR;
|
||||
}
|
||||
|
||||
json_object *result = json_object_object_get(json, "result");
|
||||
const json_object *result = json_object_object_get(json, "result");
|
||||
json_object *first_name = json_object_object_get(result, "first_name");
|
||||
strncpy(me->first_name, json_object_get_string(first_name), sizeof(me->first_name) - 1);
|
||||
snprintf(me->first_name, sizeof(me->first_name), "%s", json_object_get_string(first_name));
|
||||
|
||||
json_object *username = json_object_object_get(result, "username");
|
||||
strncpy(me->username, json_object_get_string(username), sizeof(me->username) - 1);
|
||||
snprintf(me->username, sizeof(me->username), "%s", json_object_get_string(username));
|
||||
|
||||
json_object_put(json);
|
||||
|
||||
return TGBOT_OK;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
tgbot_inlinekeyboard_s *keyboard) {
|
||||
tgbot_option_s options[4] = {
|
||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||
@@ -207,7 +208,7 @@ tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, con
|
||||
: TGBOT_SENDMESSAGE_ERROR;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||
tgbot_inlinekeyboard_s *keyboard) {
|
||||
tgbot_option_s options[4] = {
|
||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||
@@ -221,7 +222,7 @@ tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id,
|
||||
: TGBOT_EDITMESSAGETEXT_ERROR;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
||||
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
||||
tgbot_option_s options[2] = {
|
||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||
{"emoji", (void *)emoji, tgbot_opt_string},
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "tgbot.h"
|
||||
|
||||
tgbot_s *tgbot_new(char *token) {
|
||||
tgbot_s *tgbot_new(const char *token) {
|
||||
tgbot_s *bot = malloc(sizeof(tgbot_s));
|
||||
if (!bot) {
|
||||
return NULL;
|
||||
|
||||
Reference in New Issue
Block a user