]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - daemon/OlyUtility.cpp
Merge branch 'master' into android
[android-sdk/arm-ds5-gator.git] / daemon / OlyUtility.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2014. 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 "OlyUtility.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
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
24 OlyUtility* util = NULL;
26 bool OlyUtility::stringToBool(const char* string, bool defValue) {
27   char value[32];
29   if (string == NULL) {
30     return defValue;
31   }
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
39   // Convert to lowercase
40   int i = 0;
41   while (value[i]) {
42     value[i] = tolower(value[i]);
43     i++;
44   }
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 }
55 void OlyUtility::stringToLower(char* string) {
56   if (string == NULL) {
57     return;
58   }
60   while (*string) {
61     *string = tolower(*string);
62     string++;
63   }
64 }
66 // Modifies fullpath with the path part including the trailing path separator
67 int 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
81   if (length == sizeOfPath) {
82     return -1;
83   }
85   fullpath[length] = 0;
86   getPathPart(fullpath);
88   return 0;
89 }
91 char* 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   }
98   // Obtain file size
99   fseek(pFile , 0 , SEEK_END);
100   unsigned int lSize = ftell(pFile);
101   rewind(pFile);
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   }
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   }
117   // Terminate
118   fclose(pFile);
120   if (appendNull) {
121     buffer[lSize] = 0;
122   }
124   if (size) {
125     *size = lSize;
126   }
128   return buffer;
131 int 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   }
138   // Write the data to disk
139   if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
140     fclose(pFile);
141     return -1;
142   }
144   // Terminate
145   fclose(pFile);
146   return 0;
149 int 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   }
156   // Write the data to disk
157   if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
158     fclose(pFile);
159     return -1;
160   }
162   // Terminate
163   fclose(pFile);
164   return 0;
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
173 int OlyUtility::copyFile(const char* srcFile, const char* dstFile) {
174   char buffer[TRANSFER_SIZE];
175   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);
200   return 1;
203 const char* OlyUtility::getFilePart(const char* path) {
204   const char* last_sep = strrchr(path, PATH_SEPARATOR);
206   // in case path is not a full path
207   if (last_sep == NULL) {
208     return path;
209   }
211   return last_sep++;
214 // getPathPart may modify the contents of path
215 // returns the path including the trailing path separator
216 char* OlyUtility::getPathPart(char* path) {
217   char* last_sep = strrchr(path, PATH_SEPARATOR);
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;
226   return (path);