2007-11-23 14:11:42 +08:00
|
|
|
#ifndef _RPMSTRING_H_
|
|
|
|
#define _RPMSTRING_H_
|
1995-12-14 00:01:36 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* \file rpmio/rpmstring.h
|
|
|
|
* String manipulation helper functions
|
2000-08-28 03:27:03 +08:00
|
|
|
*/
|
|
|
|
|
2008-01-28 19:43:32 +08:00
|
|
|
#include <stddef.h>
|
2009-09-22 21:02:39 +08:00
|
|
|
#include <string.h>
|
2008-01-28 19:43:32 +08:00
|
|
|
|
2008-03-18 16:17:03 +08:00
|
|
|
#include <rpm/rpmutil.h>
|
|
|
|
|
1999-07-14 05:37:57 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive islower(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int rislower(int c) {
|
2007-11-23 14:32:08 +08:00
|
|
|
return (c >= 'a' && c <= 'z');
|
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isupper(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risupper(int c) {
|
2007-11-23 14:32:08 +08:00
|
|
|
return (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isalpha(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risalpha(int c) {
|
|
|
|
return (rislower(c) || risupper(c));
|
2007-11-23 14:32:08 +08:00
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isdigit(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risdigit(int c) {
|
2007-11-23 14:32:08 +08:00
|
|
|
return (c >= '0' && c <= '9');
|
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isalnum(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risalnum(int c) {
|
|
|
|
return (risalpha(c) || risdigit(c));
|
2007-11-23 14:32:08 +08:00
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isblank(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risblank(int c) {
|
2007-11-23 14:32:08 +08:00
|
|
|
return (c == ' ' || c == '\t');
|
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive isspace(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int risspace(int c) {
|
|
|
|
return (risblank(c) || c == '\n' || c == '\r' || c == '\f' || c == '\v');
|
2007-11-23 14:32:08 +08:00
|
|
|
}
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive tolower(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int rtolower(int c) {
|
|
|
|
return ((risupper(c)) ? (c | ('a' - 'A')) : c);
|
2007-11-23 14:32:08 +08:00
|
|
|
}
|
2007-11-23 21:20:46 +08:00
|
|
|
|
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Locale insensitive toupper(3)
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-03-18 15:10:13 +08:00
|
|
|
static inline int rtoupper(int c) {
|
|
|
|
return ((rislower(c)) ? (c & ~('a' - 'A')) : c);
|
2007-11-23 14:32:08 +08:00
|
|
|
}
|
|
|
|
|
2008-04-08 17:35:36 +08:00
|
|
|
/**
|
|
|
|
* Convert hex to binary nibble.
|
|
|
|
* @param c hex character
|
|
|
|
* @return binary nibble
|
|
|
|
*/
|
2008-10-09 21:14:23 +08:00
|
|
|
RPM_GNUC_CONST
|
2008-04-08 17:35:36 +08:00
|
|
|
static inline unsigned char rnibble(char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return (c - '0');
|
|
|
|
if (c >= 'A' && c <= 'F')
|
|
|
|
return (c - 'A') + 10;
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return (c - 'a') + 10;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-31 14:33:52 +08:00
|
|
|
/**
|
|
|
|
* Test for string equality
|
|
|
|
* @param s1 string 1
|
|
|
|
* @param s2 string 2
|
|
|
|
* @return 0 if strings differ, 1 if equal
|
|
|
|
*/
|
|
|
|
static inline int rstreq(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
return (strcmp(s1, s2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for string equality
|
|
|
|
* @param s1 string 1
|
|
|
|
* @param s2 string 2
|
|
|
|
* @param n compare at most n characters
|
|
|
|
* @return 0 if strings differ, 1 if equal
|
|
|
|
*/
|
2009-08-31 15:44:35 +08:00
|
|
|
static inline int rstreqn(const char *s1, const char *s2, size_t n)
|
2009-08-31 14:33:52 +08:00
|
|
|
{
|
|
|
|
return (strncmp(s1, s2, n) == 0);
|
|
|
|
}
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2007-11-23 14:32:08 +08:00
|
|
|
* Locale insensitive strcasecmp(3).
|
|
|
|
*/
|
2008-10-09 21:17:53 +08:00
|
|
|
RPM_GNUC_PURE
|
2008-03-18 15:10:13 +08:00
|
|
|
int rstrcasecmp(const char * s1, const char * s2) ;
|
2007-11-23 14:32:08 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2007-11-23 14:32:08 +08:00
|
|
|
* Locale insensitive strncasecmp(3).
|
|
|
|
*/
|
2008-10-09 21:17:53 +08:00
|
|
|
RPM_GNUC_PURE
|
2008-03-18 15:10:13 +08:00
|
|
|
int rstrncasecmp(const char *s1, const char * s2, size_t n) ;
|
2007-11-23 14:32:08 +08:00
|
|
|
|
2008-03-18 16:17:03 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* asprintf() clone
|
|
|
|
*/
|
|
|
|
int rasprintf(char **strp, const char *fmt, ...) RPM_GNUC_PRINTF(2, 3);
|
|
|
|
|
2008-04-15 22:30:58 +08:00
|
|
|
/** \ingroup rpmstring
|
2008-04-17 14:57:43 +08:00
|
|
|
* Concatenate two strings with dynamically (re)allocated memory.
|
2008-04-15 22:30:58 +08:00
|
|
|
* @param dest pointer to destination string
|
|
|
|
* @param src source string
|
|
|
|
* @return realloc'd dest with src appended
|
|
|
|
*/
|
|
|
|
char *rstrcat(char **dest, const char *src);
|
|
|
|
|
2008-04-17 14:57:43 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* Concatenate multiple strings with dynamically (re)allocated memory.
|
|
|
|
* @param dest pointer to destination string
|
|
|
|
* @param arg NULL terminated list of strings to concatenate
|
|
|
|
* @return realloc'd dest with strings appended
|
|
|
|
*/
|
2008-04-17 16:09:03 +08:00
|
|
|
char *rstrscat(char **dest, const char *arg, ...) RPM_GNUC_NULL_TERMINATED;
|
2008-04-17 14:57:43 +08:00
|
|
|
|
2008-04-17 22:33:57 +08:00
|
|
|
/** \ingroup rpmstring
|
|
|
|
* strlcpy() clone:
|
|
|
|
* Copy src to string dest of size n. At most n-1 characters
|
|
|
|
* will be copied. Always zero-terminates (unless n == 0).
|
|
|
|
* Length of src is returned; if retval >= n, truncation occurred.
|
|
|
|
* @param dest destination buffer
|
|
|
|
* @param src string to copy
|
|
|
|
* @param n destination buffer size
|
|
|
|
* @return length of src string
|
|
|
|
*/
|
|
|
|
size_t rstrlcpy(char *dest, const char *src, size_t n);
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2007-11-23 14:21:23 +08:00
|
|
|
* Remove occurences of trailing character from string.
|
|
|
|
* @param s string
|
|
|
|
* @param c character to strip
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
char * stripTrailingChar(char * s, char c);
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2007-10-08 16:05:06 +08:00
|
|
|
*/
|
|
|
|
typedef struct StringBufRec *StringBuf;
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
StringBuf newStringBuf(void);
|
2001-04-10 20:36:45 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
StringBuf freeStringBuf( StringBuf sb);
|
2001-04-10 20:36:45 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
void truncStringBuf(StringBuf sb);
|
2001-04-10 20:36:45 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
char * getStringBuf(StringBuf sb);
|
2001-04-10 20:36:45 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
void stripTrailingBlanksStringBuf(StringBuf sb);
|
1995-12-14 00:01:36 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
1995-12-14 00:01:36 +08:00
|
|
|
#define appendStringBuf(sb, s) appendStringBufAux(sb, s, 0)
|
2001-04-10 20:36:45 +08:00
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
1995-12-14 00:01:36 +08:00
|
|
|
#define appendLineStringBuf(sb, s) appendStringBufAux(sb, s, 1)
|
|
|
|
|
2007-11-23 21:20:46 +08:00
|
|
|
/** \ingroup rpmstring
|
2001-04-10 20:36:45 +08:00
|
|
|
*/
|
2007-09-12 01:07:39 +08:00
|
|
|
void appendStringBufAux(StringBuf sb, const char * s, int nl);
|
1995-12-14 00:01:36 +08:00
|
|
|
|
1999-07-14 05:37:57 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-11-23 14:11:42 +08:00
|
|
|
#endif /* _RPMSTRING_H_ */
|