jag

snake_c

public

Small snake game in C

Star0
Watch0
f47b31cfbecf3f569566b9977e522083fc9bc485
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
source/settings_file/settings_file.cC88 lines1.8 KB
1
#include "settings_file.h"
2
3
uint8_t read_settings(char *settings_path, setting_arr set_arr) {
4
5
  FILE* setting_file = fopen(settings_path, "r");
6
7
  if (setting_file == NULL) {
8
    if (errno == ENOENT) 
9
      return 1;
10
11
    free(set_arr.settings);
12
    return 0;
13
  }
14
    
15
  char setting_input[SET_LEN]; 
16
17
  size_t i;
18
  for (i = 0; i < set_arr.length; i++) {
19
20
    fgets(setting_input, sizeof(setting_input), setting_file);
21
22
    char *endptr;
23
    int64_t new_num = strtoll(setting_input, &endptr, 10);
24
    
25
    if (setting_input != endptr)
26
      set_arr.settings[i] = new_num;
27
28
  }
29
30
  fclose(setting_file);
31
32
  return 1;
33
}
34
35
36
uint8_t init_setting_arr(setting_arr *set_arr, size_t length) {
37
  set_arr->length = length;
38
  
39
  if (!(set_arr->settings = calloc(length, sizeof(int64_t))))
40
    return 0;
41
42
  return 1;
43
}
44
45
void free_setting_arr(setting_arr set_arr) {
46
  free(set_arr.settings);
47
}
48
49
50
51
52
uint8_t get_highscore(char *highscore_path, highscore *leader_board, size_t score_ammount) {
53
54
  FILE* highscore_file = fopen(highscore_path, "r");
55
56
  if(highscore_file == NULL) 
57
    return (errno == ENOENT) ? 1 : 0;
58
59
  char score_name[SCORE_LEN];
60
  char score_value[SCORE_LEN];
61
62
  uint8_t i;
63
  for (i = 0; i < score_ammount; i++) {
64
65
    fgets(score_name, sizeof(score_name), highscore_file);
66
    
67
    fgets(score_value, sizeof(score_value), highscore_file);
68
    leader_board[i].points = strtoll(score_value, NULL, 10);;
69
70
    if (score_name[0] == '\0')
71
      continue;
72
 
73
    uint8_t j;
74
    for(j = 0; score_name[j] != '\0'; j++) {
75
      if (score_name[j] == '\n' || score_name[j] == ' ' || score_name[j] == '\t') {
76
        score_name[j] = '\0';
77
        break;
78
      }
79
    }
80
  
81
    score_name[SCORE_LEN - 1] = '\0';
82
    strncpy(leader_board[i].name, score_name, SCORE_LEN);  
83
  }
84
85
  fclose(highscore_file);
86
87
  return 1;
88
}