Krita Source Code Documentation
Loading...
Searching...
No Matches
utils.c File Reference
#include "xcftools.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>

Go to the source code of this file.

Functions

int closeout (FILE *f, const char *name)
 
void FatalBadXCF (const char *format,...)
 
void FatalGeneric (int status, const char *format,...)
 
void FatalUnexpected (const char *format,...)
 
void FatalUnsupportedXCF (const char *format,...)
 
void gpl_blurb (void)
 
FILE * openout (const char *name)
 
static void vFatalGeneric (int status, const char *format, va_list args)
 
int xcfCheckspace (uint32_t addr, int spaceafter, const char *format,...)
 
void xcffree (void *block)
 
voidxcfmalloc (size_t size)
 

Variables

const char * progname = "$0"
 
int verboseFlag = 0
 

Function Documentation

◆ closeout()

int closeout ( FILE * f,
const char * name )

Definition at line 152 of file utils.c.

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}
void FatalUnexpected(const char *format,...)
Definition utils.c:56
#define XCF_OK
Definition xcftools.h:106
#define XCF_ERROR
Definition xcftools.h:105
#define _(s)
Definition xcftools.h:32

References _, FatalUnexpected(), XCF_ERROR, and XCF_OK.

◆ FatalBadXCF()

void FatalBadXCF ( const char * format,
... )

Definition at line 66 of file utils.c.

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}
qreal v
static void vFatalGeneric(int status, const char *format, va_list args)
Definition utils.c:29
const char * progname
Definition utils.c:25

References _, progname, v, and vFatalGeneric().

◆ FatalGeneric()

void FatalGeneric ( int status,
const char * format,
... )

Definition at line 46 of file utils.c.

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}

References progname, v, and vFatalGeneric().

◆ FatalUnexpected()

void FatalUnexpected ( const char * format,
... )

Definition at line 56 of file utils.c.

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}

References progname, v, and vFatalGeneric().

◆ FatalUnsupportedXCF()

void FatalUnsupportedXCF ( const char * format,
... )

Definition at line 91 of file utils.c.

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}

References _, progname, v, and vFatalGeneric().

◆ gpl_blurb()

void gpl_blurb ( void )

Definition at line 102 of file utils.c.

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}
#define PACKAGE_STRING
Definition config.h:15

References _, PACKAGE_STRING, and progname.

◆ openout()

FILE * openout ( const char * name)

Definition at line 138 of file utils.c.

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}
#define XCF_PTR_EMPTY
Definition xcftools.h:107

References _, FatalUnexpected(), and XCF_PTR_EMPTY.

◆ vFatalGeneric()

static void vFatalGeneric ( int status,
const char * format,
va_list args )
static

Definition at line 29 of file utils.c.

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}
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)

References void().

◆ xcfCheckspace()

int xcfCheckspace ( uint32_t addr,
int spaceafter,
const char * format,
... )

Definition at line 75 of file utils.c.

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}
size_t xcf_length
Definition xcf-general.c:30

References _, progname, v, vFatalGeneric(), XCF_ERROR, xcf_length, and XCF_OK.

◆ xcffree()

void xcffree ( void * block)

Definition at line 125 of file utils.c.

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}
uint8_t * xcf_file
Definition xcf-general.c:29

References xcf_file, and xcf_length.

◆ xcfmalloc()

void * xcfmalloc ( size_t size)

Definition at line 114 of file utils.c.

115{
116 void *ptr = malloc(size);
117 if( !ptr ) {
118 FatalUnexpected(_("Out of memory"));
119 return XCF_PTR_EMPTY;
120 }
121 return ptr ;
122}

References _, FatalUnexpected(), and XCF_PTR_EMPTY.

Variable Documentation

◆ progname

const char* progname = "$0"

Definition at line 25 of file utils.c.

◆ verboseFlag

int verboseFlag = 0

Definition at line 26 of file utils.c.