~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/xcf/xcf-write.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h> /* strlen */
 
23
#include <errno.h>
 
24
 
 
25
#include <glib.h>
 
26
 
 
27
#include "xcf-write.h"
 
28
 
 
29
 
 
30
#include "gimp-intl.h"
 
31
 
 
32
guint
 
33
xcf_write_int32 (FILE     *fp,
 
34
                 guint32  *data,
 
35
                 gint      count,
 
36
                 GError  **error)
 
37
{
 
38
  GError  *tmp_error = NULL;
 
39
  guint32  tmp;
 
40
  gint     i;
 
41
 
 
42
  if (count > 0)
 
43
    {
 
44
      for (i = 0; i < count; i++)
 
45
        {
 
46
          tmp = g_htonl (data[i]);
 
47
          xcf_write_int8 (fp, (guint8*) &tmp, 4, &tmp_error);
 
48
          if (tmp_error)
 
49
            {
 
50
              g_propagate_error (error, tmp_error);
 
51
 
 
52
              return i * 4;
 
53
            }
 
54
        }
 
55
    }
 
56
 
 
57
  return count * 4;
 
58
}
 
59
 
 
60
guint
 
61
xcf_write_float (FILE     *fp,
 
62
                 gfloat   *data,
 
63
                 gint      count,
 
64
                 GError  **error)
 
65
{
 
66
  return xcf_write_int32 (fp, (guint32 *)((void *)data), count, error);
 
67
}
 
68
 
 
69
guint
 
70
xcf_write_int8 (FILE     *fp,
 
71
                guint8   *data,
 
72
                gint      count,
 
73
                GError  **error)
 
74
{
 
75
  guint total;
 
76
  gint  bytes;
 
77
 
 
78
  total = count;
 
79
  while (count > 0)
 
80
    {
 
81
      bytes = fwrite ((gchar*) data, sizeof (gchar), count, fp);
 
82
 
 
83
      if (bytes == 0)
 
84
        {
 
85
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
 
86
                       _("Error writing XCF: %s"), g_strerror (errno));
 
87
 
 
88
          return total;
 
89
        }
 
90
 
 
91
      count -= bytes;
 
92
      data += bytes;
 
93
    }
 
94
 
 
95
  return total;
 
96
}
 
97
 
 
98
guint
 
99
xcf_write_string (FILE     *fp,
 
100
                  gchar   **data,
 
101
                  gint      count,
 
102
                  GError  **error)
 
103
{
 
104
  GError  *tmp_error = NULL;
 
105
  guint32  tmp;
 
106
  guint    total;
 
107
  gint     i;
 
108
 
 
109
  total = 0;
 
110
  for (i = 0; i < count; i++)
 
111
    {
 
112
      if (data[i])
 
113
        tmp = strlen (data[i]) + 1;
 
114
      else
 
115
        tmp = 0;
 
116
 
 
117
      xcf_write_int32 (fp, &tmp, 1, &tmp_error);
 
118
      if (tmp_error)
 
119
        {
 
120
          g_propagate_error (error, tmp_error);
 
121
          return total;
 
122
        }
 
123
 
 
124
      if (tmp > 0)
 
125
        xcf_write_int8 (fp, (guint8*) data[i], tmp, &tmp_error);
 
126
      if (tmp_error)
 
127
        {
 
128
          g_propagate_error (error, tmp_error);
 
129
          return total;
 
130
        }
 
131
 
 
132
      total += 4 + tmp;
 
133
    }
 
134
 
 
135
  return total;
 
136
}