Make compileable mhl library.
Create mhl/string.c file
Этот коммит содержится в:
родитель
14b078b908
Коммит
c9bdf6f0a4
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
AUTOMAKE_OPTIONS = 1.5
|
AUTOMAKE_OPTIONS = 1.5
|
||||||
|
|
||||||
SUBDIRS = intl po m4 vfs slang edit src lib doc syntax
|
SUBDIRS = intl po m4 vfs slang mhl edit src lib doc syntax
|
||||||
|
|
||||||
EXTRA_DIST = FAQ HACKING INSTALL.FAST MAINTAINERS README.QNX TODO pkginfo.in prototype.in mc.qpg mc.spec
|
EXTRA_DIST = FAQ HACKING INSTALL.FAST MAINTAINERS README.QNX TODO pkginfo.in prototype.in mc.qpg mc.spec
|
||||||
|
|
||||||
|
@ -606,6 +606,7 @@ slang/Makefile
|
|||||||
edit/Makefile
|
edit/Makefile
|
||||||
syntax/Makefile
|
syntax/Makefile
|
||||||
m4/Makefile
|
m4/Makefile
|
||||||
|
mhl/Makefile
|
||||||
lib/mc.ext
|
lib/mc.ext
|
||||||
vfs/extfs/a
|
vfs/extfs/a
|
||||||
vfs/extfs/apt
|
vfs/extfs/apt
|
||||||
|
12
mhl/Makefile.am
Обычный файл
12
mhl/Makefile.am
Обычный файл
@ -0,0 +1,12 @@
|
|||||||
|
AM_CFLAGS = $(GLIB_CFLAGS)
|
||||||
|
|
||||||
|
noinst_LIBRARIES = libmhl.a
|
||||||
|
|
||||||
|
libmhl_a_SOURCES = \
|
||||||
|
env.h \
|
||||||
|
memory.h \
|
||||||
|
shell_escape.h \
|
||||||
|
strhash.h \
|
||||||
|
string.c string.h
|
||||||
|
|
||||||
|
EXTRA_DIST = README
|
@ -36,6 +36,8 @@ memory.h: Memory management functions
|
|||||||
|
|
||||||
string.h: String helpers
|
string.h: String helpers
|
||||||
|
|
||||||
|
TODO: need to support multibyte codepages.
|
||||||
|
|
||||||
* mhl_str_dup(const char*s) -> char*
|
* mhl_str_dup(const char*s) -> char*
|
||||||
|
|
||||||
[MACRO-FUNC] Safe version of strdup(), when NULL passed, returns strdup("")
|
[MACRO-FUNC] Safe version of strdup(), when NULL passed, returns strdup("")
|
||||||
|
@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
#define mhl_getenv_dup(name) (mhl_str_dup(name ? getenv(name) : ""))
|
#define mhl_getenv_dup(name) (mhl_str_dup(name ? getenv(name) : ""))
|
||||||
|
|
||||||
#endif
|
#endif // __MHL_ENV_H
|
||||||
|
@ -97,4 +97,4 @@ static inline void mhl_mem_free(void* ptr)
|
|||||||
/* free an ptr and NULL it */
|
/* free an ptr and NULL it */
|
||||||
#define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0);
|
#define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0);
|
||||||
|
|
||||||
#endif
|
#endif // __MHL_MEM
|
||||||
|
@ -109,4 +109,4 @@ static inline char* mhl_shell_unescape_buf(char* text)
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // __MHL_SHELL_ESCAPE_H
|
||||||
|
@ -46,4 +46,4 @@ static inline void* mhl_strhash_lookup(MHL_STRHASH* ht, const char* key)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // __MHL_STRHASH_H
|
||||||
|
167
mhl/string.c
Обычный файл
167
mhl/string.c
Обычный файл
@ -0,0 +1,167 @@
|
|||||||
|
/* Micro helper library - strings
|
||||||
|
|
||||||
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
Written by:
|
||||||
|
2009 Enrico Weigelt
|
||||||
|
2009 Slava Zanko
|
||||||
|
2009 Patrick Winnertz
|
||||||
|
|
||||||
|
|
||||||
|
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 of the License, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------- */
|
||||||
|
char *
|
||||||
|
mhl_str_reverse (char *ptr)
|
||||||
|
{
|
||||||
|
if (!ptr)
|
||||||
|
return NULL; // missing string
|
||||||
|
if (!(ptr[0] && ptr[1]))
|
||||||
|
return ptr; // empty or 1-ch string
|
||||||
|
|
||||||
|
size_t _sz = strlen (ptr);
|
||||||
|
char *start = ptr;
|
||||||
|
char *end = ptr + _sz - 1;
|
||||||
|
|
||||||
|
while (start < end)
|
||||||
|
{
|
||||||
|
char c = *start;
|
||||||
|
*start = *end;
|
||||||
|
*end = c;
|
||||||
|
start++;
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------- */
|
||||||
|
char *
|
||||||
|
__mhl_str_concat_hlp (const char *base, ...)
|
||||||
|
{
|
||||||
|
static const char *arg_ptr[__STR_CONCAT_MAX];
|
||||||
|
static size_t arg_sz[__STR_CONCAT_MAX];
|
||||||
|
int count = 0;
|
||||||
|
size_t totalsize = 0;
|
||||||
|
|
||||||
|
// first pass: scan through the params and count string sizes
|
||||||
|
va_list par;
|
||||||
|
|
||||||
|
if (base)
|
||||||
|
{
|
||||||
|
arg_ptr[0] = base;
|
||||||
|
arg_sz[0] = totalsize = strlen (base);
|
||||||
|
count = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start (args, base);
|
||||||
|
char *a;
|
||||||
|
// note: we use ((char*)(1)) as terminator - NULL is a valid argument !
|
||||||
|
while ((a = va_arg (args, char *)) != (char *) 1)
|
||||||
|
{
|
||||||
|
// printf("a=%u\n", a);
|
||||||
|
if (a)
|
||||||
|
{
|
||||||
|
arg_ptr[count] = a;
|
||||||
|
arg_sz[count] = strlen (a);
|
||||||
|
totalsize += arg_sz[count];
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!count)
|
||||||
|
return mhl_str_dup ("");
|
||||||
|
|
||||||
|
// now as we know how much to copy, allocate the buffer
|
||||||
|
char *buffer = (char *) mhl_mem_alloc_u (totalsize + 2);
|
||||||
|
char *current = buffer;
|
||||||
|
int x = 0;
|
||||||
|
for (x = 0; x < count; x++)
|
||||||
|
{
|
||||||
|
memcpy (current, arg_ptr[x], arg_sz[x]);
|
||||||
|
current += arg_sz[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
*current = 0;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------- */
|
||||||
|
void
|
||||||
|
mhl_str_toupper (char *str)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
FIXME: need to upper miltibyte strings... utf-8 for example.
|
||||||
|
And after UPPER byte-lenght of wide string may change.
|
||||||
|
This mean, need to create new string.
|
||||||
|
*/
|
||||||
|
if (str)
|
||||||
|
for (; *str; str++)
|
||||||
|
*str = toupper (*str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------- */
|
||||||
|
char *
|
||||||
|
mhl_str_trim (char *str)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
return NULL; // NULL string ?! bail out.
|
||||||
|
|
||||||
|
// find the first non-space
|
||||||
|
char *start;
|
||||||
|
for (start = str; ((*str) && (!isspace (*str))); str++);
|
||||||
|
|
||||||
|
// only spaces ?
|
||||||
|
if (!(*str))
|
||||||
|
{
|
||||||
|
*str = 0;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the size (cannot be empty - catched above)
|
||||||
|
size_t _sz = strlen (str);
|
||||||
|
|
||||||
|
// find the proper end
|
||||||
|
char *end;
|
||||||
|
for (end = (str + _sz - 1); ((end > str) && (isspace (*end))); end--);
|
||||||
|
end[1] = 0; // terminate, just to be sure
|
||||||
|
|
||||||
|
// if we have no leading spaces, just trucate
|
||||||
|
if (start == str)
|
||||||
|
{
|
||||||
|
end++;
|
||||||
|
*end = 0;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// if it' only one char, dont need memmove for that
|
||||||
|
if (start == end)
|
||||||
|
{
|
||||||
|
str[0] = *start;
|
||||||
|
str[1] = 0;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// by here we have a (non-empty) region between start end end
|
||||||
|
memmove (str, start, (end - start + 1));
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------------- */
|
111
mhl/string.h
111
mhl/string.h
@ -9,116 +9,17 @@
|
|||||||
#define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup("")))
|
#define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup("")))
|
||||||
#define mhl_str_len(str) ((str ? strlen(str) : 0))
|
#define mhl_str_len(str) ((str ? strlen(str) : 0))
|
||||||
|
|
||||||
static inline char* mhl_str_trim(char* str)
|
char* mhl_str_trim(char*);
|
||||||
{
|
|
||||||
if (!str) return NULL; // NULL string ?! bail out.
|
|
||||||
|
|
||||||
// find the first non-space
|
void mhl_str_toupper(char*);
|
||||||
char* start; for (start=str; ((*str) && (!isspace(*str))); str++);
|
|
||||||
|
|
||||||
// only spaces ?
|
|
||||||
if (!(*str)) { *str = 0; return str; }
|
|
||||||
|
|
||||||
// get the size (cannot be empty - catched above)
|
|
||||||
size_t _sz = strlen(str);
|
|
||||||
|
|
||||||
// find the proper end
|
|
||||||
char* end;
|
|
||||||
for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--);
|
|
||||||
end[1] = 0; // terminate, just to be sure
|
|
||||||
|
|
||||||
// if we have no leading spaces, just trucate
|
|
||||||
if (start==str) { end++; *end = 0; return str; }
|
|
||||||
|
|
||||||
|
|
||||||
// if it' only one char, dont need memmove for that
|
|
||||||
if (start==end) { str[0]=*start; str[1]=0; return str; }
|
|
||||||
|
|
||||||
// by here we have a (non-empty) region between start end end
|
|
||||||
memmove(str,start,(end-start+1));
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void mhl_str_toupper(char* str)
|
|
||||||
{
|
|
||||||
if (str)
|
|
||||||
for (;*str;str++)
|
|
||||||
*str = toupper(*str);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define __STR_CONCAT_MAX 32
|
#define __STR_CONCAT_MAX 32
|
||||||
|
|
||||||
/* _NEVER_ call this function directly ! */
|
/* _NEVER_ call this function directly ! */
|
||||||
static inline char* __mhl_str_concat_hlp(const char* base, ...)
|
char* __mhl_str_concat_hlp(const char*, ...);
|
||||||
{
|
|
||||||
static const char* arg_ptr[__STR_CONCAT_MAX];
|
|
||||||
static size_t arg_sz[__STR_CONCAT_MAX];
|
|
||||||
int count = 0;
|
|
||||||
size_t totalsize = 0;
|
|
||||||
|
|
||||||
// first pass: scan through the params and count string sizes
|
|
||||||
va_list par;
|
|
||||||
|
|
||||||
if (base)
|
|
||||||
{
|
|
||||||
arg_ptr[0] = base;
|
|
||||||
arg_sz[0] = totalsize = strlen(base);
|
|
||||||
count = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start(args,base);
|
|
||||||
char* a;
|
|
||||||
// note: we use ((char*)(1)) as terminator - NULL is a valid argument !
|
|
||||||
while ((a = va_arg(args, char*))!=(char*)1)
|
|
||||||
{
|
|
||||||
// printf("a=%u\n", a);
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
arg_ptr[count] = a;
|
|
||||||
arg_sz[count] = strlen(a);
|
|
||||||
totalsize += arg_sz[count];
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!count)
|
|
||||||
return mhl_str_dup("");
|
|
||||||
|
|
||||||
// now as we know how much to copy, allocate the buffer
|
|
||||||
char* buffer = (char*)mhl_mem_alloc_u(totalsize+2);
|
|
||||||
char* current = buffer;
|
|
||||||
int x=0;
|
|
||||||
for (x=0; x<count; x++)
|
|
||||||
{
|
|
||||||
memcpy(current, arg_ptr[x], arg_sz[x]);
|
|
||||||
current += arg_sz[x];
|
|
||||||
}
|
|
||||||
|
|
||||||
*current = 0;
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1)))
|
#define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1)))
|
||||||
|
|
||||||
static inline char* mhl_str_reverse(char* ptr)
|
char* mhl_str_reverse(char*);
|
||||||
{
|
|
||||||
if (!ptr) return NULL; // missing string
|
|
||||||
if (!(ptr[0] && ptr[1])) return ptr; // empty or 1-ch string
|
|
||||||
|
|
||||||
size_t _sz = strlen(ptr);
|
#endif // __MHL_STRING_H
|
||||||
char* start = ptr;
|
|
||||||
char* end = ptr+_sz-1;
|
|
||||||
|
|
||||||
while (start<end)
|
|
||||||
{
|
|
||||||
char c = *start;
|
|
||||||
*start = *end;
|
|
||||||
*end = c;
|
|
||||||
start++;
|
|
||||||
end--;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user