(root)/
gettext-0.22.4/
libtextstyle/
lib/
libcroco/
cr-string.c
       1  /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
       2  
       3  /* libcroco - Library for parsing and applying CSS
       4   * Copyright (C) 2006-2019 Free Software Foundation, Inc.
       5   *
       6   * This file is not part of the GNU gettext program, but is used with
       7   * GNU gettext.
       8   *
       9   * The original copyright notice is as follows:
      10   */
      11  
      12  /*
      13   * This file is part of The Croco Library
      14   *
      15   * Copyright (C) 2003-2004 Dodji Seketeli.  All Rights Reserved.
      16   *
      17   * This program is free software; you can redistribute it and/or
      18   * modify it under the terms of version 2.1 of the GNU Lesser General Public
      19   * License as published by the Free Software Foundation.
      20   *
      21   * This program is distributed in the hope that it will be useful,
      22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      24   * GNU General Public License for more details.
      25   *
      26   * You should have received a copy of the GNU Lesser General Public License
      27   * along with this program; if not, write to the Free Software
      28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
      29   * USA
      30   * 
      31   * Author: Dodji Seketeli.
      32   */
      33  
      34  #include <config.h>
      35  #include <string.h>
      36  #include "cr-string.h"
      37  
      38  /**
      39   *Instanciates a #CRString
      40   *@return the newly instanciated #CRString
      41   *Must be freed with cr_string_destroy().
      42   */
      43  CRString *
      44  cr_string_new (void)
      45  {
      46  	CRString *result = NULL ;
      47  
      48  	result = g_try_malloc (sizeof (CRString)) ;
      49  	if (!result) {
      50  		cr_utils_trace_info ("Out of memory") ;
      51  		return NULL ;
      52  	}
      53  	memset (result, 0, sizeof (CRString)) ;
      54          result->stryng = g_string_new (NULL) ;
      55  	return result ;
      56  }
      57  
      58  /**
      59   *Instanciate a string and initialise it to
      60   *a_string.
      61   *@param a_string the initial string
      62   *@return the newly instanciated string.
      63   */
      64  CRString  *
      65  cr_string_new_from_string (const gchar * a_string)
      66  {
      67  	CRString *result = NULL ;
      68  
      69  	result = cr_string_new () ;
      70  	if (!result) {
      71  		cr_utils_trace_info ("Out of memory") ;
      72  		return NULL ;
      73  	}
      74  	if (a_string)
      75  		g_string_append (result->stryng, a_string) ;
      76  	return result ;
      77  }
      78  
      79  /**
      80   *Instanciates a #CRString from an instance of GString.
      81   *@param a_string the input string that will be copied into
      82   *the newly instanciated #CRString
      83   *@return the newly instanciated #CRString.
      84   */
      85  CRString *
      86  cr_string_new_from_gstring (GString const *a_string)
      87  {
      88  	CRString *result = NULL ;
      89  
      90  	result = cr_string_new () ;
      91  	if (!result) {
      92  		cr_utils_trace_info ("Out of memory") ;
      93  		return NULL ;
      94  	}
      95  	if (a_string) {
      96  		g_string_append_len (result->stryng,
      97  				     a_string->str,
      98  				     a_string->len);
      99  
     100  	}
     101  	return result ;
     102  }
     103  
     104  CRString *
     105  cr_string_dup (CRString const *a_this)
     106  {
     107  	CRString *result = NULL ;
     108  	g_return_val_if_fail (a_this, NULL) ;
     109  
     110  	result = cr_string_new_from_gstring (a_this->stryng) ;
     111  	if (!result) {
     112  		cr_utils_trace_info ("Out of memory") ;
     113  		return NULL ;
     114  	}
     115  	cr_parsing_location_copy (&result->location,
     116                                    &a_this->location) ;
     117          return result ;
     118  }
     119  
     120  gchar *
     121  cr_string_dup2 (CRString const *a_this)
     122  {
     123          gchar *result = NULL ;
     124  
     125          g_return_val_if_fail (a_this, NULL) ;
     126  
     127          if (a_this 
     128              && a_this->stryng 
     129              && a_this->stryng->str) {
     130                  result = g_strndup (a_this->stryng->str,
     131                                      a_this->stryng->len) ;
     132          }
     133          return result ;
     134  }
     135  
     136  /**
     137   *Returns a pointer to the internal raw NULL terminated string
     138   *of the current instance of #CRString.
     139   *@param a_this the current instance of #CRString
     140   */
     141  const gchar *
     142  cr_string_peek_raw_str (CRString const *a_this)
     143  {
     144          g_return_val_if_fail (a_this, NULL) ;
     145          
     146          if (a_this->stryng && a_this->stryng->str)
     147                  return a_this->stryng->str ;
     148          return NULL ;
     149  }
     150  
     151  /**
     152   *Returns the length of the internal raw NULL terminated
     153   *string of the current instance of #CRString.
     154   *@param a_this the current instance of #CRString.
     155   *@return the len of the internal raw NULL termninated string,
     156   *of -1 if no length can be returned.
     157   */
     158  gint
     159  cr_string_peek_raw_str_len (CRString const *a_this)
     160  {
     161          g_return_val_if_fail (a_this && a_this->stryng,
     162                                -1) ;
     163          return a_this->stryng->len ;
     164  }
     165  
     166  /**
     167   *@param a_this the #CRString to destroy.
     168   */
     169  void
     170  cr_string_destroy (CRString *a_this)
     171  {
     172  	g_return_if_fail (a_this) ;
     173  
     174  	if (a_this->stryng) {
     175  		g_string_free (a_this->stryng, TRUE) ;
     176  		a_this->stryng = NULL ;
     177  	}
     178  	g_free (a_this) ;
     179  }