kicad-source/common/increment.cpp
2024-10-26 15:31:12 +08:00

77 lines
2.2 KiB
C++

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
#include "increment.h"
#include <wx/wxcrt.h>
bool IncrementString( wxString& name, int aIncrement )
{
if( name.IsEmpty() )
return true;
wxString suffix;
wxString digits;
wxString outputFormat;
wxString outputNumber;
int ii = name.Len() - 1;
int dCount = 0;
while( ii >= 0 && !wxIsdigit( name.GetChar( ii ) ) )
{
suffix = name.GetChar( ii ) + suffix;
ii--;
}
while( ii >= 0 && wxIsdigit( name.GetChar( ii ) ) )
{
digits = name.GetChar( ii ) + digits;
ii--;
dCount++;
}
if( digits.IsEmpty() )
return true;
long number = 0;
if( digits.ToLong( &number ) )
{
number += aIncrement;
// Don't let result go below zero
if( number > -1 )
{
name.Remove( ii + 1 );
//write out a format string with correct number of leading zeroes
outputFormat.Printf( wxS( "%%0%dld" ), dCount );
//write out the number using the format string
outputNumber.Printf( outputFormat, number );
name << outputNumber << suffix;
return true;
}
}
return false;
}