File: range_solvers_binary_output.h

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (160 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (4)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2000 Shlomi Fish
 */
/*
 * range_solvers_binary_output.h - header file for range solvers binary
 * output.
 */
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "rinutils.h"
#include "freecell-solver/fcs_cl.h"

typedef struct
{
    FILE *fh;
    char *buffer;
    char *buffer_end;
    char *ptr;
    const char *filename;
} fcs_binary_output;

static const fcs_binary_output INIT_BINARY_OUTPUT = {.filename = NULL};

#define BINARY_OUTPUT_BUF_SIZE (sizeof(int) * 16)
#define SIZE_INT 4

static inline void write_me(fcs_binary_output *const bin)
{
    fwrite(bin->buffer, 1, (size_t)(bin->ptr - bin->buffer), bin->fh);
    fflush(bin->fh);
}

static void print_int(fcs_binary_output *const bin, int val)
{
    if (!bin->fh)
    {
        return;
    }
    unsigned char *const buffer = (unsigned char *const)bin->ptr;
    for (int p = 0; p < SIZE_INT; p++)
    {
        buffer[p] = (unsigned char)(val & 0xFF);
        val >>= 8;
    }
    bin->ptr += SIZE_INT;
    if (bin->ptr == bin->buffer_end)
    {
        write_me(bin);
        /* Reset ptr to the beginning */
        bin->ptr = bin->buffer;
    }
}

static inline void bin_close(fcs_binary_output *const bin)
{
    if (bin->filename)
    {
        write_me(bin);
        fclose(bin->fh);
        bin->fh = NULL;
        bin->filename = NULL;
    }
}

static inline bool read_int(FILE *const f, long long *const dest)
{
    unsigned char buffer[SIZE_INT];
    if (fread(buffer, 1, SIZE_INT, f) != SIZE_INT)
    {
        return TRUE;
    }
    *dest = (buffer[0] +
             ((buffer[1] + ((buffer[2] + ((buffer[3]) << 8)) << 8)) << 8));

    return FALSE;
}

static void read_int_wrapper(FILE *const in, long long *const var)
{
    if (read_int(in, var))
    {
        fc_solve_err(
            "%s", "Output file is too short to deduce the configuration!\n");
    }
}

static inline void bin_init(fcs_binary_output *const bin,
    long long *const start_board_ptr, long long *const end_board_ptr,
    fcs_int_limit_t *const total_iterations_limit_per_board_ptr)
{
    if (bin->filename)
    {
        bin->buffer = malloc(BINARY_OUTPUT_BUF_SIZE);
        bin->ptr = bin->buffer;
        bin->buffer_end = bin->buffer + BINARY_OUTPUT_BUF_SIZE;

        FILE *const in = fopen(bin->filename, "rb");
        if (!in)
        {
            if (!(bin->fh = fopen(bin->filename, "wb")))
            {
                fc_solve_err(
                    "Could not open \"%s\" for writing!\n", bin->filename);
            }

            print_int(bin, *start_board_ptr);
            print_int(bin, *end_board_ptr);
            print_int(bin, (int)(*total_iterations_limit_per_board_ptr));
        }
        else
        {
            read_int_wrapper(in, start_board_ptr);
            read_int_wrapper(in, end_board_ptr);
            {
                long long val;
                read_int_wrapper(in, &val);
                *total_iterations_limit_per_board_ptr = (fcs_int_limit_t)val;
            }

            fseek(in, 0, SEEK_END);
            const long file_len = ftell(in);
            if (file_len % 4 != 0)
            {
                fc_solve_err(
                    "%s", "Output file has an invalid length. Terminating.\n");
            }
            *start_board_ptr += (file_len - 12) / 4;
            if (*start_board_ptr >= *end_board_ptr)
            {
                fc_solve_err("%s",
                    "Output file was already finished being generated.\n");
            }
            fclose(in);
            if (!(bin->fh = fopen(bin->filename, "ab")))
            {
                fc_solve_err(
                    "Could not open \"%s\" for writing!\n", bin->filename);
            }
        }
    }
    else
    {
        bin->fh = NULL;
        bin->buffer = bin->ptr = bin->buffer_end = NULL;
    }
}

#ifdef __cplusplus
}
#endif