Converted most of GTK launcher to use glib's string functions.

This commit is contained in:
Matthew Bowra-Dean
2010-12-30 16:04:47 +13:00
committed by Chris Forbes
parent 9739e7f51f
commit 0a1e6d16bd
5 changed files with 144 additions and 132 deletions

View File

@@ -12,11 +12,11 @@
#include <glib.h>
#include <sys/wait.h>
int util_do_command_async(char * command, GChildWatchFunc callback)
gboolean util_do_command_async(gchar * command, GChildWatchFunc callback)
{
GPid child_pid;
gint * out_fd = (gint *)malloc(sizeof(gint));
char * spawn_args[] = { "mono", "OpenRA.Utility.exe", command, NULL };
gchar * spawn_args[] = { "mono", "OpenRA.Utility.exe", command, NULL };
gboolean result;
result = g_spawn_async_with_pipes(NULL, spawn_args, NULL,
@@ -33,17 +33,17 @@ int util_do_command_async(char * command, GChildWatchFunc callback)
return TRUE;
}
int util_get_mod_list (GChildWatchFunc callback)
gboolean util_get_mod_list (GChildWatchFunc callback)
{
return util_do_command_async("-l", callback);
}
int util_do_command_blocking(char * command, GChildWatchFunc callback)
gboolean util_do_command_blocking(gchar * command, GChildWatchFunc callback)
{
GPid child_pid;
int status;
gint * out_fd = (gint *)malloc(sizeof(gint));
char * spawn_args[] = { "mono", "OpenRA.Utility.exe", command, NULL };
gint * out_fd = (gint *)g_malloc(sizeof(gint));
gchar * spawn_args[] = { "mono", "OpenRA.Utility.exe", command, NULL };
gboolean result;
result = g_spawn_async_with_pipes(NULL, spawn_args, NULL,
@@ -55,7 +55,6 @@ int util_do_command_blocking(char * command, GChildWatchFunc callback)
return FALSE;
}
//g_child_watch_add(child_pid, callback, out_fd);
waitpid(child_pid, &status, 0);
callback(child_pid, status, out_fd);
@@ -63,47 +62,44 @@ int util_do_command_blocking(char * command, GChildWatchFunc callback)
return TRUE;
}
int util_get_mod_metadata(char const * mod, GChildWatchFunc callback)
gboolean util_get_mod_metadata(gchar const * mod, GChildWatchFunc callback)
{
char * util_args;
int return_val;
GString * util_args = g_string_new(NULL);
gboolean return_val;
util_args = (char *)malloc(strlen(mod) + strlen("-i=") + 1);
sprintf(util_args, "-i=%s", mod);
return_val = util_do_command_blocking(util_args, callback);
free(util_args);
g_string_printf(util_args, "-i=%s", mod);
return_val = util_do_command_blocking(util_args->str, callback);
g_string_free(util_args, TRUE);
return return_val;
}
int util_get_setting(const char * setting, GChildWatchFunc callback)
gboolean util_get_setting(gchar const * setting, GChildWatchFunc callback)
{
char * command;
int return_val;
GString * command = g_string_new(NULL);
gboolean return_val;
command = (char *)malloc(strlen(setting) + strlen("--settings-value=~/.openra,") + 1);
sprintf(command, "--settings-value=~/.openra,%s", setting);
return_val = util_do_command_blocking(command, callback);
free(command);
g_string_printf(command, "--settings-value=~/.openra,%s", setting);
return_val = util_do_command_blocking(command->str, callback);
g_string_free(command, TRUE);
return return_val;
}
int util_spawn_with_command(const char * command, const char * arg1, const char * arg2, GPid * pid)
gint util_spawn_with_command(gchar const * command, gchar const * arg1, gchar const * arg2, GPid * pid)
{
char * complete_command;
int out_fd;
GString * complete_command = g_string_new(NULL);
gint out_fd;
gboolean result;
char * launch_args[] = { "mono", "OpenRA.Utility.exe", NULL, NULL };
gchar * launch_args[] = { "mono", "OpenRA.Utility.exe", NULL, NULL };
complete_command = (char *)malloc(strlen(command) + strlen(arg1) + strlen(arg2) + 2);
sprintf(complete_command, "%s%s,%s", command, arg1, arg2);
g_string_printf(complete_command, "%s%s,%s", command, arg1, arg2);
launch_args[2] = complete_command;
launch_args[2] = complete_command->str;
result = g_spawn_async_with_pipes(NULL, launch_args, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, pid, NULL, &out_fd, NULL, NULL);
free(complete_command);
g_string_free(complete_command, TRUE);
if (!result)
{
@@ -113,39 +109,34 @@ int util_spawn_with_command(const char * command, const char * arg1, const char
return out_fd;
}
int util_do_download(const char * url, const char * dest, GPid * pid)
gint util_do_download(gchar const * url, gchar const * dest, GPid * pid)
{
return util_spawn_with_command("--download-url=", url, dest, pid);
}
int util_do_extract(const char * target, const char * dest, GPid * pid)
gint util_do_extract(gchar const * target, gchar const * dest, GPid * pid)
{
return util_spawn_with_command("--extract-zip=", target, dest, pid);
}
char * util_get_output(int fd, int * output_len)
GString * util_get_output(int fd)
{
char buffer[1024], * msg = NULL;
char buffer[1024];
GString * msg = g_string_new(NULL);
int read_bytes = 0;
*output_len = 0;
while (0 != (read_bytes = read(fd, buffer, 1024)))
{
if (-1 == read_bytes)
{
g_error("Error reading from command output");
free(msg);
*output_len = 0;
g_string_free(msg, TRUE);
return NULL;
}
*output_len += read_bytes;
msg = (char *)realloc(msg, *output_len + 1);
memcpy(msg + (*output_len - read_bytes), buffer, read_bytes);
g_string_append_len(msg, buffer, read_bytes);
}
msg[*output_len] = '\0';
g_string_append_c(msg, '\0');
return msg;
}