1 /**
2 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
3 *
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 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
14 #ifndef WIN32
15 #include <unistd.h>
16 #else
17 #include <Windows.h>
18 #endif
20 #include "OlyUtility.h"
22 OlyUtility* util = NULL;
24 bool OlyUtility::stringToBool(const char* string, bool defValue) {
25 char value[32];
27 strncpy(value, string, sizeof(value));
28 if (value[0] == 0) {
29 return defValue;
30 }
31 value[sizeof(value) - 1] = 0; // strncpy does not guarantee a null-terminated string
33 // Convert to lowercase
34 int i = 0;
35 while (value[i]) {
36 value[i] = tolower(value[i]);
37 i++;
38 }
40 if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "1") == 0 || strcmp(value, "on") == 0) {
41 return true;
42 } else if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 || strcmp(value, "0") == 0 || strcmp(value, "off") == 0) {
43 return false;
44 } else {
45 return defValue;
46 }
47 }
49 void OlyUtility::stringToLower(char* string) {
50 if (string == NULL) {
51 return;
52 }
54 while (*string) {
55 *string = tolower(*string);
56 string++;
57 }
58 }
60 // Modifies fullpath with the path part including the trailing path separator
61 int OlyUtility::getApplicationFullPath(char* fullpath, int sizeOfPath) {
62 memset(fullpath, 0, sizeOfPath);
63 #ifdef WIN32
64 int length = GetModuleFileName(NULL, fullpath, sizeOfPath);
65 #else
66 int length = readlink("/proc/self/exe", fullpath, sizeOfPath);
67 #endif
69 if (length == sizeOfPath) {
70 return -1;
71 }
73 fullpath[length] = 0;
74 fullpath = getPathPart(fullpath);
76 return 0;
77 }
79 char* OlyUtility::readFromDisk(const char* file, unsigned int *size, bool appendNull) {
80 // Open the file
81 FILE* pFile = fopen(file, "rb");
82 if (pFile==NULL) {
83 return NULL;
84 }
86 // Obtain file size
87 fseek(pFile , 0 , SEEK_END);
88 unsigned int lSize = ftell(pFile);
89 rewind(pFile);
91 // Allocate memory to contain the whole file
92 char* buffer = (char*)malloc(lSize + (int)appendNull);
93 if (buffer == NULL) {
94 fclose(pFile);
95 return NULL;
96 }
98 // Copy the file into the buffer
99 if (fread(buffer, 1, lSize, pFile) != lSize) {
100 free(buffer);
101 fclose(pFile);
102 return NULL;
103 }
105 // Terminate
106 fclose(pFile);
108 if (appendNull) {
109 buffer[lSize] = 0;
110 }
112 if (size) {
113 *size = lSize;
114 }
116 return buffer;
117 }
119 int OlyUtility::writeToDisk(const char* path, const char* data) {
120 // Open the file
121 FILE* pFile = fopen(path, "wb");
122 if (pFile == NULL) {
123 return -1;
124 }
126 // Write the data to disk
127 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
128 fclose(pFile);
129 return -1;
130 }
132 // Terminate
133 fclose(pFile);
134 return 0;
135 }
137 int OlyUtility::appendToDisk(const char* path, const char* data) {
138 // Open the file
139 FILE* pFile = fopen(path, "a");
140 if (pFile == NULL) {
141 return -1;
142 }
144 // Write the data to disk
145 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
146 fclose(pFile);
147 return -1;
148 }
150 // Terminate
151 fclose(pFile);
152 return 0;
153 }
155 /**
156 * Copies the srcFile into dstFile in 1kB chunks.
157 * The dstFile will be overwritten if it exists.
158 * 0 is returned on an error; otherwise 1.
159 */
160 #define TRANSFER_SIZE 1024
161 int OlyUtility::copyFile(const char* srcFile, const char* dstFile) {
162 char* buffer = (char*)malloc(TRANSFER_SIZE);
163 FILE * f_src = fopen(srcFile,"rb");
164 if (!f_src) {
165 return 0;
166 }
167 FILE * f_dst = fopen(dstFile,"wb");
168 if (!f_dst) {
169 fclose(f_src);
170 return 0;
171 }
172 while (!feof(f_src)) {
173 int num_bytes_read = fread(buffer, 1, TRANSFER_SIZE, f_src);
174 if (num_bytes_read < TRANSFER_SIZE && !feof(f_src)) {
175 fclose(f_src);
176 fclose(f_dst);
177 return 0;
178 }
179 int num_bytes_written = fwrite(buffer, 1, num_bytes_read, f_dst);
180 if (num_bytes_written != num_bytes_read) {
181 fclose(f_src);
182 fclose(f_dst);
183 return 0;
184 }
185 }
186 fclose(f_src);
187 fclose(f_dst);
188 free(buffer);
189 return 1;
190 }
192 const char* OlyUtility::getFilePart(const char* path) {
193 const char* last_sep = strrchr(path, PATH_SEPARATOR);
195 // in case path is not a full path
196 if (last_sep == NULL) {
197 return path;
198 }
200 return (const char*)((int)last_sep + 1);
201 }
203 // getPathPart may modify the contents of path
204 // returns the path including the trailing path separator
205 char* OlyUtility::getPathPart(char* path) {
206 char* last_sep = strrchr(path, PATH_SEPARATOR);
208 // in case path is not a full path
209 if (last_sep == NULL) {
210 return 0;
211 }
212 *(char*)((int)last_sep + 1) = 0;
214 return (path);
215 }
217 // whitespace callback utility function used with mini-xml
218 const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
219 const char *name;
221 name = mxmlGetElement(node);
223 if (loc == MXML_WS_BEFORE_OPEN) {
224 // Single indentation
225 if (!strcmp(name, "target") || !strcmp(name, "counters"))
226 return("\n ");
228 // Double indentation
229 if (!strcmp(name, "counter"))
230 return("\n ");
232 // Avoid a carriage return on the first line of the xml file
233 if (!strncmp(name, "?xml", 4))
234 return(NULL);
236 // Default - no indentation
237 return("\n");
238 }
240 if (loc == MXML_WS_BEFORE_CLOSE) {
241 // No indentation
242 if (!strcmp(name, "captured"))
243 return("\n");
245 // Single indentation
246 if (!strcmp(name, "counters"))
247 return("\n ");
249 // Default - no carriage return
250 return(NULL);
251 }
253 return(NULL);
254 }