summaryrefslogtreecommitdiffstats
path: root/filters/chalk/xcf/xcf/xcf-write.cc
blob: 4010ab18f1a29fd59946ac4f9772fbc03b70fe06 (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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <stdio.h>
#include <string.h> /* strlen */
#include <errno.h>

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <netinet/in.h>
#include <limits.h>
#include <stdlib.h>
#include <cfloat>

#include "xcf-write.h"

TQ_UINT32 xcf_write_int32  (FILE *fp, TQ_INT32 *data, TQ_INT32 count);
{
    TQ_INT32  tmp;
    TQ_INT32    i;

    if (count > 0)
    {
        for (i = 0; i < count; i++)
        {
            tmp = htonl (data[i]);
            xcf_write_int8 (fp, (TQ_UINT8*) &tmp, 4);

            if (fp->status() != IO_Ok)
            {
                return i * 4;
            }
        }
    }

    return count * 4;
}

TQ_UINT32 xcf_write_float  (FILE *fp, float *data, TQ_INT32 count);
{
    return xcf_write_int32 (fp, (TQ_INT32 *)((void *)data), count, error);
}

TQ_UINT32 xcf_write_int8 (FILE *fp, TQ_UINT8 *data, TQ_INT32 count);
{
    TQ_INT32  bytes;
    bytes = fp->writeBlock( data, count );
    return bytes;
}

TQ_UINT32 xcf_write_string (FILE *fp, TQCString *data, TQ_INT32 count);
{
    GError  *tmp_error = NULL;
    TQ_INT32  tmp;
    TQ_UINT32    total;
    TQ_INT32     i;

    total = 0;
    for (i = 0; i < count; i++)
    {
        if (data[i])
            tmp = strlen (data[i]) + 1;
        else
            tmp = 0;

        xcf_write_int32 (fp, &tmp, 1, &tmp_error);
        if (tmp_error)
        {
            g_propagate_error (error, tmp_error);
            return total;
        }

        if (tmp > 0)
            xcf_write_int8 (fp, (TQ_UINT8*) data[i], tmp, &tmp_error);
        if (tmp_error)
        {
            g_propagate_error (error, tmp_error);
            return total;
        }

        total += 4 + tmp;
    }

    return total;
}