From 9e0b65484828b2b33db73d5324959df28b8d120d Mon Sep 17 00:00:00 2001 From: Nikita Zlobin Date: Mon, 18 Oct 2010 16:32:14 +0600 Subject: [PATCH] Make file reading function common --- common/file.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ common/file.h | 34 ++++++++++++++++++++++++ gui/about.c | 41 ++--------------------------- wscript | 1 + 4 files changed, 109 insertions(+), 39 deletions(-) create mode 100644 common/file.c create mode 100644 common/file.h diff --git a/common/file.c b/common/file.c new file mode 100644 index 00000000..b9b869f0 --- /dev/null +++ b/common/file.c @@ -0,0 +1,72 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2010 Nedko Arnaudov + * Copyright (C) 2010 Nikita Zlobin + * + ************************************************************************** + * This file contains the code that implements the about dialog + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "file.h" + +#include +#include +#include +#include + +char * read_file_contents(const char * filename) +{ + int fd; + struct stat st; + char * buffer; + + if (stat(filename, &st) != 0) + { + return NULL; + } + + fd = open(filename, O_RDONLY); + if (fd == -1) + { + return NULL; + } + + buffer = malloc(st.st_size + 1); + if (buffer == NULL) + { + close(fd); + return NULL; + } + + if (read(fd, buffer, (size_t)st.st_size) != (ssize_t)st.st_size) + { + free(buffer); + buffer = NULL; + } + else + { + buffer[st.st_size] = 0; + } + + close(fd); + + return buffer; +} diff --git a/common/file.h b/common/file.h new file mode 100644 index 00000000..5ded5eac --- /dev/null +++ b/common/file.h @@ -0,0 +1,34 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2010 Nedko Arnaudov + * Copyright (C) 2010 Nikita Zlobin + * + ************************************************************************** + * This file contains the code that implements the about dialog + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __LADISH_COMMON_FILE_H +#define __LADISH_COMMON_FILE_H + +char * read_file_contents(const char * filename); + +#endif diff --git a/gui/about.c b/gui/about.c index 822abbbb..8357ce3a 100644 --- a/gui/about.c +++ b/gui/about.c @@ -34,47 +34,10 @@ #include "gtk_builder.h" #include "version.h" +#include "../common/file.h" + #define ABOUT_DIALOG_LOGO "ladish-logo-128x128.png" -static char * read_file_contents(const char * filename) -{ - int fd; - struct stat st; - char * buffer; - - if (stat(filename, &st) != 0) - { - return NULL; - } - - fd = open(filename, O_RDONLY); - if (fd == -1) - { - return NULL; - } - - buffer = malloc(st.st_size + 1); - if (buffer == NULL) - { - close(fd); - return NULL; - } - - if (read(fd, buffer, (size_t)st.st_size) != (ssize_t)st.st_size) - { - free(buffer); - buffer = NULL; - } - else - { - buffer[st.st_size] = 0; - } - - close(fd); - - return buffer; -} - void show_about(void) { GtkWidget * dialog; diff --git a/wscript b/wscript index f386bc81..702cabf8 100644 --- a/wscript +++ b/wscript @@ -540,6 +540,7 @@ def build(bld): for source in [ 'catdup.c', + 'file.c', ]: gladish.source.append(os.path.join("common", source))