summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-09-08 23:04:41 +0800
committerꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-09-08 23:04:41 +0800
commit316a8686aac06ca326c65e624559a8f7ae8b73f1 (patch)
tree5b332e1433c2d019da89f137895f7d639f392947
parent56146facdb0390a158169cf60b8fe1f41eccf303 (diff)
Functions to get user info and to free it
-rw-r--r--rtclient.c42
-rw-r--r--rtclient.h3
-rw-r--r--rtuser.h2
3 files changed, 37 insertions, 10 deletions
diff --git a/rtclient.c b/rtclient.c
index 90a0863..2d12314 100644
--- a/rtclient.c
+++ b/rtclient.c
@@ -5,9 +5,8 @@
#include <stdio.h>
#endif // ANDROID
#endif // DEBUG
-#include <stdbool.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
#include <curl/curl.h>
#include "rtclient.h"
@@ -83,8 +82,8 @@ user_callback(void *contents, size_t size, size_t nmemb, void *writedata)
lines[i++] = line;
line = strtok(NULL, "\n");
}
- rt_user *user = (rt_user *)writedata;
- user = malloc(sizeof(rt_user));
+ rt_user **userp = (rt_user **)writedata;
+ rt_user *user = *userp;
for (unsigned short i = 0; i < nproperties; i++) {
char *token = strtok(lines[i], ":");
if (!strcmp(token, "id")) {
@@ -182,7 +181,6 @@ user_callback(void *contents, size_t size, size_t nmemb, void *writedata)
user->disabled = (bool)atoi(++token);
}
}
- free(user);
}
return realsize;
@@ -197,13 +195,13 @@ static inline void request(const char *path, const char *suffix)
curl_easy_perform(curl);
}
-rt_user *rtclient_user(const char *name)
+bool rtclient_get_user(rt_user **user, const char *name)
{
- rt_user *user = NULL;
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, user);
+ *user = malloc(sizeof(rt_user));
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)user);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, user_callback);
request("/REST/1.0/user/", name);
- return user;
+ return (bool)(*user);
}
void rtclient_search(const char *query)
@@ -211,6 +209,32 @@ void rtclient_search(const char *query)
request("/REST/1.0/search/ticket?query=", query);
}
+void rtclient_user_free(rt_user *user)
+{
+ free(user->id);
+ free(user->password);
+ free(user->name);
+ free(user->emailaddress);
+ free(user->realname);
+ free(user->nickname);
+ free(user->gecos);
+ free(user->organization);
+ free(user->address1);
+ free(user->address2);
+ free(user->city);
+ free(user->state);
+ free(user->zip);
+ free(user->country);
+ free(user->homephone);
+ free(user->workphone);
+ free(user->mobilephone);
+ free(user->pagerphone);
+ free(user->contactinfo);
+ free(user->comments);
+ free(user->signature);
+ free(user);
+}
+
void rtclient_cleanup()
{
if (curl) {
diff --git a/rtclient.h b/rtclient.h
index bdf6965..32028b1 100644
--- a/rtclient.h
+++ b/rtclient.h
@@ -9,8 +9,9 @@ extern "C" {
bool rtclient_init(const char *server_url);
void rtclient_login(const char *name, const char *password);
- struct rt_user *rtclient_user(const char *name);
+ bool rtclient_get_user(struct rt_user **user, const char *name);
void rtclient_search(const char *query);
+ void rtclient_user_free(struct rt_user *user);
void rtclient_cleanup();
#ifdef __cplusplus
diff --git a/rtuser.h b/rtuser.h
index 2f7df46..9fccea8 100644
--- a/rtuser.h
+++ b/rtuser.h
@@ -1,6 +1,8 @@
#ifndef RTUSER_H
#define RTUSER_H
+#include <stdbool.h>
+
struct rt_user {
char *id;
char *password;