summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/OlyUtility.cpp
blob: d508b97c83dea13ea46a8d6ff9791c3066a25a47 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * Copyright (C) ARM Limited 2010-2016. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "OlyUtility.h"

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(WIN32)
#include <windows.h>
#elif defined(__linux__)
#include <unistd.h>
#elif defined(DARWIN)
#include <mach-o/dyld.h>
#endif

bool stringToBool(const char* string, bool defValue) {
  char value[32];

  if (string == NULL) {
    return defValue;
  }

  strncpy(value, string, sizeof(value));
  if (value[0] == 0) {
    return defValue;
  }
  value[sizeof(value) - 1] = 0; // strncpy does not guarantee a null-terminated string

  // Convert to lowercase
  int i = 0;
  while (value[i]) {
    value[i] = tolower(value[i]);
    i++;
  }

  if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "1") == 0 || strcmp(value, "on") == 0) {
    return true;
  } else if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 || strcmp(value, "0") == 0 || strcmp(value, "off") == 0) {
    return false;
  } else {
    return defValue;
  }
}

void stringToLower(char* string) {
  if (string == NULL) {
    return;
  }

  while (*string) {
    *string = tolower(*string);
    string++;
  }
}

bool stringToLongLong(long long *const value, const char *str, const int base) {
  char *endptr;
  long long v;
  errno = 0;
  v = strtoll(str, &endptr, base);
  if (errno != 0 || *endptr != '\0') {
    return false;
  }
  *value = v;

  return true;
}

bool stringToLong(long *const value, const char *str, const int base) {
  char *endptr;
  long v;
  errno = 0;
  v = strtol(str, &endptr, base);
  if (errno != 0 || *endptr != '\0') {
    return false;
  }
  *value = v;

  return true;
}

bool stringToInt(int *const value, const char *str, const int base) {
  long v;
  if (!stringToLong(&v, str, base)) {
    return false;
  }
  *value = v;

  return true;
}

// Modifies fullpath with the path part including the trailing path separator
int getApplicationFullPath(char* fullpath, int sizeOfPath) {
  memset(fullpath, 0, sizeOfPath);
#if defined(WIN32)
  int length = GetModuleFileName(NULL, fullpath, sizeOfPath);
#elif defined(__linux__)
  int length = readlink("/proc/self/exe", fullpath, sizeOfPath);
#elif defined(DARWIN)
  uint32_t length_u = (uint32_t)sizeOfPath;
  int length = sizeOfPath;
  if (_NSGetExecutablePath(fullpath, &length_u) == 0) {
    length = strlen(fullpath);
  }
#endif

  if (length == sizeOfPath) {
    return -1;
  }

  fullpath[length] = 0;
  getPathPart(fullpath);

  return 0;
}

char* readFromDisk(const char* file, unsigned int *size, bool appendNull) {
  // Open the file
  FILE* pFile = fopen(file, "rb");
  if (pFile==NULL) {
    return NULL;
  }

  // Obtain file size
  fseek(pFile , 0 , SEEK_END);
  unsigned int lSize = ftell(pFile);
  rewind(pFile);

  // Allocate memory to contain the whole file
  char* buffer = (char*)malloc(lSize + (int)appendNull);
  if (buffer == NULL) {
    fclose(pFile);
    return NULL;
  }

  // Copy the file into the buffer
  if (fread(buffer, 1, lSize, pFile) != lSize) {
    free(buffer);
    fclose(pFile);
    return NULL;
  }

  // Terminate
  fclose(pFile);

  if (appendNull) {
    buffer[lSize] = 0;
  }

  if (size) {
    *size = lSize;
  }

  return buffer;
}

int writeToDisk(const char* path, const char* data) {
  // Open the file
  FILE* pFile = fopen(path, "wb");
  if (pFile == NULL) {
    return -1;
  }

  // Write the data to disk
  if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
    fclose(pFile);
    return -1;
  }

  // Terminate
  fclose(pFile);
  return 0;
}

int appendToDisk(const char* path, const char* data) {
  // Open the file
  FILE* pFile = fopen(path, "a");
  if (pFile == NULL) {
    return -1;
  }

  // Write the data to disk
  if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
    fclose(pFile);
    return -1;
  }

  // Terminate
  fclose(pFile);
  return 0;
}

/**
 * Copies the srcFile into dstFile in 1kB chunks.
 * The dstFile will be overwritten if it exists.
 * 0 is returned on an error; otherwise 1.
 */
#define TRANSFER_SIZE 1024
int copyFile(const char* srcFile, const char* dstFile) {
  char buffer[TRANSFER_SIZE];
  FILE * f_src = fopen(srcFile,"rb");
  if (!f_src) {
    return 0;
  }
  FILE * f_dst = fopen(dstFile,"wb");
  if (!f_dst) {
    fclose(f_src);
    return 0;
  }
  while (!feof(f_src)) {
    int num_bytes_read = fread(buffer, 1, TRANSFER_SIZE, f_src);
    if (num_bytes_read < TRANSFER_SIZE && !feof(f_src)) {
      fclose(f_src);
      fclose(f_dst);
      return 0;
    }
    int num_bytes_written = fwrite(buffer, 1, num_bytes_read, f_dst);
    if (num_bytes_written != num_bytes_read) {
      fclose(f_src);
      fclose(f_dst);
      return 0;
    }
  }
  fclose(f_src);
  fclose(f_dst);
  return 1;
}

const char* getFilePart(const char* path) {
  const char* last_sep = strrchr(path, PATH_SEPARATOR);

  // in case path is not a full path
  if (last_sep == NULL) {
    return path;
  }

  return last_sep++;
}

// getPathPart may modify the contents of path
// returns the path including the trailing path separator
char* getPathPart(char* path) {
  char* last_sep = strrchr(path, PATH_SEPARATOR);

  // in case path is not a full path
  if (last_sep == NULL) {
    return 0;
  }
  last_sep++;
  *last_sep = 0;

  return (path);
}