This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding trip_step source file to remove duplicate lines from STEP files
- Loading branch information
Seth Hillbrand
committed
Jan 20, 2018
1 parent
b15c9a6
commit bbdc364
Showing
1 changed file
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* This program source code file is part of KiCad, a free EDA CAD application. | ||
* | ||
* Copyright (C) 2018 Seth Hillbrand | ||
* | ||
* 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 | ||
*/ | ||
|
||
/** | ||
* @file trim_step.cpp | ||
*/ | ||
|
||
#include <algorithm> | ||
#include <map> | ||
#include <string> | ||
#include <sstream> | ||
#include <vector> | ||
#include <fstream> | ||
#include <regex> | ||
#include <iostream> | ||
|
||
// trim from end (in place) | ||
static inline void rtrim(std::string &s) { | ||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { | ||
return !std::isspace(ch); | ||
}).base(), s.end()); | ||
} | ||
// trim from start (in place) | ||
static inline void ltrim(std::string &s) { | ||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { | ||
return !std::isspace(ch); | ||
})); | ||
} | ||
template<typename Out> | ||
void split(const std::string &s, char delim, Out result) { | ||
std::stringstream ss(s); | ||
std::string item; | ||
while (std::getline(ss, item, delim)) { | ||
*(result++) = item; | ||
} | ||
} | ||
|
||
int main( int argc, char **argv) | ||
{ | ||
size_t last_file_lines = (size_t)-1; | ||
size_t file_lines = last_file_lines - 1; | ||
|
||
std::string input_file(argv[1]); | ||
std::string output_file(argv[1]); | ||
|
||
while( file_lines < last_file_lines ) | ||
{ | ||
std::ifstream is(input_file); | ||
|
||
std::map<std::string, unsigned> uniques; | ||
std::map<unsigned, unsigned> lookup; | ||
std::vector<std::string> lines; | ||
std::vector<std::string> footer; | ||
std::vector<std::string> header; | ||
|
||
bool past_header = false; | ||
lines.reserve( 10000 ); | ||
|
||
while(is) | ||
{ | ||
std::string line; | ||
std::getline( is, line ); | ||
|
||
rtrim(line); | ||
std::vector<std::string> elems; | ||
split(line, '=', std::back_inserter(elems)); | ||
|
||
if( line.front() != '#' || elems.size() != 2 ) | ||
{ | ||
if( !past_header ) | ||
header.push_back( line ); | ||
else | ||
footer.push_back( line ); | ||
|
||
continue; | ||
} | ||
|
||
past_header = true; | ||
line.clear(); | ||
// remove the # | ||
elems[0].erase(0,1); | ||
unsigned oldnum = std::stol(elems[0]); | ||
|
||
ltrim(elems[1]); | ||
rtrim(elems[1]); | ||
|
||
while( is && elems[1].back() != ';' ) | ||
{ | ||
std::getline( is, line ); | ||
rtrim(line); | ||
ltrim(line); | ||
elems[1].append( line ); | ||
} | ||
|
||
auto it = uniques.emplace( std::make_pair(elems[1], lines.size()) ); | ||
|
||
if( !it.second ) | ||
{ | ||
// didn't insert! | ||
lookup.emplace( oldnum, it.first->second ); | ||
} | ||
else | ||
{ | ||
lookup.emplace( std::make_pair( oldnum, lines.size() ) ); | ||
lines.push_back( elems[1] ); | ||
} | ||
} | ||
|
||
is.close(); | ||
|
||
std::ofstream os(output_file); | ||
|
||
for( auto line : header ) | ||
os << line << std::endl; | ||
|
||
for( int x = 0; x < lines.size(); x++ ) | ||
{ | ||
std::string templine(lines[x]); | ||
unsigned offset = 0; | ||
|
||
std::smatch m; | ||
std::regex e ("#([0-9]+)"); | ||
|
||
os << "#" << x << "="; | ||
while( std::regex_search( templine, m, e, std::regex_constants::match_not_null) ) | ||
{ | ||
if( m.empty() ) break; | ||
unsigned oldval = std::stol(m[1]); | ||
auto newval = lookup.find( oldval ); | ||
os << m.prefix() << "#" << newval->second; | ||
templine = m.suffix(); | ||
} | ||
if( templine.size() > 0 ) | ||
os << templine << std::endl; | ||
} | ||
|
||
for( auto line : footer ) | ||
os << line << std::endl; | ||
|
||
last_file_lines = file_lines; | ||
file_lines = lines.size(); | ||
|
||
os.close(); | ||
input_file = output_file; | ||
} | ||
} |