Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ps/gsio.c
blob: 15c7f388733259c4da025988ec2d0fe5f494570c (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * gsio.c: an IO abstraction
 *
 * Copyright 2002 - 2005 the Free Software Foundation
 *
 * Author: Jaka Mocnik  <jaka@gnu.org>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <string.h>

#include <gsio.h>

#define CHUNK_SIZE 32768

typedef struct _GtkGSDocChunk GtkGSDocChunk;
struct _GtkGSDocChunk {
  gchar *buf, *ptr;
  guint len, max_len;
};

struct _GtkGSDocSink {
  GSList *chunks;
  GtkGSDocChunk *tail;
};

static GtkGSDocChunk *
gtk_gs_doc_chunk_new(guint size)
{
  GtkGSDocChunk *c;

  c = g_new0(GtkGSDocChunk, 1);
  if((c->buf = g_malloc(sizeof(gchar) * size)) == NULL) {
    g_free(c);
    return NULL;
  }
  c->ptr = c->buf;
  *c->ptr = '\0';
  c->max_len = size;
  c->len = 0;
  return c;
}

static void
gtk_gs_doc_chunk_free(GtkGSDocChunk * c)
{
  if(c->buf)
    g_free(c->buf);
  g_free(c);
}

GtkGSDocSink *
gtk_gs_doc_sink_new()
{
  GtkGSDocSink *sink;

  sink = g_new0(GtkGSDocSink, 1);
  return sink;
}

void
gtk_gs_doc_sink_free(GtkGSDocSink * sink)
{
  GSList *node;

  node = sink->chunks;
  while(node) {
    gtk_gs_doc_chunk_free((GtkGSDocChunk *) node->data);
    node = node->next;
  }
  g_slist_free(sink->chunks);
}

void
gtk_gs_doc_sink_write(GtkGSDocSink * sink, const gchar * buf, int len)
{
  gint real_len;

  if(sink->tail == NULL) {
    sink->tail = gtk_gs_doc_chunk_new(CHUNK_SIZE);
    sink->chunks = g_slist_append(sink->chunks, sink->tail);
  }

  real_len = MIN(sink->tail->max_len - sink->tail->len, len);
  if(real_len > 0) {
    strncpy(sink->tail->ptr, buf, real_len);
    sink->tail->ptr += real_len;
    sink->tail->len += real_len;
  }
  len -= real_len;
  if(len > 0) {
    sink->tail = NULL;
    gtk_gs_doc_sink_write(sink, buf + real_len, len);
  }
}

void
gtk_gs_doc_sink_printf_v(GtkGSDocSink * sink, const gchar * fmt, va_list ap)
{
  gint max_len, len;

  if(sink->tail == NULL) {
    sink->tail = gtk_gs_doc_chunk_new(CHUNK_SIZE);
    sink->chunks = g_slist_append(sink->chunks, sink->tail);
  }

  max_len = sink->tail->max_len - sink->tail->len;
  if(max_len > 0) {
    len = g_vsnprintf(sink->tail->ptr, max_len, fmt, ap);
    if(len >= max_len - 1) {
      /* force printf in the next chunk later on */
      max_len = 0;
      sink->tail = NULL;
    }
    else {
      sink->tail->ptr += len;
      sink->tail->len += len;
    }
  }
  if(max_len <= 0) {
    gtk_gs_doc_sink_printf(sink, fmt, ap);
  }
}

void
gtk_gs_doc_sink_printf(GtkGSDocSink * sink, const gchar * fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  gtk_gs_doc_sink_printf_v(sink, fmt, ap);
  va_end(ap);
}

gchar *
gtk_gs_doc_sink_get_buffer(GtkGSDocSink * sink)
{
  guint total;
  GSList *node;

  for(total = 0, node = sink->chunks; node; node = node->next) {
    total += ((GtkGSDocChunk *) node->data)->len;
  }
  if(total) {
    gchar *buf = g_malloc(sizeof(gchar) * (total + 1)), *ptr;
    if(!buf)
      return NULL;
    for(ptr = buf, node = sink->chunks; node; node = node->next) {
      memcpy(ptr,
             ((GtkGSDocChunk *) node->data)->buf,
             ((GtkGSDocChunk *) node->data)->len);
      ptr += ((GtkGSDocChunk *) node->data)->len;
    }
    buf[total] = '\0';
    return buf;
  }
  else
    return NULL;
}