Krita Source Code Documentation
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
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
25const char *progname = "$0" ;
26int verboseFlag = 0 ;
27
28static void
29vFatalGeneric(int status,const char *format, va_list args)
30{
31 (void) status; /* mark as unused */
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 /* don't exit here - Krita can't handle errors otherwise */
42 /* exit(status); */
43}
44
45void
46FatalGeneric(int status,const char* format,...)
47{
48 va_list v;
49 va_start(v,format);
50 if( format ) fprintf(stderr,"%s: ",progname);
51 vFatalGeneric(status,format,v);
52 va_end(v);
53}
54
55void
56FatalUnexpected(const char* format,...)
57{
58 va_list v;
59 va_start(v, format);
60 fprintf(stderr,"%s: ",progname);
61 vFatalGeneric(127, format, v);
62 va_end(v);
63}
64
65void
66FatalBadXCF(const char* format,...)
67{
68 va_list v; va_start(v,format);
69 fprintf(stderr,"%s: %s:\n ",progname,_("Corrupted or malformed XCF file"));
70 vFatalGeneric(125,format,v) ;
71 va_end(v);
72}
73
74int
75xcfCheckspace(uint32_t addr,int spaceafter,const char *format,...)
76{
77 if( xcf_length < spaceafter || addr > xcf_length - spaceafter ) {
78 va_list v;
79 va_start(v,format);
80 fprintf(stderr,"%s: %s\n ",progname,_("Corrupted or truncated XCF file"));
81 fprintf(stderr,"(0x%" PRIXPTR " bytes): ",(uintptr_t)xcf_length);
82 vFatalGeneric(125,format,v) ;
83 va_end(v);
84 return XCF_ERROR;
85 }
86 return XCF_OK;
87}
88
89
90void
91FatalUnsupportedXCF(const char* format,...)
92{
93 va_list v;
94 va_start(v,format);
95 fprintf(stderr,"%s: %s\n ",progname,
96 _("The image contains features not understood by this program:"));
97 vFatalGeneric(123,format,v) ;
98 va_end(v);
99}
100
101void
103{
104 fprintf(stderr,PACKAGE_STRING "\n");
105 fprintf(stderr,
106 _("Type \"%s -h\" to get an option summary.\n"),progname);
107 /* don't exit here - Krita will close otherwise */
108 /* exit(1) ; */
109}
110
111/* ******************************************************* */
112
113void *
114xcfmalloc(size_t size)
115{
116 void *ptr = malloc(size);
117 if( !ptr ) {
118 FatalUnexpected(_("Out of memory"));
119 return XCF_PTR_EMPTY;
120 }
121 return ptr ;
122}
123
124void
125xcffree(void *block)
126{
127 if( xcf_file &&
128 (uint8_t*)block >= xcf_file &&
129 (uint8_t*)block < xcf_file + xcf_length )
130 ;
131 else
132 free(block);
133}
134
135/* ******************************************************* */
136
137FILE *
138openout(const char *name)
139{
140 FILE *newfile ;
141 if( strcmp(name,"-") == 0 )
142 return stdout ;
143 newfile = fopen(name,"wb") ;
144 if( newfile == NULL ) {
145 FatalUnexpected(_("!Cannot create file %s"),name);
146 return XCF_PTR_EMPTY;
147 }
148 return newfile ;
149}
150
151int
152closeout(FILE *f,const char *name)
153{
154 if( f == NULL )
155 return XCF_OK;
156 if( fflush(f) == 0 ) {
157 errno = 0 ;
158 if( !ferror(f) ) {
159 if( fclose(f) == 0 )
160 return XCF_OK;
161 } else if( errno == 0 ) {
162 /* Attempt to coax a valid errno out of the standard library,
163 * following an idea by Bruno Haible
164 * https://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00157.html
165 */
166 if( fputc('\0', f) != EOF &&
167 fflush(f) == 0 )
168 errno = EIO ; /* Argh, everything succedes. Just call it an I/O error */
169 }
170 }
171 FatalUnexpected(_("!Error writing file %s"),name);
172 return XCF_ERROR;
173}
qreal v
#define PACKAGE_STRING
Definition config.h:15
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
FILE * openout(const char *name)
Definition utils.c:138
void FatalUnsupportedXCF(const char *format,...)
Definition utils.c:91
void * xcfmalloc(size_t size)
Definition utils.c:114
int closeout(FILE *f, const char *name)
Definition utils.c:152
void xcffree(void *block)
Definition utils.c:125
void FatalGeneric(int status, const char *format,...)
Definition utils.c:46
void FatalUnexpected(const char *format,...)
Definition utils.c:56
int verboseFlag
Definition utils.c:26
void FatalBadXCF(const char *format,...)
Definition utils.c:66
int xcfCheckspace(uint32_t addr, int spaceafter, const char *format,...)
Definition utils.c:75
static void vFatalGeneric(int status, const char *format, va_list args)
Definition utils.c:29
void gpl_blurb(void)
Definition utils.c:102
const char * progname
Definition utils.c:25
size_t xcf_length
Definition xcf-general.c:30
uint8_t * xcf_file
Definition xcf-general.c:29
#define XCF_OK
Definition xcftools.h:106
#define XCF_ERROR
Definition xcftools.h:105
#define XCF_PTR_EMPTY
Definition xcftools.h:107
#define _(s)
Definition xcftools.h:32