Added white space trimming program to toolkit. Trimmed whitespace off toolkit. Minor cleanup to extraext.

This commit is contained in:
n-a-c-h
2005-06-02 00:13:26 +00:00
parent 97690f8d3f
commit b13fbb8f41
6 changed files with 127 additions and 19 deletions

View File

@@ -33,6 +33,8 @@ using namespace std;
#include "fileutil.h" #include "fileutil.h"
#define LINE_LENGTH 500
set<string> ignore_include_file; set<string> ignore_include_file;
void handle_file(const char *filename) void handle_file(const char *filename)
@@ -51,10 +53,10 @@ void handle_file(const char *filename)
ifstream file(fname.c_str(), ios::in); ifstream file(fname.c_str(), ios::in);
if (file) if (file)
{ {
char line[500]; char line[LINE_LENGTH];
//Build lists //Build lists
for (size_t i = 0; file.getline(line, 500); i++) for (size_t i = 0; file.getline(line, LINE_LENGTH); i++)
{ {
vector<string> tokens; vector<string> tokens;
char *p = line; char *p = line;
@@ -79,7 +81,7 @@ void handle_file(const char *filename)
for (vector<string>::iterator i = tokens.begin(); i != tokens.end(); i++) for (vector<string>::iterator i = tokens.begin(); i != tokens.end(); i++)
{ {
extsyms.insert(*i); extsyms.insert(*i);
} }
} }
else if (*p && (*p != ';')) else if (*p && (*p != ';'))
{ {
@@ -97,15 +99,15 @@ void handle_file(const char *filename)
} }
} }
} }
} }
} }
} }
else if (ignore_include_file.find(fname) == ignore_include_file.end()) else if (ignore_include_file.find(fname) == ignore_include_file.end())
{ {
cout << "Error opening: " << fname << endl; cout << "Error opening: " << fname << endl;
} }
} while(!included_files.empty()); } while(!included_files.empty());
set_difference(extsyms.begin(), extsyms.end(), used_vars.begin(), used_vars.end(), back_inserter(not_used_extsyms)); set_difference(extsyms.begin(), extsyms.end(), used_vars.begin(), used_vars.end(), back_inserter(not_used_extsyms));
if (not_used_extsyms.size()) if (not_used_extsyms.size())

View File

