~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 6ada1974eeefafcf50eed752374704d263533283 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/options.c -rw-r--r-- 3.8 KiB
6ada1974 — František Boháček feat: add new font with variable width 3 years ago
                                                                                
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
#include "options.h"
#include "file_access.h"
#include <bits/stdint-uintn.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

exec_options_loader_t exec_options_loader_create(char *filename) {
  exec_options_loader_t loader = {
      .options_filename = filename,
      .bytes_size = 0,
      .exec_options = NULL,
  };

  return loader;
}

file_operation_error_t exec_options_loader_get_size(exec_options_loader_t *loader) {
  FILE *file = fopen(loader->options_filename, "r");
  if (file == NULL) {
    return file_operation_error_from_errno(errno);
  }
  int read = fread(&loader->bytes_size, sizeof(loader->bytes_size), 1, file);
  if (read < 1 && ferror(file)) {
    fclose(file);
    return file_operation_error_from_errno(errno);
  }

  fclose(file);
  return FILOPER_SUCCESS;
}

file_operation_error_t exec_options_loader_load(exec_options_loader_t *loader,
                                         char *buffer) {
  FILE *file = fopen(loader->options_filename, "r");
  if (file == NULL) {
    return file_operation_error_from_errno(errno);
  }

  fread(buffer, sizeof(char), loader->bytes_size, file);

  if (ferror(file)) {
    fclose(file);
    return file_operation_error_from_errno(errno);
  }

  fclose(file);
  loader->exec_options = (exec_options_t*)buffer;

  uint64_t buffer_ptr = (uint64_t)buffer;
  loader->exec_options->options += buffer_ptr;

  for (int i = 0; i < loader->exec_options->options_count; i++) {
    exec_option_t option = loader->exec_options->options[i];

    option.mime += buffer_ptr;
    option.program += buffer_ptr;
    loader->exec_options->options[i] = option;
  }

  return FILOPER_SUCCESS;
}

file_operation_error_t exec_options_save(exec_options_t *options, char *filename) {
  uint32_t length = sizeof(uint32_t);
  for (int i = 0; i < options->options_count; i++) {
    exec_option_t option = options->options[i];
    option.mime_length = strlen(option.mime);
    option.program_length = strlen(option.program);
    option.length = (option.mime_length + option.program_length) * sizeof(char) +
                sizeof(uint16_t) * 2;
    length += option.length;

    options->options[i] = option;
  }
  options->bytes_size = length;

  FILE *file = fopen(filename, "w");
  if (file == NULL) {
    return file_operation_error_from_errno(errno);
  }

  fwrite(&options->bytes_size, sizeof(options->bytes_size), 1, file);
  fwrite(&options->options_count, sizeof(options->options_count), 1, file);

  uint64_t offset = 1;
  fwrite(&offset, sizeof(offset), 1, file);

  if (ferror(file)) {
    fclose(file);
    return file_operation_error_from_errno(errno);
  }

  uint64_t chars_offset = sizeof(exec_option_t) * options->options_count;
  file_operation_error_t error = FILOPER_SUCCESS;
  for (int i = 0; i < options->options_count; i++) {
    exec_option_t option = options->options[i];
    fwrite(&option, sizeof(option.mime_length) + sizeof(option.length) + sizeof(option.program_length), 1, file);

    fwrite(&chars_offset, sizeof(chars_offset), 1, file);
    chars_offset += option.program_length + 1;

    fwrite(&chars_offset, sizeof(chars_offset), 1, file);
    chars_offset += option.mime_length + 1;

    if (ferror(file)) {
      fclose(file);
      return file_operation_error_from_errno(errno);
    }
  }

  for (int i = 0; i < options->options_count; i++) {
    exec_option_t option = options->options[i];
    fwrite(option.program, option.program_length + 1, 1, file);
    fwrite(option.mime, option.mime_length + 1, 1, file);

    if (ferror(file)) {
      fclose(file);
      return file_operation_error_from_errno(errno);
    }
  }

  fclose(file);
  return error;
}

char *exec_options_get_program(exec_options_t *options, char *mime) {
  for (int i = 0; i < options->options_count; i++) {
    exec_option_t option = options->options[i];
    if (strcmp(option.mime, mime) == 0) {
      return option.program;
    }
  }

  return NULL;
}
Do not follow this link