about summary refs log tree commit diff
path: root/converter/other/cameratopam/stdio_nofail.c
blob: 97a8796d38e0615c1fde155233ded184ec5247de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <netpbm/pm.h>

#include "stdio_nofail.h"



size_t
fread_or_eof_nofail(void * const ptr,
                    size_t const size,
                    size_t const nmemb,
                    FILE * const streamP) {

    size_t rc;

    rc = fread(ptr, size, nmemb, streamP);

    if (rc < nmemb) {
        if (!feof(streamP))
            pm_error("File read failed.  Errno=%d (%s)",
                     errno, strerror(errno));
    }
    return rc;
}



void
fread_nofail(void * const ptr,
             size_t const size,
             size_t const nmemb,
             FILE * const streamP) {

    size_t rc;

    rc = fread(ptr, size, nmemb, streamP);

    if (rc < nmemb) {
        if (feof(streamP))
            pm_error("File read failed.  Premature end of file");
        else
            pm_error("File read failed.  Errno=%d (%s)",
                     errno, strerror(errno));
    }
}



int
fgetc_nofail(FILE * streamP) {

    int rc;

    rc = fgetc(streamP);

    if (rc == EOF) {
        if (feof(streamP))
            pm_error("File read failed.  Premature end of file");
        else
            pm_error("File read failed.  Errno=%d (%s)",
                     errno, strerror(errno));
    }
    return rc;
}



int
fseek_nofail(FILE * const streamP,
             long   const offset,
             int    const whence) {

    int rc;

    rc = fseek(streamP, offset, whence);

    if (rc < 0)
        pm_error("File seek failed.  Errno=%d (%s)", errno, strerror(errno));

    return rc;
}



long
ftell_nofail(FILE * const streamP) {

    long rc;

    rc = ftell(streamP);

    if (rc < 0)
        pm_error("File position query failed.  Errno=%d (%s)",
                 errno, strerror(errno));

    return rc;
}



char *
fgets_nofail(char * const s,
             int    const size,
             FILE * const streamP) {

    char * rc;

    rc = fgets(s, size, streamP);

    if (ferror(streamP))
        pm_error("File read failed.  Errno=%d (%s)", errno, strerror(errno));

    return rc;
}