winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
io_default_writer.c
1#include "io.h"
2
3typedef struct {
4 HANDLE out_fp;
5 bool writing_to_stdout;
6} default_writer_user_args_t;
7
8static bool default_writer_setup(void *instance, bool writing_to_stdout, wchar_t *full_output_dir) {
9 writer_t *writer = (writer_t *)instance;
10 default_writer_user_args_t *args = malloc(sizeof(default_writer_user_args_t));
11 if (!args) { // LCOV_EXCL_START
12 return false;
13 } // LCOV_EXCL_STOP
14 memset(args, 0, sizeof(default_writer_user_args_t));
15 args->writing_to_stdout = writing_to_stdout;
16 args->out_fp = !writing_to_stdout ? CreateFile(full_output_dir,
17 GENERIC_READ | GENERIC_WRITE,
18 0,
19 nullptr,
20 CREATE_ALWAYS,
21 FILE_ATTRIBUTE_NORMAL,
22 nullptr) :
23 GetStdHandle(STD_OUTPUT_HANDLE);
24 if (args->out_fp == INVALID_HANDLE_VALUE) {
25 goto fail;
26 }
27 writer->user_args = args;
28 return true;
29fail:
30 free(args);
31 return false;
32}
33
34static bool default_writer_write_output(void *instance,
35 const char *mb_out,
36 size_t total_size,
37 long unsigned int *written) {
38 writer_t *writer = (writer_t *)instance;
39 default_writer_user_args_t *args = (default_writer_user_args_t *)writer->user_args;
40 return WriteFile(args->out_fp, mb_out, (DWORD)total_size, (LPDWORD)written, nullptr);
41}
42
43static void default_writer_teardown(void *instance) {
44 writer_t *writer = (writer_t *)instance;
45 default_writer_user_args_t *args = (default_writer_user_args_t *)writer->user_args;
46 if (!args->writing_to_stdout) {
47 CloseHandle(args->out_fp);
48 }
49 free(writer->user_args);
50}
51
52writer_t *get_default_writer() {
53 writer_t *out = malloc(sizeof(writer_t));
54 if (!out) { // LCOV_EXCL_START
55 return nullptr;
56 } // LCOV_EXCL_STOP
57 out->setup = default_writer_setup;
58 out->write_output = default_writer_write_output;
59 out->teardown = default_writer_teardown;
60 return out;
61}