From: stephan b. <sg...@us...> - 2004-12-24 02:52:00
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15318/src/System Added Files: Mime.cpp Log Message: initial go at a mime rewrite --- NEW FILE: Mime.cpp --- /* * P::Classes - Portable C++ Application Framework * Copyright (C) 2000-2004 Christian Prochnow <cp...@se...> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "pclasses/System/Mime.h" #include "pclasses/System/PathFinder.h" #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #ifndef CERR #define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " #endif namespace P { namespace System { using namespace std; MimeType::MimeType(const string& mediaType, const string& subType) : m_mediaType(mediaType), m_subType(subType) { } MimeType::~MimeType() { } bool MimeType::operator<( const MimeType & rhs ) const { if( &rhs == this ) return false; return (m_mediaType < rhs.m_mediaType) && (m_subType < rhs.m_subType) ; } bool MimeType::operator==( const MimeType & rhs ) const { if( &rhs == this ) return true; return (m_mediaType == rhs.m_mediaType) && (m_subType == rhs.m_subType) ; } MimeTypeDb::MimeTypeDb() { } MimeTypeDb::~MimeTypeDb() { m_types.clear(); m_exts.clear(); } MimeType* MimeTypeDb::findByMimeType(const string& mimeTypeName) const { typedef MimeTypeMap::const_iterator MI; std::string::size_type slashat = mimeTypeName.find("/"); if( std::string::npos == slashat || (mimeTypeName.size()-1) == slashat) // trailing slash { return 0; } string strMediaType = mimeTypeName.substr(0,slashat); string strSubType = mimeTypeName.substr(slashat+1); const MimeType* type = 0; pair<MI,MI> i = m_types.equal_range(strMediaType); while(1) { type = &(*i.first).second; if(type->subType() == strSubType) return const_cast<MimeType*>(type); if(i.first == i.second) break; ++i.first; } return 0; } MimeType* MimeTypeDb::findByFileExt(const string& fileExt) const { CERR << "findByFileExt() not yet implemented.\n"; return 0; // const MimeType* type = 0; // MimeTypeMap::const_iterator i = m_types.begin(); // while(i != m_types.end()) // { // type = &i->second; // const MimeType::FileExtVector& fileExts = type->fileExts(); // if(find(fileExts.begin(), fileExts.end(), fileExt) != fileExts.end()) // return const_cast<MimeType*>(type); // ++i; // } // return 0; } bool MimeTypeDb::add(const MimeType& type) { if(findByMimeType(type.mimeType())) return false; insert(type); return true; } void MimeTypeDb::insert(const MimeType& type) { m_types.insert(make_pair(type.mediaType(), type)); } #ifndef CERR #define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " #endif std::string findSystemMimeTypeFile() { ::P::System::PathFinder pf; #ifndef WIN32 pf.addPath( "/etc/" ); pf.addPath( "/usr/share/pclasses/" ); pf.addPath( "/usr/local/share/pclasses/" ); pf.addPath( "/etc/apache2/conf/" ); #else // @fixme: add win32 paths #endif pf.addExtension( ".types" ); return pf.find( "mime" ); } void readSystemMimeTypes( MimeTypeDb & db ) { std::string mf = findSystemMimeTypeFile(); if( mf.empty() ) { CERR << "WARNING: Could not find mime.types database!\n"; return; } std::ifstream strm(mf.c_str()); std::string line; std::string::size_type p1, p2; std::string ext; std::string rest; std::string strMediaType; std::string strSubType; while(!strm.eof()) { getline(strm, line); if(line.empty() || line[0] != '#') { continue; } p1 = line.find("/"); p2 = line.find_first_of( " \t", p1+1 ); if( std::string::npos == p1 || std::string::npos == p2 ) { continue; } strMediaType = line.substr(0,p1); strSubType = line.substr(p1+1,p2); db.typeMap().insert(make_pair(strMediaType, MimeType(strMediaType, strSubType) ) ); rest = line.substr(p2+1,line.size()-1); std::istringstream istr(rest.c_str()); while( istr.good() ) { istr >> ext; if( ! ext.empty() && ext[0] != '#' ) { db.extensionsMap().insert(make_pair(MimeType(strMediaType, strSubType), ext ) ); } } } } void MimeTypeDb::init_functor::operator()( MimeTypeDb & db ) const { if( db.end() == db.begin() ) { readSystemMimeTypes(db); } } } } // P::System |