Sleipnir
src/file.h
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #ifndef FILE_H
00023 #define FILE_H
00024 
00025 #ifdef _MSC_VER
00026 #define FOR_EACH_DIRECTORY_FILE(strDir, strFile)                                            \
00027     HANDLE          hSearch;                                                                \
00028     WIN32_FIND_DATA sEntry;                                                                 \
00029     bool            fOk;                                                                    \
00030     for( fOk = ( ( hSearch = FindFirstFile( ( (strDir) + "/*" ).c_str( ), &sEntry ) )       \
00031         != INVALID_HANDLE_VALUE ); fOk; fOk = !!FindNextFile( hSearch, &sEntry ) ) {        \
00032         if( sEntry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )                            \
00033             continue;                                                                       \
00034         (strFile) = sEntry.cFileName;
00035 #else // _MSC_VER
00036 #include <dirent.h>
00037 #include <sys/stat.h>
00038 
00039 #define FOR_EACH_DIRECTORY_FILE(strDir, strFile)                                            \
00040     DIR*            pDir;                                                                   \
00041     struct dirent*  psEntry;                                                                \
00042     struct stat     sStat;                                                                  \
00043     pDir = opendir( (strDir).c_str( ) );                                                    \
00044     while( pDir && ( psEntry = readdir( pDir ) ) ) {                                        \
00045         stat( ( (strDir) + '/' + psEntry->d_name ).c_str( ), &sStat );                      \
00046         if( S_ISDIR( sStat.st_mode ) )                                                      \
00047             continue;                                                                       \
00048         (strFile) = psEntry->d_name;
00049 #endif // _MSC_VER
00050 
00051 #include <iostream>
00052 #include <string>
00053 
00054 #include "filei.h"
00055 
00056 namespace Sleipnir {
00057 
00066 class CFile : protected CFileImpl {
00067 public:
00068     static std::string OpenToken( std::istream& istm );
00069 
00070     static size_t GetBufferSize( ) {
00071 
00072         return c_iBufferSize; }
00073 
00074     static bool IsNewline( char c ) {
00075 
00076         return ( ( c == '\n' ) || ( c == '\r' ) ); }
00077 
00091     static std::string OpenToken( const char* szInput, const char** ppcEnd = NULL ) {
00092         const char* pcStart;
00093         const char* pcEnd;
00094         char        c;
00095 
00096         do
00097             c = *(szInput++);
00098         while( c && ( c != -1 ) && ( c != '\t' ) && isspace( c ) );
00099         pcStart = szInput - 1;
00100         for( ; c && ( c != -1 ) && ( c != '\t' ) && !IsNewline( c ); c = *(szInput++) );
00101         pcEnd = szInput;
00102         if( ppcEnd )
00103             *ppcEnd = pcEnd - ( ( !c || IsNewline( c ) ) ? 1 : 0 );
00104 
00105         return std::string( pcStart, pcEnd - pcStart - 1 ); }
00106 };
00107 
00108 }
00109 
00110 #endif // FILE_H