2010-12-18 15:19:15 +00:00
|
|
|
/*
|
2011-11-04 15:53:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2010-12-18 15:19:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 SoftPLC Corporation, <dick@softplc.com>
|
2011-11-04 15:53:37 +00:00
|
|
|
* Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
|
2010-12-18 15:19:15 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Note: this LIB_SOURCE implementation relies on the posix specified opendir() and
|
2010-12-19 21:42:55 +00:00
|
|
|
related functions rather than wx functions which might do the same thing. This
|
|
|
|
is because I did not want to become very dependent on wxWidgets at such a low
|
|
|
|
level as this, in case someday this code needs to be used on kde or whatever.
|
|
|
|
|
|
|
|
Mingw and unix, linux, & osx will all have these posix functions.
|
2010-12-18 15:19:15 +00:00
|
|
|
MS Visual Studio may need the posix compatible opendir() functions brought in
|
|
|
|
http://www.softagalleria.net/dirent.php
|
2010-12-19 21:42:55 +00:00
|
|
|
wx has these but they are based on wxString which can be wchar_t based and wx should
|
|
|
|
not be introduced at a level this low.
|
2010-12-20 06:06:34 +00:00
|
|
|
|
|
|
|
Part files: have the general form partname.part[.revN...]
|
|
|
|
Categories: are any subdirectories immediately below the sourceURI, one level only.
|
|
|
|
Part names: [category/]partname[/revN...]
|
2010-12-18 15:19:15 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2010-12-23 08:40:53 +00:00
|
|
|
#include <assert.h>
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
#include <sch_dir_lib_source.h>
|
|
|
|
using namespace SCH;
|
2010-12-27 16:49:39 +00:00
|
|
|
|
|
|
|
|
2010-12-18 15:19:15 +00:00
|
|
|
/**
|
2010-12-19 21:42:55 +00:00
|
|
|
* Class DIR_WRAP
|
2010-12-20 06:06:34 +00:00
|
|
|
* provides a destructor which is invoked if an exception is thrown.
|
2010-12-18 15:19:15 +00:00
|
|
|
*/
|
2010-12-19 21:42:55 +00:00
|
|
|
class DIR_WRAP
|
2010-12-18 15:19:15 +00:00
|
|
|
{
|
2010-12-19 21:42:55 +00:00
|
|
|
DIR* dir;
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
public:
|
|
|
|
DIR_WRAP( DIR* aDir ) : dir( aDir ) {}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
~DIR_WRAP()
|
|
|
|
{
|
|
|
|
if( dir )
|
|
|
|
closedir( dir );
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
DIR* operator->() { return dir; }
|
|
|
|
DIR* operator*() { return dir; }
|
2010-12-20 06:06:34 +00:00
|
|
|
operator bool () { return dir!=0; }
|
2010-12-19 21:42:55 +00:00
|
|
|
};
|
2010-12-18 15:19:15 +00:00
|
|
|
|
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
/**
|
|
|
|
* Class FILE_WRAP
|
2010-12-20 06:06:34 +00:00
|
|
|
* provides a destructor which is invoked if an exception is thrown.
|
2010-12-19 21:42:55 +00:00
|
|
|
*/
|
|
|
|
class FILE_WRAP
|
|
|
|
{
|
|
|
|
int fh;
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
public:
|
|
|
|
FILE_WRAP( int aFileHandle ) : fh( aFileHandle ) {}
|
|
|
|
~FILE_WRAP()
|
|
|
|
{
|
|
|
|
if( fh != -1 )
|
|
|
|
close( fh );
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int () { return fh; }
|
2010-12-18 15:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
/**
|
|
|
|
* Function strrstr
|
|
|
|
* finds the last instance of needle in haystack, if any.
|
|
|
|
*/
|
|
|
|
static const char* strrstr( const char* haystack, const char* needle )
|
|
|
|
{
|
|
|
|
const char* ret = 0;
|
|
|
|
const char* next = haystack;
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
// find last instance of haystack
|
|
|
|
while( (next = strstr( next, needle )) != 0 )
|
|
|
|
{
|
|
|
|
ret = next;
|
|
|
|
++next; // don't keep finding the same one.
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-28 21:06:08 +00:00
|
|
|
#if 1 // @todo switch over to EndsWithRev() global
|
2010-12-20 16:19:45 +00:00
|
|
|
|
2010-12-28 13:23:43 +00:00
|
|
|
static inline bool isDigit( char c )
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
/**
|
|
|
|
* Function endsWithRev
|
2010-12-20 16:19:45 +00:00
|
|
|
* returns a pointer to the final string segment: "revN[N..]" or NULL if none.
|
2010-12-20 06:06:34 +00:00
|
|
|
* @param start is the beginning of string segment to test, the partname or
|
|
|
|
* any middle portion of it.
|
2010-12-20 16:19:45 +00:00
|
|
|
* @param tail is a pointer to the terminating nul, or one past inclusive end of
|
|
|
|
* segment, i.e. the string segment of interest is [start,tail)
|
2010-12-20 06:06:34 +00:00
|
|
|
* @param separator is the separating byte, expected: '.' or '/', depending on context.
|
|
|
|
*/
|
2010-12-30 22:15:53 +00:00
|
|
|
static const char* endsWithRev( const char* start, const char* tail, char separator = '/' )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-20 06:06:34 +00:00
|
|
|
bool sawDigit = false;
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-28 16:55:28 +00:00
|
|
|
while( tail > start && isDigit( *--tail ) )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
|
|
|
sawDigit = true;
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
// if sawDigit, tail points to the 'v' here.
|
|
|
|
|
|
|
|
if( sawDigit && tail-3 >= start )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
tail -= 3;
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
if( tail[0]==separator && tail[1]=='r' && tail[2]=='e' && tail[3]=='v' )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
return tail+1; // omit separator, return "revN[N..]"
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
static inline const char* endsWithRev( const STRING& aPartName, char separator = '/' )
|
2010-12-23 08:40:53 +00:00
|
|
|
{
|
|
|
|
return endsWithRev( aPartName.c_str(), aPartName.c_str()+aPartName.size(), separator );
|
|
|
|
}
|
|
|
|
|
2010-12-28 21:06:08 +00:00
|
|
|
#endif
|
|
|
|
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
// see struct BY_REV
|
2010-12-20 16:19:45 +00:00
|
|
|
bool BY_REV::operator() ( const STRING& s1, const STRING& s2 ) const
|
|
|
|
{
|
2010-12-20 17:12:41 +00:00
|
|
|
// avoid instantiating new STRINGs, and thank goodness that c_str() is const.
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* rev1 = endsWithRev( s1 );
|
|
|
|
const char* rev2 = endsWithRev( s2 );
|
2010-12-20 16:19:45 +00:00
|
|
|
|
|
|
|
int rootLen1 = rev1 ? rev1 - s1.c_str() : s1.size();
|
|
|
|
int rootLen2 = rev2 ? rev2 - s2.c_str() : s2.size();
|
|
|
|
|
|
|
|
int r = memcmp( s1.c_str(), s2.c_str(), min( rootLen1, rootLen2 ) );
|
|
|
|
|
|
|
|
if( r )
|
|
|
|
{
|
|
|
|
return r < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rootLen1 != rootLen2 )
|
|
|
|
{
|
|
|
|
return rootLen1 < rootLen2;
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
// root strings match at this point, compare the revision number numerically,
|
2010-12-20 16:19:45 +00:00
|
|
|
// and chose the higher numbered version as "less", according to std::set lingo.
|
|
|
|
|
|
|
|
if( bool(rev1) != bool(rev2) )
|
|
|
|
{
|
|
|
|
return bool(rev1) < bool(rev2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rev1 && rev2 )
|
|
|
|
{
|
|
|
|
int rnum1 = atoi( rev1+3 );
|
|
|
|
int rnum2 = atoi( rev2+3 );
|
|
|
|
|
2010-12-27 16:49:39 +00:00
|
|
|
// higher numbered revs are "less" so that they bubble to top.
|
2010-12-20 16:19:45 +00:00
|
|
|
return rnum1 > rnum2;
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
return false; // strings are equal, and they don't have a rev
|
2010-12-20 16:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
bool DIR_LIB_SOURCE::makePartName( STRING* aPartName, const char* aEntry,
|
|
|
|
const STRING& aCategory )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-27 16:49:39 +00:00
|
|
|
const char* cp = strrstr( aEntry, SWEET_EXT );
|
2010-12-19 21:42:55 +00:00
|
|
|
|
2010-12-27 16:49:39 +00:00
|
|
|
// if base name is not empty, contains SWEET_EXT, && cp is not NULL
|
2010-12-19 21:42:55 +00:00
|
|
|
if( cp > aEntry )
|
2010-12-18 15:19:15 +00:00
|
|
|
{
|
2010-12-19 21:42:55 +00:00
|
|
|
const char* limit = cp + strlen( cp );
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
// If versioning, then must find a trailing "revN.." type of string.
|
2010-12-19 21:42:55 +00:00
|
|
|
if( useVersioning )
|
|
|
|
{
|
2010-12-28 16:55:28 +00:00
|
|
|
const char* rev = endsWithRev( cp + SWEET_EXTZ, limit, '.' );
|
2010-12-19 21:42:55 +00:00
|
|
|
if( rev )
|
|
|
|
{
|
|
|
|
if( aCategory.size() )
|
|
|
|
*aPartName = aCategory + "/";
|
|
|
|
else
|
|
|
|
aPartName->clear();
|
|
|
|
|
|
|
|
aPartName->append( aEntry, cp - aEntry );
|
|
|
|
aPartName->append( "/" );
|
|
|
|
aPartName->append( rev );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-12-20 17:12:41 +00:00
|
|
|
|
|
|
|
// If using versioning, then all valid partnames must have a rev string,
|
|
|
|
// so we don't even bother to try and load any other partfile down here.
|
|
|
|
else
|
|
|
|
{
|
2010-12-27 16:49:39 +00:00
|
|
|
// if file extension is exactly SWEET_EXT, and no rev
|
2010-12-20 17:12:41 +00:00
|
|
|
if( cp==limit-5 )
|
|
|
|
{
|
|
|
|
if( aCategory.size() )
|
|
|
|
*aPartName = aCategory + "/";
|
|
|
|
else
|
|
|
|
aPartName->clear();
|
|
|
|
|
|
|
|
aPartName->append( aEntry, cp - aEntry );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-20 17:12:41 +00:00
|
|
|
|
|
|
|
STRING DIR_LIB_SOURCE::makeFileName( const STRING& aPartName )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-20 17:12:41 +00:00
|
|
|
// create a fileName for the sweet string, using a reversible
|
|
|
|
// partname <-> fileName conversion protocol:
|
|
|
|
|
|
|
|
STRING fileName = sourceURI + "/";
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* rev = endsWithRev( aPartName );
|
2010-12-20 17:12:41 +00:00
|
|
|
|
|
|
|
if( rev )
|
|
|
|
{
|
|
|
|
int basePartLen = rev - aPartName.c_str() - 1; // omit '/' separator
|
|
|
|
fileName.append( aPartName, 0, basePartLen );
|
2010-12-27 16:49:39 +00:00
|
|
|
fileName += SWEET_EXT;
|
|
|
|
fileName += '.';
|
2010-12-20 17:12:41 +00:00
|
|
|
fileName += rev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileName += aPartName;
|
2010-12-27 16:49:39 +00:00
|
|
|
fileName += SWEET_EXT;
|
2010-12-20 17:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileName;
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void DIR_LIB_SOURCE::readString( STRING* aResult, const STRING& aFileName )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-23 08:40:53 +00:00
|
|
|
FILE_WRAP fw = open( aFileName.c_str(), O_RDONLY );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
|
|
|
if( fw == -1 )
|
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
STRING msg = strerror( errno );
|
2010-12-23 08:40:53 +00:00
|
|
|
msg += "; cannot open(O_RDONLY) file " + aFileName;
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
struct stat fs;
|
|
|
|
|
|
|
|
fstat( fw, &fs );
|
|
|
|
|
|
|
|
// sanity check on file size
|
|
|
|
if( fs.st_size > (1*1024*1024) )
|
|
|
|
{
|
2010-12-23 08:40:53 +00:00
|
|
|
STRING msg = aFileName;
|
2010-12-20 16:19:45 +00:00
|
|
|
msg += " seems too big. ( > 1 mbyte )";
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
#if 0
|
|
|
|
// I read somewhere on the Internet that std::string chars are not guaranteed
|
|
|
|
// (over time) to be contiguous in future implementations of C++, so this
|
|
|
|
// strategy is here for that eventuality. We buffer through readBuffer here.
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
// reuse same readBuffer, which is not thread safe, but the API
|
|
|
|
// is not advertising thread safe (yet, if ever).
|
|
|
|
if( (int) fs.st_size > (int) readBuffer.size() )
|
|
|
|
readBuffer.resize( fs.st_size + 1000 );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
int count = read( fw, &readBuffer[0], fs.st_size );
|
|
|
|
if( count != (int) fs.st_size )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
STRING msg = strerror( errno );
|
2010-12-23 08:40:53 +00:00
|
|
|
msg += "; cannot read file " + aFileName;
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aResult->assign( &readBuffer[0], count );
|
2010-12-30 22:15:53 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
// read into the string directly
|
|
|
|
aResult->resize( fs.st_size );
|
|
|
|
|
|
|
|
int count = read( fw, &(*aResult)[0], fs.st_size );
|
|
|
|
if( count != (int) fs.st_size )
|
|
|
|
{
|
|
|
|
STRING msg = strerror( errno );
|
|
|
|
msg += "; cannot read file " + aFileName;
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
// test trailing nul is there, which should have been put there with resize() above
|
|
|
|
// printf( "'%s'\n", aResult->c_str() ); // checked OK.
|
|
|
|
#endif
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void DIR_LIB_SOURCE::cache()
|
2010-12-20 16:19:45 +00:00
|
|
|
{
|
|
|
|
partnames.clear();
|
|
|
|
categories.clear();
|
|
|
|
|
|
|
|
cacheOneDir( "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
DIR_LIB_SOURCE::DIR_LIB_SOURCE( const STRING& aDirectoryPath,
|
2017-06-08 21:47:21 +00:00
|
|
|
const STRING& aOptions ) :
|
2010-12-20 16:19:45 +00:00
|
|
|
useVersioning( strstr( aOptions.c_str(), "useVersioning" ) )
|
2010-12-18 15:19:15 +00:00
|
|
|
{
|
2010-12-19 21:42:55 +00:00
|
|
|
sourceURI = aDirectoryPath;
|
|
|
|
sourceType = "dir";
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
if( sourceURI.size() == 0 )
|
2010-12-18 15:19:15 +00:00
|
|
|
{
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( STRING("aDirectoryPath cannot be empty") );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove any trailing separator, so we can add it back later without ambiguity
|
|
|
|
if( strchr( "/\\", sourceURI[sourceURI.size()-1] ) )
|
|
|
|
sourceURI.erase( sourceURI.size()-1 );
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
cache();
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
DIR_LIB_SOURCE::~DIR_LIB_SOURCE()
|
|
|
|
{
|
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
void DIR_LIB_SOURCE::GetCategoricalPartNames( STRINGS* aResults, const STRING& aCategory )
|
|
|
|
{
|
2011-01-03 06:33:26 +00:00
|
|
|
PN_ITER end = aCategory.size() ?
|
2010-12-23 08:40:53 +00:00
|
|
|
partnames.lower_bound( aCategory + char( '/' + 1 ) ) :
|
|
|
|
partnames.end();
|
|
|
|
|
2011-01-03 06:33:26 +00:00
|
|
|
PN_ITER it = aCategory.size() ?
|
2010-12-27 16:49:39 +00:00
|
|
|
partnames.upper_bound( aCategory + "/" ) :
|
2010-12-23 08:40:53 +00:00
|
|
|
partnames.begin();
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
aResults->clear();
|
|
|
|
|
2010-12-23 08:40:53 +00:00
|
|
|
if( useVersioning )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-23 08:40:53 +00:00
|
|
|
STRING partName;
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-03 06:33:26 +00:00
|
|
|
while( it != end )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* rev = endsWithRev( *it );
|
2010-12-23 08:40:53 +00:00
|
|
|
|
|
|
|
// all cached partnames have a rev string in useVersioning mode
|
|
|
|
assert( rev );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-23 08:40:53 +00:00
|
|
|
// partName is substring which omits the rev AND the rev separator
|
|
|
|
partName.assign( *it, 0, rev - it->c_str() - 1 );
|
2010-12-20 17:12:41 +00:00
|
|
|
|
2010-12-23 08:40:53 +00:00
|
|
|
aResults->push_back( partName );
|
|
|
|
|
|
|
|
// skip over all other versions of the same partName.
|
|
|
|
it = partnames.lower_bound( partName + char( '/' + 1 ) );
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
else
|
|
|
|
{
|
2011-01-03 06:33:26 +00:00
|
|
|
while( it != end )
|
|
|
|
aResults->push_back( *it++ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIR_LIB_SOURCE::GetRevisions( STRINGS* aResults, const STRING& aPartName )
|
|
|
|
{
|
|
|
|
aResults->clear();
|
|
|
|
|
|
|
|
if( useVersioning )
|
|
|
|
{
|
|
|
|
STRING partName;
|
|
|
|
|
|
|
|
const char* rev = endsWithRev( aPartName );
|
|
|
|
if( rev )
|
|
|
|
// partName is substring which omits the rev and the separator
|
|
|
|
partName.assign( aPartName, 0, rev - aPartName.c_str() - 1 );
|
|
|
|
else
|
|
|
|
partName = aPartName;
|
|
|
|
|
|
|
|
PN_ITER it = partnames.upper_bound( partName +'/' );
|
|
|
|
PN_ITER end = partnames.lower_bound( partName + char( '/' +1 ) );
|
|
|
|
|
2011-01-03 20:01:46 +00:00
|
|
|
for( ; it != end; ++it )
|
|
|
|
{
|
|
|
|
const char* rev = endsWithRev( *it );
|
|
|
|
assert( rev );
|
|
|
|
aResults->push_back( it->substr( rev - it->c_str() ) );
|
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2011-01-09 13:35:13 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In non-version mode, there were no revisions read in, only part
|
|
|
|
// files without a revision. But clients higher up expect to see
|
|
|
|
// at least one revision in order for the API to work, so we return
|
|
|
|
// a revision ""
|
|
|
|
aResults->push_back( "" );
|
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIR_LIB_SOURCE::ReadPart( STRING* aResult, const STRING& aPartName, const STRING& aRev )
|
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
STRING partName = aPartName;
|
|
|
|
const char* hasRev = endsWithRev( partName );
|
2010-12-30 22:15:53 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
if( useVersioning )
|
2010-12-30 22:15:53 +00:00
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
if( aRev.size() )
|
|
|
|
{
|
|
|
|
// a supplied rev replaces any in aPartName
|
|
|
|
if( hasRev )
|
|
|
|
partName.resize( hasRev - partName.c_str() - 1 );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
partName += '/';
|
|
|
|
partName + aRev;
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
// find this exact revision
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
PN_ITER it = partnames.find( partName );
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
if( it == partnames.end() ) // part not found
|
|
|
|
{
|
|
|
|
partName += " not found.";
|
|
|
|
THROW_IO_ERROR( partName );
|
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
readString( aResult, makeFileName( partName ) );
|
|
|
|
}
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
else
|
2010-12-23 08:40:53 +00:00
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
// There's no rev on partName string. Find the most recent rev, i.e. highest,
|
|
|
|
// which will be first because of the BY_REV compare method, which treats
|
|
|
|
// higher numbered revs as first.
|
2010-12-23 08:40:53 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
STRING search = partName + '/';
|
|
|
|
|
|
|
|
// There's no rev on partName string. Find the most recent rev, i.e. highest,
|
|
|
|
// which will be first because of the BY_REV compare method, which treats
|
|
|
|
// higher numbered revs as first.
|
|
|
|
PN_ITER it = partnames.upper_bound( search );
|
|
|
|
|
|
|
|
// verify that this one that is greater than partName is a match and not
|
|
|
|
// some unrelated name that is larger.
|
|
|
|
if( it == partnames.end() ||
|
|
|
|
it->compare( 0, search.size(), search ) != 0 )
|
|
|
|
{
|
|
|
|
partName += " is not present without a revision.";
|
|
|
|
THROW_IO_ERROR( partName );
|
|
|
|
}
|
|
|
|
|
|
|
|
readString( aResult, makeFileName( *it ) );
|
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
2011-01-09 13:35:13 +00:00
|
|
|
|
|
|
|
else // !useVersioning
|
2010-12-23 08:40:53 +00:00
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
#if 1
|
|
|
|
if( hasRev || aRev.size() )
|
|
|
|
{
|
|
|
|
STRING msg = "this type 'dir' LIB_SOURCE not using 'useVersioning' option, cannot ask for a revision";
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// no revisions allowed, strip it
|
|
|
|
if( hasRev )
|
|
|
|
partName.resize( hasRev - partName.c_str() - 1 );
|
|
|
|
#endif
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
// find the part name without any revision
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
PN_ITER it = partnames.find( partName );
|
|
|
|
|
|
|
|
if( it == partnames.end() ) // part not found
|
2010-12-23 08:40:53 +00:00
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
partName += " not found.";
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( partName );
|
2010-12-23 08:40:53 +00:00
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2011-01-09 13:35:13 +00:00
|
|
|
readString( aResult, makeFileName( partName ) );
|
2010-12-23 08:40:53 +00:00
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIR_LIB_SOURCE::ReadParts( STRINGS* aResults, const STRINGS& aPartNames )
|
|
|
|
{
|
|
|
|
aResults->clear();
|
|
|
|
|
|
|
|
for( STRINGS::const_iterator n = aPartNames.begin(); n!=aPartNames.end(); ++n )
|
|
|
|
{
|
|
|
|
aResults->push_back( STRING() );
|
|
|
|
ReadPart( &aResults->back(), *n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void DIR_LIB_SOURCE::GetCategories( STRINGS* aResults )
|
2010-12-20 06:06:34 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
aResults->clear();
|
|
|
|
|
|
|
|
// caller fetches them sorted.
|
|
|
|
for( NAME_CACHE::const_iterator it = categories.begin(); it!=categories.end(); ++it )
|
|
|
|
{
|
|
|
|
aResults->push_back( *it );
|
|
|
|
}
|
2010-12-20 06:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
void DIR_LIB_SOURCE::Show()
|
|
|
|
{
|
2010-12-20 06:06:34 +00:00
|
|
|
printf( "Show categories:\n" );
|
2010-12-20 16:19:45 +00:00
|
|
|
for( NAME_CACHE::const_iterator it = categories.begin(); it!=categories.end(); ++it )
|
2010-12-19 21:42:55 +00:00
|
|
|
printf( " '%s'\n", it->c_str() );
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-19 21:42:55 +00:00
|
|
|
printf( "\n" );
|
2010-12-20 06:06:34 +00:00
|
|
|
printf( "Show parts:\n" );
|
2010-12-20 16:19:45 +00:00
|
|
|
for( PART_CACHE::const_iterator it = partnames.begin(); it != partnames.end(); ++it )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
printf( " '%s'\n", it->c_str() );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
2010-12-18 15:19:15 +00:00
|
|
|
}
|
2010-12-20 16:19:45 +00:00
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
#endif
|
2010-12-18 15:19:15 +00:00
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void DIR_LIB_SOURCE::cacheOneDir( const STRING& aCategory )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
|
|
|
STRING curDir = sourceURI;
|
|
|
|
|
|
|
|
if( aCategory.size() )
|
|
|
|
curDir += "/" + aCategory;
|
|
|
|
|
|
|
|
DIR_WRAP dir = opendir( curDir.c_str() );
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
if( !dir )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
|
|
|
STRING msg = strerror( errno );
|
|
|
|
msg += "; scanning directory " + curDir;
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct stat fs;
|
|
|
|
STRING partName;
|
|
|
|
STRING fileName;
|
|
|
|
dirent* entry;
|
|
|
|
|
|
|
|
while( (entry = readdir( *dir )) != NULL )
|
|
|
|
{
|
|
|
|
if( !strcmp( ".", entry->d_name ) || !strcmp( "..", entry->d_name ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fileName = curDir + "/" + entry->d_name;
|
|
|
|
|
|
|
|
if( !stat( fileName.c_str(), &fs ) )
|
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
// is this a valid part name?
|
2010-12-20 17:12:41 +00:00
|
|
|
if( S_ISREG( fs.st_mode ) && makePartName( &partName, entry->d_name, aCategory ) )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-20 16:19:45 +00:00
|
|
|
std::pair<NAME_CACHE::iterator, bool> pair = partnames.insert( partName );
|
|
|
|
|
|
|
|
if( !pair.second )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
|
|
|
STRING msg = partName;
|
|
|
|
msg += " has already been encountered";
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
// is this an acceptable category name?
|
2010-12-19 21:42:55 +00:00
|
|
|
else if( S_ISDIR( fs.st_mode ) && !aCategory.size() && isCategoryName( entry->d_name ) )
|
|
|
|
{
|
|
|
|
// only one level of recursion is used, controlled by the
|
|
|
|
// emptiness of aCategory.
|
2010-12-20 16:19:45 +00:00
|
|
|
categories.insert( entry->d_name );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
|
|
|
// somebody needs to test Windows (mingw), make sure it can
|
|
|
|
// handle opendir() recursively
|
2010-12-20 16:19:45 +00:00
|
|
|
cacheOneDir( entry->d_name );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//D( printf( "ignoring %s\n", entry->d_name );)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-29 20:30:45 +00:00
|
|
|
#if 0 && defined(DEBUG)
|
2010-12-18 15:19:15 +00:00
|
|
|
|
2010-12-28 21:06:08 +00:00
|
|
|
void DIR_LIB_SOURCE::Test( int argc, char** argv )
|
2010-12-18 15:19:15 +00:00
|
|
|
{
|
2010-12-20 06:06:34 +00:00
|
|
|
STRINGS partnames;
|
|
|
|
STRINGS sweets;
|
2010-12-19 21:42:55 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2010-12-23 08:40:53 +00:00
|
|
|
STRINGS::const_iterator pn;
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
// DIR_LIB_SOURCE uut( argv[1] ? argv[1] : "", "" );
|
|
|
|
DIR_LIB_SOURCE uut( argv[1] ? argv[1] : "", "useVersioning" );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-28 21:06:08 +00:00
|
|
|
// show the cached content, only the directory information is cached,
|
|
|
|
// parts are cached in class LIB, not down here.
|
2010-12-19 21:42:55 +00:00
|
|
|
uut.Show();
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-27 16:49:39 +00:00
|
|
|
uut.GetCategoricalPartNames( &partnames, "lions" );
|
2010-12-20 06:06:34 +00:00
|
|
|
|
2010-12-27 16:49:39 +00:00
|
|
|
printf( "\nGetCategoricalPartNames( aCatagory = 'lions' ):\n" );
|
2010-12-20 06:06:34 +00:00
|
|
|
for( STRINGS::const_iterator it = partnames.begin(); it!=partnames.end(); ++it )
|
|
|
|
{
|
|
|
|
printf( " '%s'\n", it->c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
uut.ReadParts( &sweets, partnames );
|
|
|
|
|
2010-12-27 16:49:39 +00:00
|
|
|
printf( "\nSweets for Category = 'lions' parts:\n" );
|
2010-12-23 08:40:53 +00:00
|
|
|
pn = partnames.begin();
|
|
|
|
for( STRINGS::const_iterator it = sweets.begin(); it!=sweets.end(); ++it, ++pn )
|
|
|
|
{
|
|
|
|
printf( " %s: %s", pn->c_str(), it->c_str() );
|
|
|
|
}
|
|
|
|
|
2010-12-20 06:06:34 +00:00
|
|
|
// fetch the part names for ALL categories.
|
|
|
|
uut.GetCategoricalPartNames( &partnames );
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
printf( "\nGetCategoricalPartNames( aCategory = '' i.e. ALL):\n" );
|
2010-12-20 06:06:34 +00:00
|
|
|
for( STRINGS::const_iterator it = partnames.begin(); it!=partnames.end(); ++it )
|
|
|
|
{
|
|
|
|
printf( " '%s'\n", it->c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
uut.ReadParts( &sweets, partnames );
|
|
|
|
|
2010-12-20 16:19:45 +00:00
|
|
|
printf( "\nSweets for ALL parts:\n" );
|
2010-12-23 08:40:53 +00:00
|
|
|
pn = partnames.begin();
|
2010-12-20 06:06:34 +00:00
|
|
|
for( STRINGS::const_iterator it = sweets.begin(); it!=sweets.end(); ++it, ++pn )
|
|
|
|
{
|
|
|
|
printf( " %s: %s", pn->c_str(), it->c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch( std::exception& ex )
|
|
|
|
{
|
|
|
|
printf( "std::exception\n" );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 13:33:04 +00:00
|
|
|
catch( const IO_ERROR& ioe )
|
2010-12-19 21:42:55 +00:00
|
|
|
{
|
2010-12-30 22:15:53 +00:00
|
|
|
printf( "exception: %s\n", (const char*) ioe.errorText.ToUTF8() ) );
|
2010-12-19 21:42:55 +00:00
|
|
|
}
|
2010-12-28 21:06:08 +00:00
|
|
|
}
|
2010-12-19 21:42:55 +00:00
|
|
|
|
2010-12-28 21:06:08 +00:00
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
|
|
|
DIR_LIB_SOURCE::Test( argc, argv );
|
2010-12-19 21:42:55 +00:00
|
|
|
return 0;
|
2010-12-18 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|