*/
/**
* \defgroup MiscRout Miscellaneous routines
+ * \ingroup tools
*/
/*@{*/
*/
int num_tokens(char *source, char tok)
{
- int a;
+ int a = 0;
int count = 1;
if (source == NULL)
void remove_token(char *source, int parmnum, char separator)
{
int i;
- int len;
+ int len, slen;
int curr_parm;
int start, end;
start = (-1);
end = (-1);
- if (strlen(source) == 0) {
+ slen = strlen(source);
+ if (slen == 0) {
return;
}
- for (i = 0; i < strlen(source); ++i) {
+ for (i = 0;
+ ( (i < slen) && (end == -1) );
+ ++i) {
if ((start < 0) && (curr_parm == parmnum)) {
start = i;
}
}
if (end < 0)
- end = strlen(source);
+ end = slen;
- strcpy(&source[start], &source[end]);
+ memmove(&source[start], &source[end], slen - end + 1);
}
* \param ch the char to search
* \return the position inside of st
*/
-int haschar(st, ch)
-char st[];
-char ch;
+int haschar(char *st,char ch)
{
int a, b;
b = 0;
/**
* \brief Utility function to "readline" from memory
- * \param start position in buf
- * \param buf the buffer to create string in???
- * \param maxlen how big may the buffer get?
- * \return new pointer to read string
+ * \param start Location in memory from which we are reading.
+ * \param buf the buffer to place the string in.
+ * \param maxlen Size of string buffer
+ * \return Pointer to the source memory right after we stopped reading.
*/
char *memreadline(char *start, char *buf, int maxlen)
{
*/
void striplt(char *buf)
{
- if (strlen(buf) == 0) return;
- while ((strlen(buf) > 0) && (isspace(buf[0])))
- strcpy(buf, &buf[1]);
- if (strlen(buf) == 0) return;
- while (isspace(buf[strlen(buf) - 1]))
- buf[strlen(buf) - 1] = 0;
+ long len;
+
+ len = strlen(buf);
+ if (len == 0) return;
+ while ((len > 0) && (isspace(buf[0]))){
+ memmove (buf, &buf[1], len);
+ len --;
+ }
+ if (len == 0) return;
+ while (isspace(buf[len - 1])){
+ buf[len - 1] = 0;
+ len --;
+ }
}
/**
* \brief Determine whether the specified message number is contained within the
* specified set.
- * \param mset stringset???
- * \param msgnum a citadel server indexnumber
- * \return yes / no ???
+ *
+ * \param mset Message set string
+ * \param msgnum Message number we are looking for
+ *
+ * \return Nonzero if the specified message number is in the specified message set string.
*/
int is_msg_in_mset(char *mset, long msgnum) {
int num_sets;
* \param sourcelen the length of the source data (may contain string terminators)
*/
-void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
+void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
{
int i, hiteof = FALSE;
int spos = 0;
dest[dpos] = 0;
}
thisline += 4;
- if (thisline > 70) {
+ if ( (linebreaks) && (thisline > 70) ) {
dest[dpos++] = '\r';
dest[dpos++] = '\n';
dest[dpos] = 0;
}
}
}
- if (thisline > 70) {
+ if ( (linebreaks) && (thisline > 70) ) {
dest[dpos++] = '\r';
dest[dpos++] = '\n';
dest[dpos] = 0;
/**
* \brief Convert base64-encoded to binary.
* It will stop after reading 'length' bytes.
- * \param dest the destination buffer
- * \param source the encrypted string
- * \param length the maximal length of dest???
- * \return the length actual length of the decoded data.
+ *
+ * \param dest The destination buffer
+ * \param source The base64 data to be decoded.
+ * \param length The number of bytes to decode.
+ * \return The actual length of the decoded data.
*/
int CtdlDecodeBase64(char *dest, const char *source, size_t length)
{
/**
* \brief Generate a new, globally unique UID parameter for a calendar etc. object
- * \param buf uid to create ????
+ *
+ * \param buf String buffer into which our newly created UUID should be placed
*/
void generate_uuid(char *buf) {
static int seq = 0;
- sprintf(buf, "%s-%lx-%x-%x",
+ sprintf(buf, "%s-%lx-%lx-%x",
serv_info.serv_nodename,
(long)time(NULL),
- getpid(),
+ (long)getpid(),
(seq++)
);
}
+/*
+ * \brief case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ *
+ * \param text String to be searched
+ * \param pattern String to search for
+ */
+char *bmstrcasestr(char *text, char *pattern) {
+
+ register unsigned char *p, *t;
+ register int i, j, *delta;
+ register size_t p1;
+ int deltaspace[256];
+ size_t textlen;
+ size_t patlen;
+
+ textlen = strlen (text);
+ patlen = strlen (pattern);
+
+ /* algorithm fails if pattern is empty */
+ if ((p1 = patlen) == 0)
+ return (text);
+
+ /* code below fails (whenever i is unsigned) if pattern too long */
+ if (p1 > textlen)
+ return (NULL);
+
+ /* set up deltas */
+ delta = deltaspace;
+ for (i = 0; i <= 255; i++)
+ delta[i] = p1;
+ for (p = (unsigned char *) pattern, i = p1; --i > 0;)
+ delta[tolower(*p++)] = i;
+
+ /*
+ * From now on, we want patlen - 1.
+ * In the loop below, p points to the end of the pattern,
+ * t points to the end of the text to be tested against the
+ * pattern, and i counts the amount of text remaining, not
+ * including the part to be tested.
+ */
+ p1--;
+ p = (unsigned char *) pattern + p1;
+ t = (unsigned char *) text + p1;
+ i = textlen - patlen;
+ while(1) {
+ if (tolower(p[0]) == tolower(t[0])) {
+ if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
+ return ((char *)t - p1);
+ }
+ }
+ j = delta[tolower(t[0])];
+ if (i < j)
+ break;
+ i -= j;
+ t += j;
+ }
+ return (NULL);
+}
+
+
+
+
+
/*@}*/