-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
122 lines (112 loc) · 2.02 KB
/
utils.c
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
#include "utils.h"
char *convert_path(char *path)
{
char *res = malloc(strlen(path) + strlen(ROOT_DIR) - 4);
strcpy(res, ROOT_DIR);
strcat(res, path + 5);
return res;
}
int address_error(char *path)
{
char *ptr = strrchr(path, '/');
*ptr = 0;
if (access(path, F_OK) != 0)
{
print_msg("Error: Address not found");
*ptr = '/';
return 1;
}
*ptr = '/';
return 0;
}
int file_not_found_error(char *path)
{
if (access(path, F_OK) != 0)
{
print_msg("Error: File doesn't exist");
return 1;
}
return 0;
}
size_t get_file_size(FILE *fp)
{
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
rewind(fp);
return size;
}
int count_file_lines(FILE *fp)
{
long cursor = ftell(fp);
rewind(fp);
int ans = 0;
int ch = fgetc(fp);
int prev = 0;
while (ch != EOF)
{
if (ch == '\n') ans++;
prev = ch;
ch = fgetc(fp);
}
if (prev != '\n') ans++;
fseek(fp, cursor, SEEK_SET);
return ans;
}
char *get_backup_path(char *path)
{
char *backup_path = malloc(strlen(path) + 9);
char *slash = strrchr(path, '/');
*slash = 0;
strcpy(backup_path, path);
strcat(backup_path, "/.");
strcat(backup_path, slash + 1);
strcat(backup_path, ".backup");
*slash = '/';
return backup_path;
}
void copy_file(char *src, char *dst)
{
FILE *fr = fopen(src, "r");
FILE *fw = fopen(dst, "w");
int ch = fgetc(fr);
while (ch != EOF)
{
fputc(ch, fw);
ch = fgetc(fr);
}
fclose(fr);
fclose(fw);
}
int count_digits(int num)
{
int cnt = 0;
while (num)
{
num /= 10;
cnt++;
}
return cnt;
}
size_t pos2idx(String *buf, size_t line_num, size_t idx)
{
size_t i;
for (i = 0; line_num > 1 && i < buf->len; i++)
{
if (buf->arr[i] == '\n') line_num--;
}
if (line_num != 1) return -1;
if (i + idx > buf->len) return -1;
for (int j = i; j < i + idx; j++)
if (buf->arr[j] == '\n')
return -1;
return i + idx;
}
size_t posstr2idx(String *buf, char *posstr)
{
char *ptr = strchr(posstr, ':');
*ptr = 0;
size_t line_num = strtoull(posstr, NULL, 10);
size_t idx = strtoull(ptr + 1, NULL, 10);
*ptr = ':';
return pos2idx(buf, line_num, idx);
}