From bb9bc9051629c3319c56785c2f4ae0e605d76329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sat, 21 Nov 2015 14:51:17 +0100 Subject: Initial import of bitz-server version 0.1.6-1 --- src/bitz/util.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/bitz/util.cpp (limited to 'src/bitz/util.cpp') diff --git a/src/bitz/util.cpp b/src/bitz/util.cpp new file mode 100644 index 0000000..8fedc85 --- /dev/null +++ b/src/bitz/util.cpp @@ -0,0 +1,122 @@ +/* + * bitz-server, An ICAP server implementation in C++ + * Copyright (C) 2012 Uditha Atukorala + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "util.h" + +#include +#include + + +namespace bitz { + + namespace util { + + RequestHandler * find_req_handler( req_handlers_t req_handlers, const std::string &req_method ) throw() { + + RequestHandler * req_handler; + req_handlers_index_t rh_i; + rh_i = req_handlers.find( req_method ); + + if ( rh_i != req_handlers.end() ) { + req_handler = rh_i->second; + } else { + req_handler = NULL; + } + + return req_handler; + + } + + + void delete_req_handlers( req_handlers_t req_handlers ) throw() { + + req_handlers_index_t rh_i; + + for ( rh_i = req_handlers.begin(); rh_i != req_handlers.end(); rh_i++ ) { + if ( rh_i->first != "OPTIONS" ) { + delete rh_i->second; + } + } + + return; + + } + + + std::string dirpath( const std::string &path ) throw() { + return path.substr( 0, path.find_last_of( '/' ) ); + } + + + std::string filename( const std::string &path ) throw() { + return path.substr( path.find_last_of( '/' ) ); + } + + + bool mkdirp( const std::string &path ) throw() { + + bool r_success = false; + + + if ( ::mkdir( path.c_str(), 0755 ) == -1 ) { + + switch( errno ) { + + case ENOENT: + + // parent didn't exist, try to create it + if ( mkdirp( path.substr( 0, path.find_last_of( '/' ) ) ) ) { + + // try creating the dir again + r_success = ( 0 == ::mkdir( path.c_str(), 0755 ) ); + + } else { + + // failed to create parent + r_success = false; + } + + break; + + case EEXIST: + + // already exists + r_success = true; + break; + + default: + r_success = false; + break; + + } + + } else { + r_success = true; + } + + + return r_success; + + } + + + } /* end of namespace util */ + +} /* end of namespace bitz */ + -- cgit v1.2.3