filters

xcf-read.cpp
1/* The GIMP -- an image manipulation program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "config.h"
20
21#include <stdio.h>
22
23#include <glib-object.h>
24
25#include "libgimpbase/gimpbase.h"
26
27#include "xcf-read.h"
28
29#include "gimp-intl.h"
30
31
32TQ_UINT32
33xcf_read_int32 (FILE *fp,
34 TQ_INT32 *data,
35 TQ_INT32 count)
36{
37 TQ_UINT32 total;
38
39 total = count;
40 if (count > 0)
41 {
42 xcf_read_int8 (fp, (TQ_UINT8 *) data, count * 4);
43
44 while (count--)
45 {
46 *data = g_ntohl (*data);
47 data++;
48 }
49 }
50
51 return total * 4;
52}
53
54TQ_UINT32
55xcf_read_float (FILE *fp,
56 float *data,
57 TQ_INT32 count)
58{
59 return xcf_read_int32 (fp, (TQ_INT32 *) ((void *) data), count);
60}
61
62TQ_UINT32
63xcf_read_int8 (FILE *fp,
64 TQ_UINT8 *data,
65 TQ_INT32 count)
66{
67 TQ_UINT32 total;
68 TQ_INT32 bytes;
69
70 total = count;
71 while (count > 0)
72 {
73 bytes = fread ((char *) data, sizeof (char), count, fp);
74 if (bytes <= 0) /* something bad happened */
75 break;
76 count -= bytes;
77 data += bytes;
78 }
79
80 return total;
81}
82
83TQ_UINT32
84xcf_read_string (FILE *fp,
85 TQCString **data,
86 TQ_INT32 count)
87{
88 TQ_INT32 tmp;
89 TQ_UINT32 total;
90 TQ_INT32 i;
91
92 total = 0;
93 for (i = 0; i < count; i++)
94 {
95 total += xcf_read_int32 (fp, &tmp, 1);
96 if (tmp > 0)
97 {
98 TQCString *str;
99
100 str = g_new (TQCString, tmp);
101 total += xcf_read_int8 (fp, (TQ_UINT8*) str, tmp);
102
103 if (str[tmp - 1] != '\0')
104 str[tmp - 1] = '\0';
105
106 data[i] = gimp_any_to_utf8 (str, -1,
107 _("Invalid UTF-8 string in XCF file"));
108
109 g_free (str);
110 }
111 else
112 {
113 data[i] = NULL;
114 }
115 }
116
117 return total;
118}