Write mkdirs in more idiomatic C++ style.
~ Rewrote mkdirs to be in C++ style.
~ Replaced adb_dir{start,stop} with std::string params and (r)find.
+ Added test for mkdirs.
Also make base/test_utils.h public and support temporary directories
as well as files.
Change-Id: I6fcbdc5e0099f3359d3aac6b00c436f250ca1329
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 729bbcb..3a3ffda 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -54,6 +54,8 @@
#include <windows.h>
#include <ws2tcpip.h>
+#include <string>
+
#include "fdevent.h"
#define OS_PATH_SEPARATOR '\\'
@@ -120,7 +122,7 @@
#undef unlink
#define unlink ___xxx_unlink
-static __inline__ int adb_mkdir(const char* path, int mode)
+static __inline__ int adb_mkdir(const std::string& path, int mode)
{
return _mkdir(path);
}
@@ -234,27 +236,25 @@
extern int adb_socketpair( int sv[2] );
-static __inline__ char* adb_dirstart( const char* path )
-{
- char* p = strchr(path, '/');
- char* p2 = strchr(path, '\\');
+static __inline__ size_t adb_dirstart(const std::string& path, size_t pos = 0) {
+ size_t p = path.find('/', pos);
+ size_t p2 = path.find('\\', pos);
- if ( !p )
+ if ( p == std::string::npos )
p = p2;
- else if ( p2 && p2 > p )
+ else if ( p2 != std::string::npos && p2 > p )
p = p2;
return p;
}
-static __inline__ const char* adb_dirstop( const char* path )
-{
- const char* p = strrchr(path, '/');
- const char* p2 = strrchr(path, '\\');
+static __inline__ size_t adb_dirstop(const std::string& path) {
+ size_t p = path.rfind('/');
+ size_t p2 = path.rfind('\\');
- if ( !p )
+ if ( p == std::string::npos )
p = p2;
- else if ( p2 && p2 > p )
+ else if ( p2 != std::string::npos && p2 > p )
p = p2;
return p;
@@ -284,6 +284,8 @@
#include <string.h>
#include <unistd.h>
+#include <string>
+
#define OS_PATH_SEPARATOR '/'
#define OS_PATH_SEPARATOR_STR "/"
#define ENV_PATH_SEPARATOR_STR ":"
@@ -510,10 +512,11 @@
usleep( mseconds*1000 );
}
-static __inline__ int adb_mkdir(const char* path, int mode)
+static __inline__ int adb_mkdir(const std::string& path, int mode)
{
- return mkdir(path, mode);
+ return mkdir(path.c_str(), mode);
}
+
#undef mkdir
#define mkdir ___xxx_mkdir
@@ -521,18 +524,15 @@
{
}
-static __inline__ const char* adb_dirstart(const char* path)
-{
- return strchr(path, '/');
+static __inline__ size_t adb_dirstart(const std::string& path, size_t pos = 0) {
+ return path.find('/', pos);
}
-static __inline__ const char* adb_dirstop(const char* path)
-{
- return strrchr(path, '/');
+static __inline__ size_t adb_dirstop(const std::string& path) {
+ return path.rfind('/');
}
-static __inline__ int adb_is_absolute_host_path( const char* path )
-{
+static __inline__ int adb_is_absolute_host_path(const char* path) {
return path[0] == '/';
}