* Added g_unichar_iszerowidth() and g_file_set_contents() functions for

compability with glib-2.6

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2009-10-21 22:18:03 +03:00 committed by Ilia Maslakov
parent dfb0040c5b
commit e29e6126a6
4 changed files with 80 additions and 136 deletions

View File

@ -1,24 +1,30 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
Copyright (C) 2009
Free Software Foundation, Inc.
Written by:
Slava Zanko <slavazanko@gmail.com>, 2009.
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/** \file glibcompat.c
* \brief Source: old glib compatibility
* \brief Source: compatibility with older versions of glib
*
* Following code was copied from glib to GNU Midnight Commander to
* provide compatibility with older versions of glib.
@ -26,90 +32,52 @@
#include <config.h>
#include <string.h>
#include <glib.h>
#include "global.h"
#include "glibcompat.h"
#if GLIB_MAJOR_VERSION < 2
/*** global variables ****************************************************************************/
/* Functions g_strlcpy and g_strlcat were originally developed by
* Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
* See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
* for more information.
*/
/*** file scope macro definitions ****************************************************************/
#ifdef HAVE_STRLCPY
gsize
g_strlcpy (gchar *dest,
const gchar *src,
gsize dest_size)
#if ! GLIB_CHECK_VERSION (2, 13, 0)
#define ISZEROWIDTHTYPE(Type) IS ((Type), \
OR (G_UNICODE_NON_SPACING_MARK, \
OR (G_UNICODE_ENCLOSING_MARK, \
OR (G_UNICODE_FORMAT, 0))))
#endif /* ! GLIB_CHECK_VERSION (2, 13, 0) */
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
#if ! GLIB_CHECK_VERSION (2, 13, 0)
gboolean
g_unichar_iszerowidth (gunichar c)
{
g_return_val_if_fail (dest != NULL, 0);
g_return_val_if_fail (src != NULL, 0);
return strlcpy (dest, src, dest_size);
}
if (G_UNLIKELY (c == 0x00AD))
return FALSE;
gsize
g_strlcat (gchar *dest,
const gchar *src,
gsize dest_size)
if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
return TRUE;
if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) || c == 0x200B))
return TRUE;
return FALSE;
}
#endif /* ! GLIB_CHECK_VERSION (2, 13, 0) */
/* --------------------------------------------------------------------------------------------- */
#if ! GLIB_CHECK_VERSION (2, 7, 0)
gboolean
g_file_set_contents (const gchar * filename, const gchar * contents, gssize length, GError ** error)
{
g_return_val_if_fail (dest != NULL, 0);
g_return_val_if_fail (src != NULL, 0);
return strlcat (dest, src, dest_size);
return g_file_replace (filename, contents, length, error);
}
#else /* ! HAVE_STRLCPY */
/* g_strlcpy
*
* Copy string src to buffer dest (of buffer size dest_size). At most
* dest_size-1 characters will be copied. Always NUL terminates
* (unless dest_size == 0). This function does NOT allocate memory.
* Unlike strncpy, this function doesn't pad dest (so it's often faster).
* Returns size of attempted result, strlen(src),
* so if retval >= dest_size, truncation occurred.
*/
gsize
g_strlcpy (gchar *dest,
const gchar *src,
gsize dest_size)
{
register gchar *d = dest;
register const gchar *s = src;
register gsize n = dest_size;
g_return_val_if_fail (dest != NULL, 0);
g_return_val_if_fail (src != NULL, 0);
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0)
do
{
register gchar c = *s++;
*d++ = c;
if (c == 0)
break;
}
while (--n != 0);
/* If not enough room in dest, add NUL and traverse rest of src */
if (n == 0)
{
if (dest_size != 0)
*d = 0;
while (*s++)
;
}
return s - src - 1; /* count does not include NUL */
}
#endif /* ! HAVE_STRLCPY */
#endif /* GLIB_MAJOR_VERSION < 2 */
#endif /* ! GLIB_CHECK_VERSION (2, 7, 0) */

View File

@ -1,45 +1,23 @@
/* glibcompat.h -- declarations for functions decared in glibcompat.c,
i.e. functions present only in recent versions of glib.
This program 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, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/** \file glibcompat.h
* \brief Header: old glib compatibility
*
* Following code was copied from glib to GNU Midnight Commander to
* provide compatibility with older versions of glib.
*/
#ifndef MC_GLIBCOMPAT_H
#define MC_GLIBCOMPAT_H
#if GLIB_MAJOR_VERSION < 2
/*** typedefs(not structures) and defined constants **********************************************/
gsize g_strlcat (gchar *dest, const gchar *src, gsize dest_size);
gsize g_strlcpy (gchar *dest, const gchar *src, gsize dest_size);
#define g_try_malloc(size) malloc(size)
#define g_try_realloc(ptr,size) realloc(ptr,size)
/*** enums ***************************************************************************************/
static inline GSList *
g_slist_delete_link (GSList *list, GSList *link)
{
list = g_slist_remove_link (list, link);
g_slist_free_1 (link);
return list;
}
/*** structures declarations (and typedefs of structures)*****************************************/
#endif /* GLIB_MAJOR_VERSION < 2 */
/*** global variables defined in .c file *********************************************************/
#endif
/*** declarations of public functions ************************************************************/
#if ! GLIB_CHECK_VERSION (2, 13, 0)
gboolean g_unichar_iszerowidth (gunichar);
#endif /* ! GLIB_CHECK_VERSION (2, 13, 0) */
#if ! GLIB_CHECK_VERSION (2, 7, 0)
gboolean g_file_set_contents (const gchar *, const gchar *, gssize, GError **);
#endif /* ! GLIB_CHECK_VERSION (2, 7, 0) */
#endif /* MC_GLIBCOMPAT_H */

View File

@ -56,7 +56,6 @@
#include "layout.h" /* repaint_screen() */
#include "hotlist.h"
#include "command.h" /* cmdline */
#include "glibcompat.h" /* g_strlcpy for glib < 2.0 */
#include "history.h"
#include "strutil.h"
#include "util.h"

View File

@ -32,7 +32,6 @@
#include <glib.h>
#include "help.h"
#include "glibcompat.h"
#define BUFFER_SIZE 256