1N/A/***************************************************************************
1N/A * CVSID: $Id$
1N/A *
1N/A * utils.c - Some utils for the hald runner
1N/A *
1N/A * Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#define DBUS_API_SUBJECT_TO_CHANGE
1N/A#include <dbus/dbus-glib-lowlevel.h>
1N/A#include <glib.h>
1N/A
1N/A#include "utils.h"
1N/A
1N/Achar **
1N/Aget_string_array(DBusMessageIter *iter, char *extra)
1N/A{
1N/A GArray *array;
1N/A char **result;
1N/A array = g_array_new(TRUE, FALSE, sizeof(char *));
1N/A
1N/A while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1N/A const char *value;
1N/A char *t;
1N/A dbus_message_iter_get_basic(iter, &value);
1N/A t = g_strdup(value);
1N/A g_array_append_vals(array, &t, 1);
1N/A dbus_message_iter_next(iter);
1N/A }
1N/A if (extra != NULL)
1N/A g_array_append_vals(array, &extra, 1);
1N/A result = (char **) array->data;
1N/A g_array_free(array, FALSE);
1N/A return result;
1N/A}
1N/A
1N/Achar **
1N/Aget_string_array_from_fd(int fd)
1N/A{
1N/A GArray *array;
1N/A char **result;
1N/A GString *str;
1N/A gsize pos;
1N/A GIOChannel *io;
1N/A int i = 0;
1N/A
1N/A array = g_array_new(TRUE, FALSE, sizeof(char *));
1N/A str = g_string_new("");
1N/A io = g_io_channel_unix_new(fd);
1N/A while (g_io_channel_read_line_string(io, str, &pos, NULL) == G_IO_STATUS_NORMAL && (i++ < 128)) {
1N/A char *t;
1N/A
1N/A /* Remove the terminting char aka \n */
1N/A g_string_erase(str, pos, 1);
1N/A t = g_strdup(str->str);
1N/A g_array_append_vals(array, &t, 1);
1N/A }
1N/A g_string_free(str, TRUE);
1N/A g_io_channel_unref(io);
1N/A result = (char **) array->data;
1N/A g_array_free(array, FALSE);
1N/A return result;
1N/A}
1N/A
1N/Avoid
1N/Afree_string_array(char **array)
1N/A{
1N/A char **p;
1N/A
1N/A for (p = array; p != NULL && *p != NULL; p++)
1N/A g_free(*p);
1N/A g_free(array);
1N/A}