This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.c
73 lines (61 loc) · 1.7 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
// Sun Jul 21 09:34:39 DST 2019
// Copyright Michiel van Wessem
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "platform.h"
#include "utils.h"
#include <stdarg.h>
#include <stdio.h>
#include "mystring.h"
char* filename_from_filename(const char* old_file_name, char* old_extension, char* new_extension)
{
char* extension = strrchr(old_file_name, '.');
if (extension == NULL)
return NULL;
if (0 != strcasecmp(++extension, old_extension))
return NULL;
const size_t base_length = strlen(old_file_name) - strlen(extension) - 1;
const size_t new_filename_length = base_length + 1 + strlen(new_extension);
char* new_file_name = malloc(new_filename_length + 1);
if (new_file_name != NULL) {
strncpy(new_file_name, old_file_name, base_length + 1);
strcpy(new_file_name + base_length + 1, new_extension);
}
return new_file_name;
}
int file_exists(const char *filename)
{
#if defined(_WINDOWS)
struct __stat64 buffer;
const int s = _stat64(filename, &buffer);
#else
struct stat buffer;
const int s = stat(filename, &buffer);
#endif
return (s == 0);
}
const char *file_name(const char *path)
{
if (path == NULL)
return NULL;
const char* p = strrpbrk(path, path_separators);
return p == NULL ? path : ++p;
}
void exit_with_error(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
fprintf(stderr, "\n");
exit(-1);
}
void exit_not_implemented()
{
exit_with_error("Work in progress. This functionality has not been implemented yet!\n");
}
void update_average(const double d, double* average, int count)
{
*average = (*average * (double) count + d) / ((double) count + 1);
}