File: write2file.cal

package info (click to toggle)
calc 2.15.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,848 kB
  • sloc: ansic: 62,147; makefile: 7,664; sh: 503; awk: 74; sed: 7
file content (121 lines) | stat: -rw-r--r-- 3,395 bytes parent folder | download
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
121
/*
 * write2file - write results to file
 *
 * This resource file serves as an example of how to perform file I/O.
 * This write2file hows how to perform file I/O and is provided as
 * an illustrative example.
 *
 * Copyright (C) 2023  Landon Curt Noll
 *
 * Calc is open software; you can redistribute it and/or modify it under
 * the terms of the version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * Calc is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
 * Public License for more details.
 *
 * A copy of version 2.1 of the GNU Lesser General Public License is
 * distributed with calc under the filename COPYING-LGPL.  You should have
 * received a copy with calc; if not, write to Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Under source code control:   2023/08/20 13:13:11
 * File existed as early as:    2023
 *
 * chongo <was here> /\oo/\     http://www.isthe.com/chongo/
 * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
 */


static outfilename;  /* where to write stuff */
static outfile;      /* open file stream on which to write */
static stderr;       /* write errors to standard error */


/*
 * setup
 */
if (!isstr(outfilename)) {
    outfilename = "./outfile";
}
if (!isfile(stderr)) {
    stderr = files(2);
    if (!isfile(stderr)) {
        printf("Warning: stderr as files(2) is not a file!\n");
    }
}


/*
 * try to create or open and truncate the outfile
 */
if (!isfile(outfile)) {
    /* open outfilename for writing at the beginning */
    outfile = fopen(outfilename, "w");

    /* firewall - check for failing to open */
    if (!isfile(outfile)) {
        fprintf(stderr, "failed to open: %s error(%d): %s\n", outfilename, errno(outfile), strerror(outfile));
    }
}


/*
 * write a value to a file given a format
 */
define w2f(filename, stream, fmt, value) {

    /* firewall */
    if (!isfile(stderr)) {
        printf("stderr is not an open file\n");
        return;
    }
    if (!isstr(filename)) {
        fprintf(stderr, "filename argument is not a string\n");
        return;
    }
    if (!isfile(stream)) {
        fprintf(stderr, "stream argument is not an open file\n");
        return;
    }
    if (!isstr(fmt)) {
        fprintf(stderr, "fmt argument is not a string\n");
        return;
    }
    if (!isnum(value)) {
        fprintf(stderr, "value argument is not a numeric value\n");
        return;
    }

    /* write value in decimal */
    fprintf(stream, fmt, value);
    if (ferror(stream)) {
        fprintf(stderr, "error in writing to file: %s error(%d): %s\n", filename, errno(stream), strerror(stream));
        return;
    }

    /* flush to be sure it is completely written */
    fflush(outfile);
    if (ferror(outfile)) {
        fprintf(stderr, "error in flushing to file: %s error(%d): %s\n", filename, errno(stream), strerror(stream));
    }
    return;
}


/*
 * wd2f - write a value to an open file in decimal
 */
define wd2f(value) {
    return w2f(outfilename, outfile, "%d\n", value);
}


/*
 * wx2f - write a value to an open file in hexadecimal
 */
define wx2f(value) {
    return w2f(outfilename, outfile, "%x\n", value);
}