13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DEFILEPATH_HPP 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DEFILEPATH_HPP 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------- 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Filesystem path class. 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.hpp" 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string> 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector> 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid FilePath_selfTest (void); 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass FilePath 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic: 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry enum Type 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry TYPE_UNKNOWN = 0, /*!< Non-existent or unknown object. */ 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry TYPE_FILE, /*!< File. */ 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry TYPE_DIRECTORY, /*!< Directory. */ 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry TYPE_LAST 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry }; 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static const std::string separator; /*!< Path separator. */ 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath (void); 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath (const std::string& path); 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath (const char* path); 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath (const std::vector<std::string>& components); 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry ~FilePath (void); 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry bool exists (void) const; 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry Type getType (void) const; 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const char* getPath (void) const; 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::string getBaseName (void) const; 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::string getDirName (void) const; 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::string getFileExtension (void) const; 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static FilePath join (const FilePath& a, const FilePath& b); 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath& join (const FilePath& b); 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static FilePath normalize (const FilePath& path); 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry FilePath& normalize (void); 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry void split (std::vector<std::string>& components) const; 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry bool isAbsolutePath (void) const; 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry static bool isSeparator (char c); 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate: 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry bool isRootPath (void) const; 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry bool isWinNetPath (void) const; 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry bool beginsWithDrive (void) const; 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::string m_path; 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// \todo [2012-09-05 pyry] Move to delibs? 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid createDirectory (const char* path); 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid createDirectoryAndParents (const char* path); 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath::FilePath (void) 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath::FilePath (const std::string& path) 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : m_path(path) 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath::FilePath (const char* path) 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : m_path(path) 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath::~FilePath () 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath& FilePath::join (const FilePath& b) 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (m_path == "") 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_path = b.m_path; 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry else 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_path += separator + b.m_path; 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return *this; 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline FilePath FilePath::join (const FilePath& a, const FilePath& b) 1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return FilePath(a).join(b); 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline const char* FilePath::getPath (void) const 1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return m_path.c_str(); 1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool FilePath::isSeparator (char c) 1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return c == '/' || c == '\\'; 1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool FilePath::isRootPath (void) const 1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return m_path.length() >= 1 && isSeparator(m_path[0]); 1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool FilePath::isWinNetPath (void) const 1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return m_path.length() >= 2 && isSeparator(m_path[0]) && isSeparator(m_path[1]); 1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de 1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _DEFILEPATH_HPP 143