aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-03-02 22:54:30 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-03-02 22:54:30 -0800
commitee2298a313b6e425d6ff0324be6a313b1cd9a399 (patch)
treee23c2549bfe9c14afbabe8cc2afa1da5218b540b /android/utils
parentd944e7a273e10cb40d795bdc25503b97ee60ae66 (diff)
downloadexternal_qemu-ee2298a313b6e425d6ff0324be6a313b1cd9a399.zip
external_qemu-ee2298a313b6e425d6ff0324be6a313b1cd9a399.tar.gz
external_qemu-ee2298a313b6e425d6ff0324be6a313b1cd9a399.tar.bz2
auto import from //depot/cupcake/@137055
Diffstat (limited to 'android/utils')
-rw-r--r--android/utils/ini.c42
-rw-r--r--android/utils/ini.h5
-rw-r--r--android/utils/path.c129
-rw-r--r--android/utils/path.h30
4 files changed, 200 insertions, 6 deletions
diff --git a/android/utils/ini.c b/android/utils/ini.c
index 56e40f2..95bb4e3 100644
--- a/android/utils/ini.c
+++ b/android/utils/ini.c
@@ -17,6 +17,7 @@
#include <errno.h>
#include "android/utils/debug.h"
#include "android/utils/system.h" /* for ASTRDUP */
+#include "android/utils/bufprint.h"
#include "osdep.h"
/* W() is used to print warnings, D() to print debugging info */
@@ -172,7 +173,7 @@ iniFile_newFromMemory( const char* text, const char* fileName )
int lineno = 0;
if (!fileName)
- fileName = "<unknownFile>";
+ fileName = "<memoryFile>";
D("%s: parsing as .ini file", fileName);
@@ -197,8 +198,8 @@ iniFile_newFromMemory( const char* text, const char* fileName )
key = p++;
if (!isKeyStartChar(*key)) {
p = skipToEOL(p);
- W("%s:%d: key name doesn't start with valid character. line ignored",
- fileName, lineno);
+ W("%4d: key name doesn't start with valid character. line ignored",
+ lineno);
continue;
}
@@ -210,8 +211,8 @@ iniFile_newFromMemory( const char* text, const char* fileName )
/* check the equal */
if (*p != '=') {
- W("%s:%d: missing expected assignment operator (=). line ignored",
- fileName, lineno);
+ W("%4d: missing expected assignment operator (=). line ignored",
+ lineno);
p = skipToEOL(p);
continue;
}
@@ -232,12 +233,14 @@ iniFile_newFromMemory( const char* text, const char* fileName )
valueLen = p - value;
iniFile_addPair(ini, key, keyLen, value, valueLen);
- D("%s:%d: KEY='%.*s' VALUE='%.*s'", fileName, lineno,
+ D("%4d: KEY='%.*s' VALUE='%.*s'", lineno,
keyLen, key, valueLen, value);
p = skipToEOL(p);
}
+ D("%s: parsing finished", fileName);
+
return ini;
}
@@ -283,6 +286,33 @@ EXIT:
return ini;
}
+int
+iniFile_saveToFile( IniFile* f, const char* filepath )
+{
+ FILE* fp = fopen(filepath, "wt");
+ IniPair* pair = f->pairs;
+ IniPair* pairEnd = pair + f->numPairs;
+ int result = 0;
+
+ if (fp == NULL) {
+ D("could not create .ini file: %s: %s",
+ filepath, strerror(errno));
+ return -1;
+ }
+
+ for ( ; pair < pairEnd; pair++ ) {
+ char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);
+ p = bufprint(temp, end, "%s = %s\n", pair->key, pair->value);
+ if (fwrite(temp, p - temp, 1, fp) != 1) {
+ result = -1;
+ break;
+ }
+ }
+
+ fclose(fp);
+ return result;
+}
+
char*
iniFile_getString( IniFile* f, const char* key )
{
diff --git a/android/utils/ini.h b/android/utils/ini.h
index bc8193e..a176bfe 100644
--- a/android/utils/ini.h
+++ b/android/utils/ini.h
@@ -62,6 +62,11 @@ IniFile* iniFile_newFromMemory( const char* text, const char* fileName );
*/
IniFile* iniFile_newFromFile( const char* filePath);
+/* try to write an IniFile into a given file.
+ * returns 0 on success, -1 on error (see errno for error code)
+ */
+int iniFile_saveToFile( IniFile* f, const char* filePath );
+
/* free an IniFile object */
void iniFile_free( IniFile* f );
diff --git a/android/utils/path.c b/android/utils/path.c
index 9dd238e..b15b6de 100644
--- a/android/utils/path.c
+++ b/android/utils/path.c
@@ -101,6 +101,111 @@ path_parent( const char* path, int levels )
return result;
}
+static char*
+substring_dup( const char* start, const char* end )
+{
+ int len = end - start;
+ char* result = android_alloc(len+1);
+ memcpy(result, start, len);
+ result[len] = 0;
+ return result;
+}
+
+int
+path_split( const char* path, char* *pdirname, char* *pbasename )
+{
+ const char* end = path + strlen(path);
+ const char* last;
+ char* basename;
+
+ /* prepare for errors */
+ if (pdirname)
+ *pdirname = NULL;
+ if (pbasename)
+ *pbasename = NULL;
+
+ /* handle empty path case */
+ if (end == path) {
+ return -1;
+ }
+
+ /* strip trailing path separators */
+ while (end > path && ispathsep(end[-1]))
+ end -= 1;
+
+ /* handle "/" and degenerate cases like "////" */
+ if (end == path) {
+ return -1;
+ }
+
+ /* find last separator */
+ last = end;
+ while (last > path && !ispathsep(last[-1]))
+ last -= 1;
+
+ /* handle cases where there is no path separator */
+ if (last == path) {
+ if (pdirname)
+ *pdirname = ASTRDUP(".");
+ if (pbasename)
+ *pbasename = substring_dup(path,end);
+ return 0;
+ }
+
+ /* handle "/foo" */
+ if (last == path+1) {
+ if (pdirname)
+ *pdirname = ASTRDUP("/");
+ if (pbasename)
+ *pbasename = substring_dup(path+1,end);
+ return 0;
+ }
+
+ /* compute basename */
+ basename = substring_dup(last,end);
+ if (strcmp(basename, ".") == 0 || strcmp(basename, "..") == 0) {
+ AFREE(basename);
+ return -1;
+ }
+
+ if (pbasename)
+ *pbasename = basename;
+ else {
+ AFREE(basename);
+ }
+
+ /* compute dirname */
+ if (pdirname != NULL)
+ *pdirname = substring_dup(path,last-1);
+
+ return 0;
+}
+
+char*
+path_basename( const char* path )
+{
+ char* basename;
+
+ if (path_split(path, NULL, &basename) < 0)
+ return NULL;
+
+ return basename;
+}
+
+char*
+path_dirname( const char* path )
+{
+ char* dirname;
+
+ if (path_split(path, &dirname, NULL) < 0)
+ return NULL;
+
+ return dirname;
+}
+
+
+
+
/** MISC FILE AND DIRECTORY HANDLING
**/
@@ -290,6 +395,30 @@ path_get_size( const char* path, uint64_t *psize )
}
+ABool
+path_is_absolute( const char* path )
+{
+#ifdef _WIN32
+ if (path == NULL)
+ return 0;
+
+ if (path[0] == '/' || path[0] == '\\')
+ return 1;
+
+ /* 'C:' is always considered to be absolute
+ * even if used with a relative path like C:foo which
+ * is different from C:\foo
+ */
+ if (path[0] != 0 && path[1] == ':')
+ return 1;
+
+ return 0;
+#else
+ return (path != NULL && path[0] == '/');
+#endif
+}
+
+
/** OTHER FILE UTILITIES
**
** path_empty_file() creates an empty file at a given path location.
diff --git a/android/utils/path.h b/android/utils/path.h
index 10436c6..e822834 100644
--- a/android/utils/path.h
+++ b/android/utils/path.h
@@ -55,6 +55,9 @@ extern ABool path_is_regular( const char* path );
/* checks that a path points to a directory */
extern ABool path_is_dir( const char* path );
+/* checks that a path is absolute or not */
+extern ABool path_is_absolute( const char* path );
+
/* checks that one can read/write a given (regular) file */
extern ABool path_can_read( const char* path );
extern ABool path_can_write( const char* path );
@@ -77,6 +80,33 @@ extern APosixStatus path_get_size( const char* path, uint64_t *psize );
*/
extern char* path_parent( const char* path, int levels );
+/* split a path into a (dirname,basename) pair. the result strings must be freed
+ * by the caller. Return 0 on success, or -1 on error. Error conditions include
+ * the following:
+ * - 'path' is empty
+ * - 'path' is "/" or degenerate cases like "////"
+ * - basename is "." or ".."
+ *
+ * if there is no directory separator in path, *dirname will be set to "."
+ * if the path is of type "/foo", then *dirname will be set to "/"
+ *
+ * pdirname can be NULL if you don't want the directory name
+ * pbasename can be NULL if you don't want the base name
+ */
+extern int path_split( const char* path, char* *pdirname, char* *pbasename );
+
+/* a convenience function to retrieve the directory name as returned by
+ * path_split(). Returns NULL if path_split() returns an error.
+ * the result string must be freed by the caller
+ */
+extern char* path_dirname( const char* path );
+
+/* a convenience function to retrieve the base name as returned by
+ * path_split(). Returns NULL if path_split() returns an error.
+ * the result must be freed by the caller.
+ */
+extern char* path_basename( const char* path );
+
/** OTHER FILE UTILITIES
**
** path_empty_file() creates an empty file at a given path location.