Created
August 2, 2018 00:21
-
-
Save blooser/ac32382d1e57cce50ff50b455234a391 to your computer and use it in GitHub Desktop.
Words counter
This file contains hidden or 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
#include <iostream> | |
#include <cstring> | |
#include <iomanip> | |
#include "wordscounter.h" | |
#define REP(i, n, v) for(int i=v; i<n; i++) | |
#define FOREACH(it, v) for(auto it = v.begin(); it != v.end(); it++) | |
void CheckIfUserAskForHelp(int argc, char ** argv); | |
void Options(int argc, char ** argv, WordsCounter&); | |
int main(int argc, char *argv[]){ | |
std::ios_base::sync_with_stdio(false); | |
if(argc < 2) std::exit(EXIT_FAILURE); | |
CheckIfUserAskForHelp(argc, argv); | |
WordsCounter wc; | |
Options(argc, argv, wc); | |
REP(i, argc, 1){ | |
if(std::strlen(argv[i]) == 0) continue; | |
std::ifstream file; | |
file.open(argv[i], std::ios_base::binary); | |
if(!file.is_open()){ | |
std::cerr<<"Can't read "<<argv[i]<<" file!\n"; | |
continue; | |
} | |
std::stringstream fileSummary; | |
fileSummary<<wc.countWords(file); | |
file.close(); | |
fileSummary<<'\t'<<std::string(argv[i]); | |
wc.result.push_back(fileSummary.str()); | |
} | |
FOREACH(it, wc.result) | |
std::cout<<*it<<"\n"; | |
if(wc.result.size() > 1) std::cout<<wc.totalSummary(); | |
} | |
void CheckIfUserAskForHelp(int argc, char ** arguments){ | |
REP(i, argc, 1){ | |
if(std::strcmp(arguments[i], "--help") == 0 || | |
std::strcmp(arguments[i], "-h") == 0){ | |
std::cout <<"\e[1m-m, --chars\n\t\e[0mprint the character counts" | |
<<"\n\e[1m-l, --lines\n\t\e[0mprint the new lines counts" | |
<<"\n\e[1m-c, --bytes\n\t\e[0mprint the bytes counts\n"; | |
std::exit(EXIT_SUCCESS); | |
} | |
} | |
} | |
void Options(int argc, char ** arguments, WordsCounter & wc){ | |
REP(i, argc, 1){ | |
if(std::strcmp(arguments[i], "--chars") == 0 || | |
std::strcmp(arguments[i], "-m") == 0){ | |
wc.readChars = true; | |
arguments[i][0] = '\0'; | |
} | |
if(std::strcmp(arguments[i], "--lines") == 0 || | |
std::strcmp(arguments[i], "-l") == 0){ | |
wc.readNewLines = true; | |
arguments[i][0] = '\0'; | |
} | |
if(std::strcmp(arguments[i], "-c") == 0 || | |
std::strcmp(arguments[i], "--bytes") == 0){ | |
wc.readBytes = true; | |
arguments[i][0] = '\0'; | |
} | |
} | |
} |
This file contains hidden or 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
#include "wordscounter.h" | |
std::string WordsCounter::getFileContent(std::ifstream & file){ | |
std::string data; | |
data.assign(std::istreambuf_iterator<char>(file), | |
std::istreambuf_iterator<char>()); | |
return data; | |
} | |
std::string WordsCounter::countWords(std::ifstream & file){ | |
auto content = getFileContent(file); | |
std::stringstream sstream(content); | |
numberOfWords = std::distance(std::istream_iterator<std::string>(sstream), | |
std::istream_iterator<std::string>()); | |
allWords += numberOfWords; | |
std::stringstream summary; | |
summary <<numberOfWords; | |
if(readChars){ | |
numberOfChars = std::count_if(content.begin(), content.end(), [=](char c){ | |
return std::isalpha(c); | |
}); | |
allChars += numberOfChars; | |
summary << std::setw(4)<<numberOfChars; | |
} | |
if(readNewLines){ | |
file.clear(); | |
file.seekg(0, std::ios_base::beg); | |
numberOfNewLines = std::count(std::istreambuf_iterator<char>(file), | |
std::istreambuf_iterator<char>(), | |
'\n'); | |
allNewLines += numberOfNewLines; | |
summary<<std::setw(4)<<numberOfNewLines; | |
} | |
if(readBytes){ | |
file.clear(); | |
file.seekg(0, std::ios_base::end); | |
numberOfBytes = file.tellg(); | |
allBytes += numberOfBytes; | |
summary<<std::setw(4)<<numberOfBytes; | |
} | |
return summary.str(); | |
} | |
const std::string WordsCounter::totalSummary(){ | |
std::stringstream summaryTotal; | |
summaryTotal<<std::setw(4)<<allWords; | |
if(readChars) summaryTotal<<std::setw(4)<<allChars; | |
if(readNewLines) summaryTotal<<std::setw(4)<<allNewLines; | |
if(readBytes) summaryTotal<<std::setw(4)<<allBytes; | |
summaryTotal<<"\ttotal\n"; | |
return summaryTotal.str(); | |
} |
This file contains hidden or 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
#ifndef WORDSCOUNTER_H_ | |
#define WORDSCOUNTER_H_ | |
#include <fstream> | |
#include <iterator> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <iomanip> | |
#include <sstream> | |
typedef struct{ | |
int allWords = 0; | |
int allChars = 0; | |
int allBytes = 0; | |
int allNewLines = 0; | |
bool readChars = false; | |
bool readBytes = false; | |
bool readNewLines = false; | |
int numberOfNewLines, numberOfWords, numberOfChars, numberOfBytes; | |
std::vector<std::string> result; | |
std::string getFileContent(std::ifstream&); | |
std::string countWords(std::ifstream&); | |
const std::string totalSummary(); | |
} WordsCounter; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment