blob: 6f40168c1ee086cfb36067278e8d7cde7759638a [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include "OlyUtility.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <ctype.h>
15
16#if defined(WIN32)
17#include <windows.h>
18#elif defined(__linux__)
19#include <unistd.h>
20#elif defined(DARWIN)
21#include <mach-o/dyld.h>
22#endif
23
24OlyUtility* util = NULL;
25
26bool OlyUtility::stringToBool(const char* string, bool defValue) {
27 char value[32];
28
29 if (string == NULL) {
30 return defValue;
31 }
32
33 strncpy(value, string, sizeof(value));
34 if (value[0] == 0) {
35 return defValue;
36 }
37 value[sizeof(value) - 1] = 0; // strncpy does not guarantee a null-terminated string
38
39 // Convert to lowercase
40 int i = 0;
41 while (value[i]) {
42 value[i] = tolower(value[i]);
43 i++;
44 }
45
46 if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "1") == 0 || strcmp(value, "on") == 0) {
47 return true;
48 } else if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 || strcmp(value, "0") == 0 || strcmp(value, "off") == 0) {
49 return false;
50 } else {
51 return defValue;
52 }
53}
54
55void OlyUtility::stringToLower(char* string) {
56 if (string == NULL) {
57 return;
58 }
59
60 while (*string) {
61 *string = tolower(*string);
62 string++;
63 }
64}
65
66// Modifies fullpath with the path part including the trailing path separator
67int OlyUtility::getApplicationFullPath(char* fullpath, int sizeOfPath) {
68 memset(fullpath, 0, sizeOfPath);
69#if defined(WIN32)
70 int length = GetModuleFileName(NULL, fullpath, sizeOfPath);
71#elif defined(__linux__)
72 int length = readlink("/proc/self/exe", fullpath, sizeOfPath);
73#elif defined(DARWIN)
74 uint32_t length_u = (uint32_t)sizeOfPath;
75 int length = sizeOfPath;
76 if (_NSGetExecutablePath(fullpath, &length_u) == 0) {
77 length = strlen(fullpath);
78 }
79#endif
80
81 if (length == sizeOfPath) {
82 return -1;
83 }
84
85 fullpath[length] = 0;
Jon Medhurstd3698592013-10-10 16:48:56 +010086 getPathPart(fullpath);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010087
88 return 0;
89}
90
91char* OlyUtility::readFromDisk(const char* file, unsigned int *size, bool appendNull) {
92 // Open the file
93 FILE* pFile = fopen(file, "rb");
94 if (pFile==NULL) {
95 return NULL;
96 }
97
98 // Obtain file size
99 fseek(pFile , 0 , SEEK_END);
100 unsigned int lSize = ftell(pFile);
101 rewind(pFile);
102
103 // Allocate memory to contain the whole file
104 char* buffer = (char*)malloc(lSize + (int)appendNull);
105 if (buffer == NULL) {
106 fclose(pFile);
107 return NULL;
108 }
109
110 // Copy the file into the buffer
111 if (fread(buffer, 1, lSize, pFile) != lSize) {
112 free(buffer);
113 fclose(pFile);
114 return NULL;
115 }
116
117 // Terminate
118 fclose(pFile);
119
120 if (appendNull) {
121 buffer[lSize] = 0;
122 }
123
124 if (size) {
125 *size = lSize;
126 }
127
128 return buffer;
129}
130
131int OlyUtility::writeToDisk(const char* path, const char* data) {
132 // Open the file
133 FILE* pFile = fopen(path, "wb");
134 if (pFile == NULL) {
135 return -1;
136 }
137
138 // Write the data to disk
139 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
140 fclose(pFile);
141 return -1;
142 }
143
144 // Terminate
145 fclose(pFile);
146 return 0;
147}
148
149int OlyUtility::appendToDisk(const char* path, const char* data) {
150 // Open the file
151 FILE* pFile = fopen(path, "a");
152 if (pFile == NULL) {
153 return -1;
154 }
155
156 // Write the data to disk
157 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
158 fclose(pFile);
159 return -1;
160 }
161
162 // Terminate
163 fclose(pFile);
164 return 0;
165}
166
167/**
168 * Copies the srcFile into dstFile in 1kB chunks.
169 * The dstFile will be overwritten if it exists.
170 * 0 is returned on an error; otherwise 1.
171 */
172#define TRANSFER_SIZE 1024
173int OlyUtility::copyFile(const char* srcFile, const char* dstFile) {
Jon Medhurstd3698592013-10-10 16:48:56 +0100174 char buffer[TRANSFER_SIZE];
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100175 FILE * f_src = fopen(srcFile,"rb");
176 if (!f_src) {
177 return 0;
178 }
179 FILE * f_dst = fopen(dstFile,"wb");
180 if (!f_dst) {
181 fclose(f_src);
182 return 0;
183 }
184 while (!feof(f_src)) {
185 int num_bytes_read = fread(buffer, 1, TRANSFER_SIZE, f_src);
186 if (num_bytes_read < TRANSFER_SIZE && !feof(f_src)) {
187 fclose(f_src);
188 fclose(f_dst);
189 return 0;
190 }
191 int num_bytes_written = fwrite(buffer, 1, num_bytes_read, f_dst);
192 if (num_bytes_written != num_bytes_read) {
193 fclose(f_src);
194 fclose(f_dst);
195 return 0;
196 }
197 }
198 fclose(f_src);
199 fclose(f_dst);
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100200 return 1;
201}
202
203const char* OlyUtility::getFilePart(const char* path) {
204 const char* last_sep = strrchr(path, PATH_SEPARATOR);
205
206 // in case path is not a full path
207 if (last_sep == NULL) {
208 return path;
209 }
210
211 return last_sep++;
212}
213
214// getPathPart may modify the contents of path
215// returns the path including the trailing path separator
216char* OlyUtility::getPathPart(char* path) {
217 char* last_sep = strrchr(path, PATH_SEPARATOR);
218
219 // in case path is not a full path
220 if (last_sep == NULL) {
221 return 0;
222 }
223 last_sep++;
224 *last_sep = 0;
225
226 return (path);
227}