allplacefinder/lib/misc.cpp

188 lines
3.8 KiB
C++

/*
* This file is a part of AllPlaceFinder library
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream>
#include <sys/stat.h>
#include "utf8/utf8.h"
#include "misc.h"
#include "convert/convert.h"
namespace allplacefinder
{
bool file_exists(const std::wstring & file_path)
{
struct stat sb;
std::string file_path_utf8;
pt::wide_to_utf8(file_path, file_path_utf8);
if( stat(file_path_utf8.c_str(), &sb) != 0 )
return false;
return (sb.st_mode & S_IFREG) != 0;
}
bool dir_exists(const std::wstring & file_path)
{
struct stat sb;
std::string file_path_utf8;
pt::wide_to_utf8(file_path, file_path_utf8);
if( stat(file_path_utf8.c_str(), &sb) != 0 )
return false;
return (sb.st_mode & S_IFDIR) != 0;
}
bool open_file(const std::wstring & file_path, std::ifstream & str, const char * err_msg)
{
std::string file_path_utf8;
pt::wide_to_utf8(file_path, file_path_utf8);
str.clear();
str.open(file_path_utf8.c_str(), std::ios_base::in | std::ios_base::binary);
if( !str )
{
std::cerr << err_msg << std::endl;
return false;
}
return true;
}
void create_id_str(long id, std::vector<std::wstring> & path)
{
wchar_t buffer[64];
size_t len = sizeof(buffer) / sizeof(wchar_t);
size_t str_len = 0;
std::wstring d;
path.clear();
pt::Toa(id, buffer, len, 16, &str_len);
if( str_len == 0 )
return;
size_t padding = 0;
if( str_len < 16 )
padding = 16 - str_len;
d = L"00";
padding = padding & (~1);
for(size_t i=0 ; i < padding ; i += 2)
{
path.push_back(d);
}
d.clear();
size_t i = 0;
if( (str_len & 1) != 0 )
{
d = L"0";
d += buffer[0];
i = 1;
path.push_back(d);
}
for( ; i + 1 < str_len ; i += 2)
{
d = buffer[i];
d += buffer[i+1];
path.push_back(d);
}
}
bool create_dir(const std::wstring & path)
{
std::string path_utf8;
pt::wide_to_utf8(path, path_utf8);
return mkdir(path_utf8.c_str(), 0755) == 0;
}
bool create_dir_if_not_exists(const std::wstring & path)
{
if( !dir_exists(path) )
{
if( !create_dir(path) )
{
std::cout << "I cannot create directory: ";
print(path);
std::cout << std::endl;
return false;
}
}
return true;
}
void print(const std::wstring & str)
{
std::string str_utf8;
pt::wide_to_utf8(str, str_utf8);
std::cout << str_utf8;
}
void save_stream(const pt::TextStream & in, std::ofstream & out)
{
pt::TextStream::const_iterator i = in.begin();
for( ; i != in.end() ; ++i)
{
out << *i;
}
}
} // namespace