[262685]: Utils.c Maximize Restore History

Download this file

Utils.c    176 lines (149 with data), 3.2 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
172
173
174
#include "AutoPanoSift.h"
/* these are generic support routines */
#ifdef _MSC_VER
#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>
#include <direct.h>
#define PATH_MAX 1024
#define getcwd _getcwd
#else
#include <unistd.h>
#endif
void Write(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fflush(stdout);
}
void WriteLine(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void WriteError(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
void FatalError(char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
WriteError(fmt, ap);
exit(-1);
}
char* FileNameToFullPath(char* filename)
{
char buf[PATH_MAX];
char* cwd = getcwd(buf, sizeof(buf));
char* path = (char*)malloc(strlen(cwd)+1+strlen(filename)+1);
sprintf(path, "%s/%s", cwd, filename);
return path;
}
char* FullPathToFileName(char* path)
{
char* filename = strrchr(path, '/');
if (filename == NULL) filename = path;
return strdup(filename);
}
void*** PtrMap_new(int yDim, int xDim)
{
int* mem = (int*) malloc(sizeof(int)*2 + yDim * sizeof(void**));
void*** self = (void***)(mem+2);
int i;
mem[0] = xDim;
mem[1] = yDim;
for(i=0; i<yDim; i++) {
self[i] = (void**)calloc(xDim , sizeof(void*));
}
return self;
}
void PtrMap_delete(void*** self, void (*deletefn)(void*) ) {
if (self) {
int* mem = ((int*)self)-2;
if (*mem) {
int xDim = mem[0];
int yDim = mem[1];
int i;
for(i=0; i<yDim; i++) {
if (deletefn != NULL) {
void (*_deletefn)(void*) = deletefn;
int j;
for(j=0; i< xDim; j++) {
if (self[i][j] != NULL) {
_deletefn(self[i][j]);
self[i][j] = NULL;
}
}
}
free(self[i]);
self[i]=NULL;
}
*mem = 0;
free(mem);
}
}
}
int** IntMap_new(int yDim, int xDim) {
int* mem = (int*) malloc(sizeof(int)*2 + yDim * sizeof(int*));
int** self = (int**)(mem+2);
int i;
mem[0] = xDim;
mem[1] = yDim;
for(i=0; i<yDim; i++) {
self[i] = (int*)calloc(xDim , sizeof(int));
}
return self;
}
void IntMap_delete(int** self) {
if (self) {
int* mem = ((int*)self)-2;
if (*mem) {
int xDim = mem[0];
int yDim = mem[1];
int i;
for(i=0; i<yDim; i++) {
free(self[i]);
self[i]=NULL;
}
*mem = 0;
free(mem);
}
}
}
double** DoubleMap_new(int yDim, int xDim) {
int* mem = (int*)malloc(sizeof(int)*2 + yDim * sizeof(double*));
double** self = (double**)(mem+2);
int i;
mem[0] = xDim;
mem[1] = yDim;
for(i=0; i<yDim; i++) {
self[i] = (double*)malloc(xDim * sizeof(double));
memset(self[i], 0, xDim*sizeof(double));
if (self[i] == NULL) { FatalError("out of memory"); }
}
return self;
}
void DoubleMap_delete(double** self) {
if (self) {
int* mem = ((int*)self)-2;
if (*mem) {
int xDim = mem[0];
int yDim = mem[1];
int i;
for(i=0; i<yDim; i++) {
if (self[i] != NULL) {
free(self[i]);
self[i]=NULL;
}
}
*mem = 0;
free(mem);
}
}
}