aboutsummaryrefslogtreecommitdiffstats
path: root/daemon/string.c
blob: 668466401ffa62b1fccaa19e15b6561b29a89d7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * string.c - syslogd implementation for windows, reference counted strings
 *
 * Created by Alexander Yaworsky
 *
 * THIS SOFTWARE IS NOT COPYRIGHTED
 *
 * This source code is offered for use in the public domain. You may
 * use, modify or distribute it freely.
 *
 * This code is distributed in the hope that it will be useful but
 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
 * DISCLAIMED. This includes but is not limited to warranties of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>
#include <string.h>
#include <windows.h>

#include <glib.h>
#include <glib/gprintf.h>

#include <syslog.h>
#include <syslogd.h>

struct string* string_new( const gchar* init )
{
    struct string *s = g_malloc( sizeof(struct string) );
    s->refcount = 1;
    s->gstr = g_string_new( init );
    return s;
}

struct string* string_new_len( const gchar* init, gssize len )
{
    struct string *s = g_malloc( sizeof(struct string) );
    s->refcount = 1;
    s->gstr = g_string_new_len( init, len );
    return s;
}

struct string* string_addref( struct string* s )
{
    InterlockedIncrement( &s->refcount );
    return s;
}

void string_release( struct string* s )
{
    if( InterlockedDecrement( &s->refcount ) )
        return;

    g_string_free( s->gstr, TRUE );
    g_free( s );
}

gsize string_concat( gchar** result, struct string* s, ... )
{
    va_list args;
    struct string *str;
    gsize length = s->gstr->len;
    gchar *p;

    va_start( args, s );
    while( (str = va_arg( args, struct string* )) )
        length += str->gstr->len;
    va_end( args );

    *result = g_malloc( length + 1 );

    memcpy( *result, s->gstr->str, s->gstr->len );
    p = *result + s->gstr->len;

    va_start( args, s );
    while( (str = va_arg( args, struct string* )) )
    {
        memcpy( p, str->gstr->str, str->gstr->len );
        p += str->gstr->len;
    }
    *p = 0;
    va_end( args );

    return length;
}

struct string* string_vprintf( gchar* fmt, va_list args )
{
    struct string *s;
    gchar *buf;
    int len;

    len = g_vasprintf( &buf, fmt, args );
    s = string_new_len( buf, len );
    g_free( buf );
    return s;
}

struct string* string_printf( gchar* fmt, ... )
{
    struct string *s;
    va_list args;

    va_start( args, fmt );
    s = string_vprintf( fmt, args );
    va_end( args );
    return s;
}

int string_compare( struct string* s1, struct string* s2 )
{
    if( s1 == s2 )
        return 0;
    return strcmp( s1->gstr->str, s2->gstr->str );
}