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

ref: b62ac26978e78532bcfdb97d8f2ac397d40a76b4 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/options.c -rw-r--r-- 4.6 KiB
b62ac269 — František Boháček fix: loading and saving options correctly 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "options.h"
#include "file_access.h"
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

typedef struct {
  uint16_t length;
  uint16_t mime_length;
  uint16_t program_length;
  uint64_t mime;
  uint64_t program;
} __attribute__((__packed__)) load_exec_option_t;

typedef struct {
  uint32_t bytes_size;
  uint16_t options_count;
  uint64_t options;
} __attribute__((__packed__)) load_exec_options_t;

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;

  load_exec_options_t *load_options = (load_exec_options_t*)loader->exec_options;

  loader->exec_options->options = (exec_option_t*)(buffer + load_options->options);

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

    char *mime = buffer + load_option->mime;
    char *program = buffer + load_option->program;

    option.mime = mime;
    option.program = program;

    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(exec_options_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(exec_option_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 = sizeof(options->bytes_size) + sizeof(options->options_count) + sizeof(offset);
  fwrite(&offset, sizeof(offset), 1, file);

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

  uint64_t chars_offset = offset + (uint64_t) (sizeof(load_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.mime_length + 1;

    fwrite(&chars_offset, sizeof(chars_offset), 1, file);
    chars_offset += option.program_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.mime, option.mime_length + 1, 1, file);
    fwrite(option.program, option.program_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;
}

exec_options_t *browser_exec_options;
Do not follow this link