2 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
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.
9 #include "OlyUtility.h"
18 #elif defined(__linux__)
21 #include <mach-o/dyld.h>
24 OlyUtility* util = NULL;
26 bool OlyUtility::stringToBool(const char* string, bool defValue) {
33 strncpy(value, string, sizeof(value));
37 value[sizeof(value) - 1] = 0; // strncpy does not guarantee a null-terminated string
39 // Convert to lowercase
42 value[i] = tolower(value[i]);
46 if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "1") == 0 || strcmp(value, "on") == 0) {
48 } else if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 || strcmp(value, "0") == 0 || strcmp(value, "off") == 0) {
55 void OlyUtility::stringToLower(char* string) {
61 *string = tolower(*string);
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);
70 int length = GetModuleFileName(NULL, fullpath, sizeOfPath);
71 #elif defined(__linux__)
72 int length = readlink("/proc/self/exe", fullpath, sizeOfPath);
74 uint32_t length_u = (uint32_t)sizeOfPath;
75 int length = sizeOfPath;
76 if (_NSGetExecutablePath(fullpath, &length_u) == 0) {
77 length = strlen(fullpath);
81 if (length == sizeOfPath) {
86 getPathPart(fullpath);
91 char* OlyUtility::readFromDisk(const char* file, unsigned int *size, bool appendNull) {
93 FILE* pFile = fopen(file, "rb");
99 fseek(pFile , 0 , SEEK_END);
100 unsigned int lSize = ftell(pFile);
103 // Allocate memory to contain the whole file
104 char* buffer = (char*)malloc(lSize + (int)appendNull);
105 if (buffer == NULL) {
110 // Copy the file into the buffer
111 if (fread(buffer, 1, lSize, pFile) != lSize) {
131 int OlyUtility::writeToDisk(const char* path, const char* data) {
133 FILE* pFile = fopen(path, "wb");
138 // Write the data to disk
139 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
149 int OlyUtility::appendToDisk(const char* path, const char* data) {
151 FILE* pFile = fopen(path, "a");
156 // Write the data to disk
157 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) {
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.
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");
179 FILE * f_dst = fopen(dstFile,"wb");
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)) {
191 int num_bytes_written = fwrite(buffer, 1, num_bytes_read, f_dst);
192 if (num_bytes_written != num_bytes_read) {
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) {
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) {