2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_string.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __STRING_H
|
|
|
|
#define __STRING_H
|
|
|
|
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gbx_debug.h"
|
2008-01-27 15:00:04 +01:00
|
|
|
#include "gb_common_string.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int ref;
|
2008-01-27 15:00:04 +01:00
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
char data[0];
|
|
|
|
}
|
|
|
|
PACKED
|
|
|
|
STRING;
|
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
typedef
|
|
|
|
void (*SUBST_FUNC)(int, char **, int *);
|
|
|
|
|
|
|
|
typedef
|
|
|
|
void (*SUBST_ADD_FUNC)(int);
|
|
|
|
|
|
|
|
#define STRING_MAKE_TEMP 32
|
|
|
|
|
|
|
|
typedef
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
char *ptr;
|
|
|
|
int inc;
|
|
|
|
int len;
|
|
|
|
int max;
|
|
|
|
char temp[STRING_MAKE_TEMP];
|
|
|
|
int ntemp;
|
|
|
|
}
|
|
|
|
STRING_MAKE;
|
|
|
|
|
2010-02-08 00:10:41 +01:00
|
|
|
// NOTE: Defined in gambas.h too
|
2008-10-21 11:28:34 +02:00
|
|
|
#define SC_UNICODE ((char *)-1)
|
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
#ifndef __STRING_C
|
|
|
|
extern STRING_MAKE STRING_make_buffer;
|
2010-05-22 20:02:34 +02:00
|
|
|
extern const char STRING_char_string[];
|
2008-11-10 01:18:00 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_init(void);
|
|
|
|
void STRING_exit(void);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
char *STRING_new(const char *src, int len);
|
|
|
|
#define STRING_new_zero(_src) \
|
2010-05-22 20:02:34 +02:00
|
|
|
({ \
|
|
|
|
const char *_s = (_src); \
|
2010-06-05 01:48:53 +02:00
|
|
|
STRING_new(_s, _s ? strlen(_s) : 0); \
|
2010-05-22 20:02:34 +02:00
|
|
|
})
|
|
|
|
|
2008-03-19 15:32:30 +01:00
|
|
|
void STRING_free_real(char *ptr);
|
2010-06-05 01:48:53 +02:00
|
|
|
char *STRING_free_later(char *ptr);
|
2008-01-17 22:39:26 +01:00
|
|
|
int STRING_get_free_index(void);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
#define STRING_new_temp(_src, _len) STRING_free_later(STRING_new(_src, _len))
|
|
|
|
#define STRING_new_temp_zero(_src) STRING_free_later(STRING_new_zero(_src))
|
2008-03-19 15:32:30 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_extend(char **ptr, int new_len);
|
|
|
|
void STRING_extend_end(char **ptr);
|
|
|
|
void STRING_add(char **ptr, const char *src, int len);
|
2008-01-27 15:00:04 +01:00
|
|
|
void STRING_add_char(char **ptr, char c);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
#define STRING_copy_from_value(_value) \
|
|
|
|
({ \
|
|
|
|
char *ptr; \
|
|
|
|
if ((_value)->_string.len == 0) \
|
|
|
|
ptr = NULL; \
|
|
|
|
else if ((_value)->type == T_STRING && (_value)->_string.start == 0 && (_value)->_string.len == STRING_length((_value)->_string.addr)) \
|
|
|
|
ptr = (_value)->_string.addr; \
|
|
|
|
else \
|
|
|
|
ptr = STRING_new(&(_value)->_string.addr[(_value)->_string.start], (_value)->_string.len); \
|
|
|
|
ptr; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define STRING_copy_from_value_temp(_value) \
|
|
|
|
({ \
|
|
|
|
char *ptr; \
|
|
|
|
if ((_value)->_string.len == 0) \
|
|
|
|
ptr = NULL; \
|
|
|
|
else if ((_value)->type == T_STRING && (_value)->_string.start == 0 && (_value)->_string.len == STRING_length((_value)->_string.addr)) \
|
|
|
|
ptr = (_value)->_string.addr; \
|
|
|
|
else \
|
|
|
|
ptr = STRING_new_temp(&(_value)->_string.addr[(_value)->_string.start], (_value)->_string.len); \
|
|
|
|
ptr; \
|
|
|
|
})
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_new_temp_value(VALUE *value, const char *src, int len);
|
|
|
|
void STRING_new_constant_value(VALUE *value, const char *src, int len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
#define STRING_char_value(_value, _car) \
|
|
|
|
do { \
|
|
|
|
_value->type = T_CSTRING; \
|
|
|
|
_value->_string.addr = (char *)&STRING_char_string[(_car) * 2]; \
|
|
|
|
_value->_string.start = 0; \
|
|
|
|
_value->_string.len = 1; \
|
|
|
|
} while(0)
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_void_value(VALUE *value);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-26 17:28:18 +01:00
|
|
|
char *STRING_subst(const char *str, int len, SUBST_FUNC get_param);
|
|
|
|
char *STRING_subst_add(const char *str, int len, SUBST_ADD_FUNC add_param);
|
2008-01-17 22:39:26 +01:00
|
|
|
int STRING_conv(char **result, const char *str, int len, const char *src, const char *dst, bool throw);
|
|
|
|
char *STRING_conv_file_name(const char *name, int len);
|
|
|
|
char *STRING_conv_to_UTF8(const char *name, int len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#define STRING_from_ptr(_ptr) ((STRING *)((_ptr) - offsetof(STRING, data)))
|
|
|
|
#define STRING_length(_ptr) ((_ptr) == NULL ? 0 : STRING_from_ptr(_ptr)->len)
|
|
|
|
|
|
|
|
#if DEBUG_STRING
|
|
|
|
|
2008-03-19 15:32:30 +01:00
|
|
|
void STRING_free(char **ptr);
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_ref(char *ptr);
|
|
|
|
void STRING_unref(char **ptr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2008-03-19 15:32:30 +01:00
|
|
|
#define STRING_free(_p) \
|
|
|
|
({ \
|
|
|
|
char **pptr = _p; \
|
|
|
|
char *ptr = *pptr; \
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(ptr != NULL)) \
|
2008-03-19 15:32:30 +01:00
|
|
|
{ \
|
|
|
|
STRING_free_real(ptr); \
|
|
|
|
*pptr = NULL; \
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#define STRING_ref(_p) \
|
|
|
|
({ \
|
|
|
|
char *ptr = _p; \
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(ptr != NULL)) \
|
2007-12-30 17:41:49 +01:00
|
|
|
STRING_from_ptr(ptr)->ref++; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define STRING_unref(_p) \
|
|
|
|
({ \
|
|
|
|
char **pptr = _p; \
|
|
|
|
char *ptr = *pptr; \
|
|
|
|
STRING *str; \
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(ptr != NULL)) \
|
2007-12-30 17:41:49 +01:00
|
|
|
{ \
|
|
|
|
str = STRING_from_ptr(ptr); \
|
|
|
|
if ((--str->ref) <= 0) \
|
2008-03-19 15:32:30 +01:00
|
|
|
{ \
|
|
|
|
STRING_free_real(ptr); \
|
|
|
|
*pptr = NULL; \
|
|
|
|
} \
|
2007-12-30 17:41:49 +01:00
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STRING_unref_keep(char **ptr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STRING_search(const char *ps, int ls, const char *pp, int lp, int is, bool right, bool nocase);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-11-10 01:18:00 +01:00
|
|
|
void STRING_start_len(int len);
|
|
|
|
#define STRING_start() STRING_start_len(0)
|
|
|
|
char *STRING_end();
|
|
|
|
char *STRING_end_temp();
|
|
|
|
void STRING_make(const char *src, int len);
|
|
|
|
void STRING_make_dump();
|
|
|
|
|
|
|
|
#define STRING_make_char(_c) \
|
2009-01-02 01:05:38 +01:00
|
|
|
({ \
|
2010-05-19 22:18:23 +02:00
|
|
|
if (UNLIKELY(STRING_make_buffer.ntemp == STRING_MAKE_TEMP)) \
|
2008-11-10 01:18:00 +01:00
|
|
|
STRING_make_dump(); \
|
|
|
|
STRING_make_buffer.temp[STRING_make_buffer.ntemp++] = (_c); \
|
2009-01-02 01:05:38 +01:00
|
|
|
})
|
2008-11-10 01:18:00 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
#endif
|