Subversion Repositories mdb

Rev

Rev 4 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include "helplib.h"

char *readLine(int fd, char *buf, int bufLen)
{
        int i, end;
        char ch, *p;
        
        if (fd <= 0)
        {
                syslog(LOG_DAEMON,"Function readLine was called with an invalid file descriptor of %d!", fd);
                return NULL;
        }
        
        i = end = 0;
        p = buf;
        
        while (read(fd, &ch, 1) > 0)
        {
                end = 1;
                
                if (ch == 0x0a)
                {
                        *p = 0;
                        return buf;
                }
                
                if (ch == 0x0d)      // ignore this!
                        continue;
                
                if (i < (bufLen - 1))
                {
                        *p = ch;
                        p++;
                        i++;
                }
        }
        
        *p = 0;
        
        if (end)
                return buf;
        else
                return NULL;
}

char *trim(char *str)
{
        char *p1, *p2, *p;
        
        if (!str)
                return NULL;
        
        if (!strlen(str))
                return str;
        
        p = str;
        p1 = p2 = NULL;
        
        while (*p)
        {
                if (!p1 && *p != ' ')
                {
                        p1 = p;
                        break;
                }
                
                p++;
        }
        
        p2 = str + (strlen(str) - 1);
        
        while (p2 > str && *p2 == ' ')
                p2--;
        
        if (p2)
                *(p2+1) = 0;
        
        if (p1)
        {
                char *buf = strdup (p1);
                strcpy (str, buf);
                free (buf);
        }
        
        return str;
}

char *remove_string(char *str, char *search, char *ret)
{
        char *p;
        
        if (!strlen(str) || !strlen(search))
                return NULL;
        
        if ((p = strstr(str, search)) != NULL)
        {
                int len = strlen(search);
                
                strncpy(ret, str, p - str + len);
                ret[p - str + len] = 0;
                memmove(str, p + len, strlen(p+len));
                str[strlen(p+len)] = 0;
                return ret;
        }
        
        return NULL;
}