1b15d33d0e56970caa2f4f5404f2debf68e3849f
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 void OlyUtility::stringToLower(char* string) {
25 if (string == NULL)
26 return;
28 while (*string) {
29 *string = tolower(*string);
30 string++;
31 }
32 }
34 // Modifies fullpath with the path part including the trailing path separator
35 int OlyUtility::getApplicationFullPath(char* fullpath, int sizeOfPath) {
36 memset(fullpath, 0, sizeOfPath);
37 #ifdef WIN32
38 int length = GetModuleFileName(NULL, fullpath, sizeOfPath);
39 #else
40 int length = readlink("/proc/self/exe", fullpath, sizeOfPath);
41 #endif
43 if (length == sizeOfPath)
44 return -1;
46 fullpath[length] = 0;
47 fullpath = getPathPart(fullpath);
49 return 0;
50 }
52 char* OlyUtility::readFromDisk(const char* file, unsigned int *size, bool appendNull) {
53 // Open the file
54 FILE* pFile = fopen(file, "rb");
55 if (pFile==NULL) return NULL;
57 // Obtain file size
58 fseek(pFile , 0 , SEEK_END);
59 unsigned int lSize = ftell(pFile);
60 rewind(pFile);
62 // Allocate memory to contain the whole file
63 char* buffer = (char*)malloc(lSize + (int)appendNull);
64 if (buffer == NULL) return NULL;
66 // Copy the file into the buffer
67 if (fread(buffer, 1, lSize, pFile) != lSize) return NULL;
69 // Terminate
70 fclose(pFile);
72 if (appendNull)
73 buffer[lSize] = 0;
75 if (size)
76 *size = lSize;
78 return buffer;
79 }
81 int OlyUtility::writeToDisk(const char* path, const char* data) {
82 // Open the file
83 FILE* pFile = fopen(path, "wb");
84 if (pFile == NULL) return -1;
86 // Write the data to disk
87 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) return -1;
89 // Terminate
90 fclose(pFile);
91 return 0;
92 }
94 int OlyUtility::appendToDisk(const char* path, const char* data) {
95 // Open the file
96 FILE* pFile = fopen(path, "a");
97 if (pFile == NULL) return -1;
99 // Write the data to disk
100 if (fwrite(data, 1, strlen(data), pFile) != strlen(data)) return -1;
102 // Terminate
103 fclose(pFile);
104 return 0;
105 }
107 /**
108 * Copies the srcFile into dstFile in 1kB chunks.
109 * The dstFile will be overwritten if it exists.
110 * 0 is returned on an error; otherwise 1.
111 */
112 #define TRANSFER_SIZE 1024
113 int OlyUtility::copyFile(const char * srcFile, const char * dstFile) {
114 char* buffer = (char*)malloc(TRANSFER_SIZE);
115 FILE * f_src = fopen(srcFile,"rb");
116 if (!f_src) {
117 return 0;
118 }
119 FILE * f_dst = fopen(dstFile,"wb");
120 if (!f_dst) {
121 fclose(f_src);
122 return 0;
123 }
124 while (!feof(f_src)) {
125 int num_bytes_read = fread(buffer, 1, TRANSFER_SIZE, f_src);
126 if (num_bytes_read < TRANSFER_SIZE && !feof(f_src)) {
127 fclose(f_src);
128 fclose(f_dst);
129 return 0;
130 }
131 int num_bytes_written = fwrite(buffer, 1, num_bytes_read, f_dst);
132 if (num_bytes_written != num_bytes_read) {
133 fclose(f_src);
134 fclose(f_dst);
135 return 0;
136 }
137 }
138 fclose(f_src);
139 fclose(f_dst);
140 free(buffer);
141 return 1;
142 }
144 const char* OlyUtility::getFilePart(const char* path) {
145 const char* last_sep = strrchr(path, PATH_SEPARATOR);
147 // in case path is not a full path
148 if (last_sep == NULL) {
149 return path;
150 }
152 return (const char*)((int)last_sep + 1);
153 }
155 // getPathPart may modify the contents of path
156 // returns the path including the trailing path separator
157 char* OlyUtility::getPathPart(char* path) {
158 char* last_sep = strrchr(path, PATH_SEPARATOR);
160 // in case path is not a full path
161 if (last_sep == NULL) {
162 return 0;
163 }
164 *(char*)((int)last_sep + 1) = 0;
166 return (path);
167 }