@@ -35,15 +35,15 @@ bool parse_dir(const char *dir_loc, void (*func)(const char *, struct stat&))
while ((curFile = readdir(curDir))) while ((curFile = readdir(curDir)))
{ {
char *filename = curFile->d_name; char *filename = curFile->d_name;
if (!strcmp(filename, ".") || !strcmp(filename, "..")) if (!strcmp(filename, ".") || !strcmp(filename, ".."))
{ {
continue; continue;
} }
struct stat stat_buffer; struct stat stat_buffer;
if (stat(filename, &stat_buffer)) { continue; } if (stat(filename, &stat_buffer)) { continue; }
//Directory //Directory
if (S_ISDIR(stat_buffer.st_mode)) if (S_ISDIR(stat_buffer.st_mode))
{ {

View File

@@ -0,0 +1,106 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
This program trims unneeded at end of line whitespace.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "fileutil.h"
#define LINE_LENGTH 2048
void handle_file(const char *filename, size_t orig_file_size)
{
bool file_modified = false;
vector<string> file_buffer;
ifstream file(filename, ios::in);
if (file)
{
char line[LINE_LENGTH];
while (file.getline(line, LINE_LENGTH))
{
for (char *p = line+strlen(line)-1; p >= line; p--)
{
if (strchr(" \t\r", *p))
{
*p = 0;
file_modified = true;
}
else
{
break;
}
}
file_buffer.push_back(line);
}
file.close();
}
else
{
cerr << "Could not open " << filename << "." << endl;
}
if (file_modified)
{
ofstream file(filename, ios::out);
if (file)
{
for (vector<string>::iterator i = file_buffer.begin(); i != file_buffer.end(); i++)
{
file.write(i->data(), i->length());
file << "\n";
}
size_t file_size = file.tellp();
file.close();
cout << "Trimmed " << filename << " of " << orig_file_size-file_size << " bytes." << endl;
}
else
{
cerr << filename << " has extra whitespace, but a trimmed copy can't be saved." << endl;
}
}
}
void trim_whitespace(const char *filename, struct stat& stat_buffer)
{
if (is_c_file(filename) ||
is_cpp_file(filename) ||
is_asm_file(filename) ||
is_psr_file(filename))
{
handle_file(filename, stat_buffer.st_size);
}
}
int main()
{
parse_dir(".", trim_whitespace);
return(0);
}

View File

@@ -33,7 +33,7 @@ using namespace std;
void handle_file(const char *filename) void handle_file(const char *filename)
{ {
enum sections { sec_unknown, sec_bss, sec_data, sec_text }; enum sections { sec_unknown, sec_bss, sec_data, sec_text };
ifstream file(filename, ios::in); ifstream file(filename, ios::in);
if (file) if (file)
@@ -45,20 +45,20 @@ void handle_file(const char *filename)
if (!strcasecmp(line, "SECTION .BSS")) { cur_section = sec_bss; } if (!strcasecmp(line, "SECTION .BSS")) { cur_section = sec_bss; }
if (!strcasecmp(line, "SECTION .DATA")) { cur_section = sec_data; } if (!strcasecmp(line, "SECTION .DATA")) { cur_section = sec_data; }
if (!strcasecmp(line, "SECTION .text")) { cur_section = sec_text; } if (!strcasecmp(line, "SECTION .text")) { cur_section = sec_text; }
if ((cur_section != sec_bss) && if ((cur_section != sec_bss) &&
(strstr(line, " resd ") || strstr(line, " resw ") || strstr(line, " resb ") || (strstr(line, " resd ") || strstr(line, " resw ") || strstr(line, " resb ") ||
(strstr(line, ",resd ") || strstr(line, ",resw ") || strstr(line, ",resb ")) )) (strstr(line, ",resd ") || strstr(line, ",resw ") || strstr(line, ",resb ")) ))
{ {
cout << filename << ": line " << i << ": Error, resx in non BSS section. \"" << line << "\"" << endl; cout << filename << ": line " << i << ": Error, resx in non BSS section. \"" << line << "\"" << endl;
} }
if ((cur_section != sec_data) && if ((cur_section != sec_data) &&
(strstr(line, " dd ") || strstr(line, " dw ") || strstr(line, " db ") || (strstr(line, " dd ") || strstr(line, " dw ") || strstr(line, " db ") ||
(strstr(line, ",dd ") || strstr(line, ",dw ") || strstr(line, ",db ")) )) (strstr(line, ",dd ") || strstr(line, ",dw ") || strstr(line, ",db ")) ))
{ {
cout << filename << ": line " << i << ": Error, dx in non DATA section. \"" << line << "\"" << endl; cout << filename << ": line " << i << ": Error, dx in non DATA section. \"" << line << "\"" << endl;
} }
} }
} }
else else

View File

@@ -45,7 +45,7 @@ void size_tally(const char *filename, struct stat& stat_buffer)
int main() int main()
{ {
parse_dir(".", size_tally); parse_dir(".", size_tally);
cout << "Total C files use " << c_count << " bytes.\n" cout << "Total C files use " << c_count << " bytes.\n"
<< "Total C++ files use " << cpp_count << " bytes.\n" << "Total C++ files use " << cpp_count << " bytes.\n"
<< "Total Assembly files use " << asm_count << " bytes.\n" << "Total Assembly files use " << asm_count << " bytes.\n"

View File

@@ -28,7 +28,7 @@ void Tokenize(const string& str, vector<string>& tokens, const string& delimiter
{ {
//Skip delimiters at beginning. //Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0); string::size_type lastPos = str.find_first_not_of(delimiters, 0);
//Find first "non-delimiter". //Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos); string::size_type pos = str.find_first_of(delimiters, lastPos);
@@ -36,10 +36,10 @@ void Tokenize(const string& str, vector<string>& tokens, const string& delimiter
{ {
//Found a token, add it to the vector. //Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos)); tokens.push_back(str.substr(lastPos, pos - lastPos));
//Skip delimiters. Note the "not_of" //Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos); lastPos = str.find_first_not_of(delimiters, pos);
//Find next "non-delimiter" //Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos); pos = str.find_first_of(delimiters, lastPos);
} }