~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/formats/xcf/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Generic support functions for Xcftools
 
2
 *
 
3
 * This file was written by Henning Makholm <henning@makholm.net>
 
4
 * It is hereby in the public domain.
 
5
 * 
 
6
 * In jurisdictions that do not recognise grants of copyright to the
 
7
 * public domain: I, the author and (presumably, in those jurisdictions)
 
8
 * copyright holder, hereby permit anyone to distribute and use this code,
 
9
 * in source code or binary form, with or without modifications. This
 
10
 * permission is world-wide and irrevocable.
 
11
 *
 
12
 * Of course, I will not be liable for any errors or shortcomings in the
 
13
 * code, since I give it away without asking any compenstations.
 
14
 *
 
15
 * If you use or distribute this code, I would appreciate receiving
 
16
 * credit for writing it, in whichever way you find proper and customary.
 
17
 */
 
18
 
 
19
#include "xcftools.h"
 
20
#include <string.h>
 
21
#include <stdarg.h>
 
22
#include <stdlib.h>
 
23
#include <errno.h>
 
24
 
 
25
const char *progname = "$0" ;
 
26
int verboseFlag = 0 ;
 
27
 
 
28
 
 
29
static void  __ATTRIBUTE__((noreturn))
 
30
vFatalGeneric(int status,const char *format,va_list args)
 
31
{
 
32
  if( format ) {
 
33
    if( *format == '!' ) {
 
34
      vfprintf(stderr,format+1,args);
 
35
      fprintf(stderr,": %s\n",strerror(errno));
 
36
    } else {
 
37
      vfprintf(stderr,format,args);
 
38
      fputc('\n',stderr);
 
39
    }
 
40
  }
 
41
  exit(status);
 
42
}
 
43
 
 
44
void
 
45
FatalGeneric(int status,const char* format,...)
 
46
{
 
47
  va_list v; va_start(v,format);
 
48
  if( format ) fprintf(stderr,"%s: ",progname);
 
49
  vFatalGeneric(status,format,v);
 
50
}
 
51
 
 
52
void
 
53
FatalUnexpected(const char* format,...)
 
54
{
 
55
  va_list v; va_start(v,format);
 
56
  fprintf(stderr,"%s: ",progname);
 
57
  vFatalGeneric(127,format,v) ;
 
58
}
 
59
 
 
60
void
 
61
FatalBadXCF(const char* format,...)
 
62
{
 
63
  va_list v; va_start(v,format);
 
64
  fprintf(stderr,"%s: %s:\n ",progname,_("Corrupted or malformed XCF file"));
 
65
  vFatalGeneric(125,format,v) ;
 
66
}
 
67
 
 
68
void
 
69
xcfCheckspace(uint32_t addr,int spaceafter,const char *format,...)
 
70
{
 
71
  if( xcf_length < spaceafter || addr > xcf_length - spaceafter ) {
 
72
    va_list v; va_start(v,format);
 
73
    fprintf(stderr,"%s: %s\n ",progname,_("Corrupted or truncated XCF file"));
 
74
    fprintf(stderr,"(0x%" PRIXPTR " bytes): ",(uintptr_t)xcf_length);
 
75
    vFatalGeneric(125,format,v) ;
 
76
  }
 
77
}
 
78
 
 
79
 
 
80
void
 
81
FatalUnsupportedXCF(const char* format,...)
 
82
{
 
83
  va_list v; va_start(v,format);
 
84
  fprintf(stderr,"%s: %s\n ",progname,
 
85
          _("The image contains features not understood by this program:"));
 
86
  vFatalGeneric(123,format,v) ;
 
87
}
 
88
 
 
89
void
 
90
gpl_blurb(void)
 
91
{
 
92
  fprintf(stderr,PACKAGE_STRING "\n");
 
93
  fprintf(stderr,
 
94
          _("Type \"%s -h\" to get an option summary.\n"),progname);
 
95
  exit(1) ;
 
96
}
 
97
 
 
98
/* ******************************************************* */
 
99
 
 
100
void *
 
101
xcfmalloc(size_t size)
 
102
{
 
103
  void *ptr = malloc(size);
 
104
  if( !ptr )
 
105
    FatalUnexpected(_("Out of memory"));
 
106
  return ptr ;
 
107
}
 
108
 
 
109
void
 
110
xcffree(void *block)
 
111
{
 
112
  if( xcf_file &&
 
113
      (uint8_t*)block >= xcf_file &&
 
114
      (uint8_t*)block < xcf_file + xcf_length )
 
115
    ;
 
116
  else
 
117
    free(block);
 
118
}
 
119
 
 
120
/* ******************************************************* */
 
121
 
 
122
FILE *
 
123
openout(const char *name)
 
124
{
 
125
  FILE *newfile ;
 
126
  if( strcmp(name,"-") == 0 )
 
127
    return stdout ;
 
128
  newfile = fopen(name,"wb") ;
 
129
  if( newfile == NULL )
 
130
    FatalUnexpected(_("!Cannot create file %s"),name);
 
131
  return newfile ;
 
132
}
 
133
 
 
134
void
 
135
closeout(FILE *f,const char *name)
 
136
{
 
137
  if( f == NULL )
 
138
    return ;
 
139
  if( fflush(f) == 0 ) {
 
140
    errno = 0 ;
 
141
    if( !ferror(f) ) {
 
142
      if( fclose(f) == 0 )
 
143
        return ;
 
144
    } else if( errno == 0 ) {
 
145
      /* Attempt to coax a valid errno out of the standard library,
 
146
       * following an idea by Bruno Haible
 
147
       * http://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00157.html
 
148
       */
 
149
      if( fputc('\0', f) != EOF &&
 
150
          fflush(f) == 0 )
 
151
        errno = EIO ; /* Argh, everything succeds. Just call it an I/O error */
 
152
    }
 
153
  }
 
154
  FatalUnexpected(_("!Error writing file %s"),name);
 
155
}
 
156
 
 
157
        
 
158
      
 
159