Minor FP_LIB_ID object changes.

This commit is contained in:
Wayne Stambaugh 2012-10-15 16:48:01 -04:00
parent 0dc4d9c19e
commit b1ea9d0eb5
3 changed files with 64 additions and 158 deletions

View File

@ -2,6 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -76,31 +77,12 @@ int RevCmp( const char* s1, const char* s2 )
//----<Policy and field test functions>-------------------------------------
// These all return -1 on success, or >= 0 if there is an error at a
// particular character offset into their respective arguments. If >=0,
// then that return value gives the character offset of the error.
static inline int okLogical( const std::string& aField )
{
// std::string::npos is largest positive number, casting to int makes it -1.
// Returning that means success.
return int( aField.find_first_of( ":/" ) );
}
static inline int okBase( const std::string& aField )
{
int offset = int( aField.find_first_of( ":/" ) );
if( offset != -1 )
return offset;
// cannot be empty
if( !aField.size() )
return 0;
return offset; // ie. -1
return int( aField.find_first_of( ":" ) );
}
@ -120,13 +102,13 @@ static int okRevision( const std::string& aField )
return 0; // first character position "is in error", is best we can do.
}
//----</Policy and field test functions>-------------------------------------
void FP_LIB_ID::clear()
{
logical.clear();
baseName.clear();
footprintName.clear();
revision.clear();
}
@ -139,7 +121,6 @@ int FP_LIB_ID::Parse( const std::string& aId )
const char* rev = EndsWithRev( aId );
size_t revNdx;
size_t partNdx;
size_t baseNdx;
int offset;
//=====<revision>=========================================
@ -149,30 +130,28 @@ int FP_LIB_ID::Parse( const std::string& aId )
// no need to check revision, EndsWithRev did that.
revision = aId.substr( revNdx );
--revNdx; // back up to omit the '/' which preceeds the rev
--revNdx; // back up to omit the '/' which precedes the rev
}
else
{
revNdx = aId.size();
}
//=====<logical>==========================================
if( ( partNdx = aId.find( ':' ) ) != aId.npos )
{
offset = SetLogicalLib( aId.substr( 0, partNdx ) );
if( offset > -1 )
{
return offset;
}
++partNdx; // skip ':'
}
else
partNdx = 0;
//=====<baseName>==========================================
offset = SetBaseName( aId.substr( baseNdx, revNdx - baseNdx ) );
if( offset > -1 )
{
return offset + baseNdx;
partNdx = 0;
}
return -1;
@ -207,37 +186,18 @@ int FP_LIB_ID::SetLogicalLib( const std::string& aLogical )
}
int FP_LIB_ID::SetBaseName( const std::string& aBaseName )
{
int offset = okBase( aBaseName );
if( offset == -1 )
{
baseName = aBaseName;
}
return offset;
}
int FP_LIB_ID::SetFootprintName( const std::string& aFootprintName )
{
std::string base;
int offset;
int separation = int( aFootprintName.find_first_of( "/" ) );
if( separation != -1 )
{
base = aFootprintName.substr( separation+1 );
logical = aFootprintName.substr( separation+1 );
return separation + (int) logical.size() + 1;
}
else
{
base = aFootprintName;
}
if( (offset = SetBaseName( base )) != -1 )
{
return offset + separation + 1;
footprintName = aFootprintName;
}
return -1;
@ -267,8 +227,6 @@ std::string FP_LIB_ID::Format() const
ret += ':';
}
ret += baseName;
if( revision.size() )
{
ret += '/';
@ -283,8 +241,6 @@ std::string FP_LIB_ID::GetFootprintNameAndRev() const
{
std::string ret;
ret += baseName;
if( revision.size() )
{
ret += '/';
@ -308,7 +264,7 @@ std::string FP_LIB_ID::Format( const std::string& aLogicalLib, const std::string
if( offset != -1 )
{
THROW_PARSE_ERROR( _( "Illegal character found in logical lib name" ),
THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
wxString::FromUTF8( aLogicalLib.c_str() ),
aLogicalLib.c_str(),
0,
@ -319,33 +275,6 @@ std::string FP_LIB_ID::Format( const std::string& aLogicalLib, const std::string
ret += ':';
}
{
std::string base;
int separation = int( aFootprintName.find_first_of( "/" ) );
if( separation != -1 )
{
base = aFootprintName.substr( separation+1 );
}
else
{
base = aFootprintName;
}
if( (offset = okBase( base )) != -1 )
{
THROW_PARSE_ERROR( _( "Illegal character found in base name" ),
wxString::FromUTF8( aRevision.c_str() ),
aRevision.c_str(),
0,
offset + separation + 1 );
}
ret += base;
}
if( aRevision.size() )
{
offset = okRevision( aRevision );
@ -374,16 +303,9 @@ std::string FP_LIB_ID::Format( const std::string& aLogicalLib, const std::string
void FP_LIB_ID::Test()
{
static const char* lpids[] = {
"/R/rev0",
"passives/R/rev2",
":passives/R/rev3",
"C/rev22",
"passives/C22",
"R",
"me:R",
// most difficult:
"me:/R/rev0",
"me:R/rev0",
"smt:R_0805/rev0",
"mysmt:R_0805/rev2",
"device:AXIAL-0500",
};
for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
@ -393,10 +315,10 @@ void FP_LIB_ID::Test()
FP_LIB_ID lpid( lpids[i] ); // parse
// format
printf( "input:'%s' full:'%s' base:'%s' footprintName:'%s' rev:'%s'\n",
printf( "input:'%s' full:'%s' logical: %s footprintName:'%s' rev:'%s'\n",
lpids[i],
lpid.Format().c_str(),
lpid.GetBaseName().c_str(),
lpid.GetLogicalLib().c_str(),
lpid.GetFootprintName().c_str(),
lpid.GetRevision().c_str() );
}

View File

@ -304,4 +304,3 @@ bool FP_LIB_TABLE::InsertRow( const ROW& aRow, bool doReplace )
return false;
}

View File

@ -1,7 +1,8 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -30,26 +31,26 @@
/**
* Class FP_LIB_ID
* (aka GUID) is a Logical Part ID and consists of various portions much like a URI.
* It is a container for the separated portions of a logical part id std::string so they
* is a Logical Footprint ID and consists of various portions much like a URI.
* It is a container for the separated portions of a logical footprint id so they
* can be accessed individually. The various portions of an FP_LIB_ID are:
* logicalLibraryName, category, baseName, and revision. Only the baseName is
* mandatory. There is another construct called "footprintName" which consists of
* [category/]baseName. That is the category followed by a slash, but only if
* the category is not empty.
* <p>
* footprintName = [category/]baseName
* <p>
* logicalLibraryName (nick name), footprint name, and revision. The logical library
* name and the footprint name are mandatory. The revision is optional and currently is
* not used.
*
* Example FP_LIB_ID string:
* "smt:R_0805".
* "smt:R_0805/rev0".
*
* <p>
* <ul>
* <li> "smt" is the logical library name.
* <li> "R" is the footprint name.
* <li> "rev6" is the revision, which is optional. If missing then its
* <li> "smt" is the logical library name used to look up library information saved in the
* #FP_LIB_TABLE.
* <li> "R" is the name of the footprint within the library.
* <li> "rev0" is the revision, which is optional. If missing then its
* / delimiter should also not be present. A revision must begin with
* "rev" and be followed by at least one or more decimal digits.
* </ul>
*
* @author Dick Hollenbeck
*/
class FP_LIB_ID // aka GUID
@ -60,26 +61,28 @@ public:
/**
* Constructor FP_LIB_ID
* takes \a aId string and parses it. A typical FP_LIB_ID string uses a logical
* takes \a aId string and parses it. A typical FP_LIB_ID string consists of a logical
* library name followed by a footprint name.
* e.g.: "smt:R_0805", or
* e.g.: "mylib:R_0805"
*
* @param aId is a string to be parsed into the FP_LIB_ID object.
*/
FP_LIB_ID( const std::string& aId ) throw( PARSE_ERROR );
/**
* Function Parse
* [re-]stuffs this FP_LIB_ID with the information from @a aId.
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the
* character offset into aId at which an error was detected.
*
* @param aId is the string to populate the #FP_LIB_ID object.
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset into
* aId at which an error was detected.
*/
int Parse( const std::string& aId );
/**
* Function GetLogicalLib
* returns the logical library portion of a FP_LIB_ID. There is not Set accessor
* for this portion since it comes from the library table and is considered
* read only here.
* returns the logical library name portion of a FP_LIB_ID.
*/
const std::string& GetLogicalLib() const
{
@ -87,35 +90,17 @@ public:
}
/**
* Function SetCategory
* overrides the logical lib name portion of the FP_LIB_ID to @a aLogical, and can be empty.
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the
* character offset into the parameter at which an error was detected, usually
* because it contained '/' or ':'.
* Function SetLogicalLib
* overrides the logical footprint library name portion of the FP_LIB_ID to @a aLogical.
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
* into the parameter at which an error was detected, usually because it
* contained '/' or ':'.
*/
int SetLogicalLib( const std::string& aLogical );
/**
* Function GetBaseName
* returns the part name without the category.
*/
const std::string& GetBaseName() const
{
return baseName;
}
/**
* Function SetBaseName
* overrides the base name portion of the FP_LIB_ID to @a aBaseName
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the
* character offset into the parameter at which an error was detected, usually
* because it contained '/' or ':', or is blank.
*/
int SetBaseName( const std::string& aBaseName );
/**
* Function GetFootprintName
* returns the part name, i.e. category/baseName without revision.
* returns the footprint name, i.e. footprintName part without revision.
*/
const std::string& GetFootprintName() const
{
@ -124,18 +109,17 @@ public:
/**
* Function GetFootprintNameAndRev
* returns the part name with revision if any, i.e. baseName[/revN..]
* returns the part name with revision if any, i.e. footprintName[/revN..]
*/
std::string GetFootprintNameAndRev() const;
/**
* Function SetFootprintName
* overrides the part name portion of the FP_LIB_ID to @a aFootprintName
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the
* character offset into the parameter at which an error was detected, usually
* because it contained more than one '/', or one or more ':', or is blank.
* A single '/' is allowed, since that is used to separate the category from the
* base name.
* overrides the footprint name portion of the FP_LIB_ID to @a aFootprintName
*
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
* into the parameter at which an error was detected, usually because it contained
* more than one '/', or one or more ':', or is blank.
*/
int SetFootprintName( const std::string& aFootprintName );
@ -152,15 +136,16 @@ public:
* Function SetRevision
* overrides the revision portion of the FP_LIB_ID to @a aRevision and must
* be in the form "rev<num>" where "<num>" is "1", "2", etc.
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the
* character offset into the parameter at which an error was detected,
* because it did not look like "rev23"
*
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset*
* into the parameter at which an error was detected,because it did not
* look like "rev23"
*/
int SetRevision( const std::string& aRevision );
/**
* Function Format
* returns the full text of the FP_LIB_ID.
* returns the fully formatted text of the FP_LIB_ID.
*/
std::string Format() const;
@ -168,6 +153,7 @@ public:
* Function Format
* returns a std::string in the proper format as an FP_LIB_ID for a combination of
* aLogicalLib, aFootprintName, and aRevision.
*
* @throw PARSE_ERROR if any of the pieces are illegal.
*/
static std::string Format( const std::string& aLogicalLib, const std::string& aFootprintName,
@ -182,9 +168,8 @@ public:
protected:
std::string logical; ///< logical lib name or empty
std::string baseName; ///< without category
std::string revision; ///< "revN[N..]" or empty
std::string footprintName; ///< cannot be set directory, set via SetBaseName() & SetCategory()
std::string footprintName; ///< The name of the footprint in the logical library.
};
/**