root/trunk/0015_Path/PathTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 5.48 KB

(June 16, 2010 10:58 UTC) Almost 2 years ago

0015_Path

 
Show/hide line numbers
//
//  PathTest.cpp
//
//  Created by Setsu on 5/16/2010.
//  Copyright 2010 RoundSquare Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

//----------------------------------------
//	include
//----------------------------------------
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/Path.h>
#include <Poco/Format.h>

#include <string>
#include <vector>
#include <algorithm>

#include "ScopedLogMessage.h"

//----------------------------------------
//	PrepareConsoleLogger
//----------------------------------------
void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION)
{
	Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
	pFCConsole->setChannel(new Poco::ConsoleChannel);
	pFCConsole->open();

	Poco::Logger::create(name, pFCConsole, level);
}

//----------------------------------------
//	ListRoots
//----------------------------------------
class ListRoots
{
public:
	ListRoots(ScopedLogMessage& msg) :
		m_msg(msg)
	,	m_count(0)
	{
	}
    void operator()(std::string root)
	{
		m_msg.Message(Poco::format("    root[%z]: %s", m_count++, root));
	}

private:
	ScopedLogMessage&	m_msg;
	std::size_t			m_count;
};

//----------------------------------------
//	IsTrue
//----------------------------------------
inline std::string IsTrue(bool isTrue)
{
	return std::string(isTrue ? "true":"false");
};

//----------------------------------------
//	ShowPathCharacteristics
//----------------------------------------
void ShowPathCharacteristics(ScopedLogMessage& msg, const Poco::Path& path)
{
	msg.Message(Poco::format("    isAbsolute() : %s", IsTrue(path.isAbsolute())));
	msg.Message(Poco::format("    isRelative() : %s", IsTrue(path.isRelative())));
	msg.Message(Poco::format("    isDirectory(): %s", IsTrue(path.isDirectory())));
	msg.Message(Poco::format("    isFile()     : %s", IsTrue(path.isFile())));
};

//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("PathTest ", "start", "end");
	msg.Message("");

	msg.Message("--- Static methods ---");
	msg.Message(Poco::format("  home()       (Home directory): %s", Poco::Path::home()));
	msg.Message(Poco::format("  expand(\"%s\")                 : %s", std::string("~/"), Poco::Path::expand("~/")));
	msg.Message(Poco::format("  current() (Current directory): %s", Poco::Path::current()));
	msg.Message(Poco::format("  temp()  (Temporary directory): %s", Poco::Path::temp()));
	msg.Message(Poco::format("  separator()                  : %c", Poco::Path::separator()));
	msg.Message(Poco::format("  pathSeparator()              : %c", Poco::Path::pathSeparator()));
	msg.Message(Poco::format("  null()          (Null device): %s", Poco::Path::null()));

	msg.Message("  listRoots(roots)");
	std::vector<std::string> roots;
	Poco::Path::listRoots(roots);
	std::for_each(roots.begin(), roots.end(), ListRoots(msg));
	msg.Message("");

	msg.Message("--- Relative path ---");
	Poco::Path dirPath("./");
	msg.Message(Poco::format("  dirPath(\"%s\")               : %s", std::string("./"), dirPath.toString()));
	ShowPathCharacteristics(msg, dirPath);

	Poco::Path filePath(dirPath);
	msg.Message(Poco::format("  filePath(dirPath)           : %s", dirPath.toString()));
	filePath.setFileName("temp");
	msg.Message(Poco::format("  filePath.setFileName(\"%s\"): %s", std::string("temp"), filePath.toString()));
	filePath.setExtension("txt");
	msg.Message(Poco::format("  filePath.setExtension(\"%s\"): %s", std::string("txt"), filePath.toString()));
	ShowPathCharacteristics(msg, filePath);
	msg.Message("");

	msg.Message("--- Absolute path ---");
	Poco::Path absDirPath(dirPath.makeAbsolute());
	msg.Message(Poco::format("  absDirPath(dirPath.makeAbsolute())  : %s", absDirPath.toString()));
	ShowPathCharacteristics(msg, absDirPath);

	Poco::Path absFilePath(filePath.makeAbsolute());
	msg.Message(Poco::format("  absFilePath(filePath.makeAbsolute()): %s", absFilePath.toString()));
	ShowPathCharacteristics(msg, absFilePath);
	msg.Message("");

	return 0;
}