-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_functions.c
More file actions
171 lines (152 loc) · 4.96 KB
/
Copy pathfile_functions.c
File metadata and controls
171 lines (152 loc) · 4.96 KB
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
161
162
163
164
165
166
167
168
169
170
171
/******************************************************************************
jnxutil - Garmin jnx file manipulation utility.
-------------------
begin : 0:04 october,15 2019
copyright : (C)2019 by Plasteev Vladislav
email : apr2504@yandex.ru
*******************************************************************************/
/******************************************************************************
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*******************************************************************************/
#include "common.h"
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
FILE *fp_jnxfile;
void make_dir(char *dir_name)
{
struct stat sb;
if (stat(dir_name, &sb) == 0 && S_ISDIR(sb.st_mode))
{
fprintf(stderr, "mkdir: dir %s already exists\n", dir_name);
exit(-1);
}
if ((mkdir(dir_name, 0777)) == -1)
{
fprintf(stderr, "mkdir: create dir %s %s\n", dir_name, strerror(errno));
exit(-1);
}
}
void change_dir(char *dirname)
{
if ((chdir(dirname)) != 0)
{
fprintf(stderr, "chdir: change dir %s %s\n", dirname, strerror(errno));
exit(-1);
}
}
FILE *open_file(char *filename, char *mode)
{
FILE *fptr;
if ((fptr = fopen(filename, mode)) == NULL)
{
fprintf(stderr, "fopen: open file %s %s\n", filename, strerror(errno));
exit(-1);
}
return fptr;
}
void open_jnx_file()
{
fp_jnxfile = open_file(jnx_file_name, "r");
}
void close_file(FILE **fptr)
{
if (*fptr != NULL)
{
fclose(*fptr);
*fptr = NULL;
}
}
void close_jnx_file()
{
close_file(&fp_jnxfile);
}
void write_block(void *buf, size_t size, size_t num, FILE *fptr)
{
if ((fwrite(buf, size, num, fptr)) != num)
fprintf(stderr, "fwrite: couldn't write into file %s\n", strerror(errno));
}
void write_one_block(void *buf, size_t size, FILE *fptr)
{
write_block(buf, size, 1, fptr);
}
void read_block(void *buf, size_t size, size_t num)
{
if ((fread(buf, size, num, fp_jnxfile)) != num)
{
fprintf(stderr, "fread: couldn't read file %s %s\n", jnx_file_name, strerror(errno));
close_jnx_file();
exit(-1);
}
}
void read_uint_block(char * value_name, uint layer)
{
elements[elements_counter].number = elements_counter;
elements[elements_counter].offset = ftell(fp_jnxfile);
elements[elements_counter].length = 4;
elements[elements_counter].type = UINT_VALUE;
elements[elements_counter].layer = layer;
strcpy(elements[elements_counter].name, value_name);
read_block(&elements[elements_counter].uint_value, 4, 1);
elements_counter++;
}
void read_int_block(char * value_name, uint layer)
{
elements[elements_counter].number = elements_counter;
elements[elements_counter].offset = ftell(fp_jnxfile);
elements[elements_counter].length = 4;
elements[elements_counter].type = INT_VALUE;
elements[elements_counter].layer = layer;
strcpy(elements[elements_counter].name, value_name);
read_block(&elements[elements_counter].int_value, 4, 1);
elements_counter++;
}
void read_short_block(char * value_name, uint layer)
{
elements[elements_counter].number = elements_counter;
elements[elements_counter].offset = ftell(fp_jnxfile);
elements[elements_counter].length = 2;
elements[elements_counter].type = SHORT_VALUE;
elements[elements_counter].layer = layer;
strcpy(elements[elements_counter].name, value_name);
read_block(&elements[elements_counter].short_value, 2, 1);
elements_counter++;
}
void read_str(char *value_name, uint layer)
{
uint j = 0;
char ch;
elements[elements_counter].number = elements_counter;
elements[elements_counter].offset = ftell(fp_jnxfile);
elements[elements_counter].type = CHAR_VALUE;
elements[elements_counter].layer = layer;
strcpy(elements[elements_counter].name, value_name);
do
{
read_block(&ch, sizeof(char), 1);
elements[elements_counter].char_value[j++] = ch;
} while (ch != 0);
elements[elements_counter].length = (j - 1);
elements_counter++;
}
void seek_block(long offset, int whence)
{
if ((fseek(fp_jnxfile, offset, whence)) != 0)
{
fprintf(stderr, "fseek: error set position in file %s %s\n", jnx_file_name, strerror(errno));
close_jnx_file();
exit(-2);
}
}