Skip to content

Instantly share code, notes, and snippets.

@kauevestena
Created July 2, 2016 06:42
Show Gist options
  • Save kauevestena/c192be1fab7c68b467ef0e313d47f473 to your computer and use it in GitHub Desktop.
Save kauevestena/c192be1fab7c68b467ef0e313d47f473 to your computer and use it in GitHub Desktop.
programa para a separação de mensagens NMEA
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <sstream>
#include <stdio.h>
#include<time.h>
#include<ctime>
#include <fstream>
#include <ostream>
#include<vector>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
using namespace std;
string destfolder = "./dest/";
string extension = ".csv";
string separator = ",";
string writeLine(vector<string> brString,string sep)
{
string res="";
for (unsigned int i = 0;i < brString.size();i++)
{
res += brString.at(i);
if (i != brString.size()-1)
{
res+=sep;
}
}
return res;
}
struct allLogs
{
//to contain all the messages
vector<vector<vector<string>>> logs;
//to contain all the valid separators
vector<string> allMessages;
//to contain all the files names
vector<string> allFiles;
//the destination path
string destpath;
// //the list of needed files for each original log
//the full path names
vector<vector<string>> fullPathnames;
//to create the list of needed files for each original file
vector<vector<string>> logsFiles();
//to set the default destination folder
void setDestpath(string path);
//counters
vector<int> counters();
//the general report
void report();
//remove the extensions and the paths of the folders
void removeExtPath(string base);
//create the folders
void createFolders();
//to create all the files
void createfiles2(string ext,string fieldSep);
};
void allLogs::createfiles2(string ext,string fieldSep)
{
ofstream files;
string filename;
vector<string> temp,temp2;
//logsFiles();
int i = 0;
cout<<endl << "escrevendo arquivos do original "<<allFiles.at(i)<<endl<<endl;
for (unsigned int j = 0;j < allMessages.size();j++)
{
filename = allFiles.at(i)+"/"+allMessages.at(j)+ext;
temp.push_back(filename);
files.open(filename);
for (unsigned int j2 = 0;j2 < logs[i].size();j2++)
{
temp2 = logs[i][j2];
if (strcmp(allMessages[j].c_str(),temp2[0].c_str()) == 0)
{
files << writeLine(logs[i][j2],separator) <<endl;
}
if (j2%100==0 && j2 != 0)
{
cout <<j2<<" linhas processadas"<<endl;
}
}
cout<<endl<<"gerado "<<filename<<endl<<endl;
files.close();
}
fullPathnames.push_back(temp);
temp.clear();
}
void allLogs::setDestpath(string base)
{
destpath = base;
}
void allLogs::removeExtPath(string base)
{
//funtions to modify the filenames to the required format
int pos;
for(int i = 0; i < allFiles.size(); i++)
{
if(allFiles.at(i).rfind(".") != -1)
{
pos = allFiles.at(i).rfind(".");
allFiles.at(i).replace(pos,allFiles.at(i).size()+1-pos,"");
}
if(allFiles.at(i).rfind("/") != -1)
{
pos = allFiles.at(i).rfind("/");
allFiles.at(i).replace(0,base.size()-1,"");
}
allFiles.at(i) = base+allFiles.at(i);
}
}
void allLogs::createFolders()
{
for (int i = 0; i < allFiles.size();i++)
{
CreateDirectory(allFiles[i].c_str(),NULL);
}
}
string dateTimeString()
{
time_t rawtime;
time (&rawtime);;
return ctime(&rawtime);
}
vector<int> allLogs::counters()
{
vector<int> temp(allMessages.size(),0);
for (int i = 0;i<logs.size();i++)
{
for(int j = 0; j < logs[i].size();j++)
{
for (int i2 = 0; i2 < allMessages.size(); i2++)
{
if(logs[i][j].at(0) == allMessages.at(i2))
{
temp.at(i2)++;
}
}
}
}
return temp;
}
void allLogs::report ()
{
ofstream general("report.txt");
general << "RELATÓRIO DO NMEA READ 0.1"<<endl<<endl;
general << dateTimeString() << endl;
general << "total de "<<allFiles.size()<<" arquivos lidos"<<endl;
general << "com um total de "<<allMessages.size()<<" diferentes mensagens NMEA"<<endl;
for (int i = 0; i < allMessages.size() ; i++)
{
general <<" "<< allMessages.at(i)<<" : "<<counters().at(i)<<" vezes"<<endl;
}
general<<endl << "Lista de Arquivos Lidos:"<<endl<<endl;
for (int j = 0; j < allFiles.size() ; j++)
{
general << allFiles.at(j)<<endl;
}
general.close();
}
string subsAfLChar(string input,string character)
{
//return the string after the last occur. of the specified character
return input.substr(input.rfind(character)+1,string::npos);
}
string subsAfChar(string input,string character)
{
//return the string after the first occur. of the specified character
return input.substr(input.find(character)+1,string::npos);
}
vector<string> find_files(string path,string ext,bool show=false){
bool res;
vector<string> names;
WIN32_FIND_DATA FindFileData;
string img = path+"*"+ext;
HANDLE hFind = FindFirstFile(img.c_str(), &FindFileData);
if(hFind == INVALID_HANDLE_VALUE){
res = false;
}
else do{
if(show){cout<<path+FindFileData.cFileName<<endl;}
names.push_back(path+FindFileData.cFileName);}
while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return names;
}
string changeChar2(string input,string charO,string charD)
{
//change the first occur of a character with another character
return input.replace(input.find(charO),1,charD);
}
vector<string> lineSep(string line,string sep,bool noEmpty=0)
{
//a function to break a line of obs, in a vector of strings
//the third argument skip empty the fields
vector<string> res;
//first, take off the asterisk
int astPos = line.rfind("*");
if (astPos!=-1)
{
line.replace(astPos,1,sep);
}
//the position of the separator
int cmP = line.find(sep);
//the position after the last occ. of comma
bool next = true;
line.push_back(sep.at(0));
while (cmP != -1)
{
if (next)
{res.push_back(line.substr(0,cmP));}
next = true;
line.replace(0,cmP+1,"");
cmP = line.find(sep);
if (noEmpty && cmP==0)
{
next = false;
}
}
return res;
}
bool testRepeated (vector<string> input,string target)
{
//a function to verify if a vector of strings contain a target string
bool contains = false;
for(int i=0;i < input.size();i++)
{
if(input.at(i)==target)
{
contains = true;
break;
}
}
return contains;
}
allLogs readAll(vector<string> files,string outPath=destfolder,string sep=",")
{
//yep, a huge container to contain all the organized messages
vector<vector<vector<string>>> all;
//a temporary ~vector of vectors~
vector<vector<string>> tempV;
allLogs res;
//counters for the messages, and the extras
vector<int> counters;
vector<string> messags;
bool repeated = false;
counters.push_back(0);
int j = 0;
string specChar = "$";
for(int i=0;i < files.size();i++)
{
ifstream infile(files.at(i));
string line,temp;
vector<string> lineBr;
while (std::getline(infile,line))
{//the loop that go between a file
if (line.find(sep) != -1)
//and all the other characters that any line must have
{
lineBr = lineSep(line,sep);
temp=lineBr.at(0);
tempV.push_back(lineBr);
}
if (j != 0)
{
repeated = testRepeated(messags,temp);
}
if(!repeated && temp[0]==specChar[0])
{
messags.push_back(temp);
cout<< temp << endl;
}
j++;
}
all.push_back(tempV);
tempV.clear();
cout << "processado "<<files.at(i)<<endl;
infile.close();
}
tempV.clear();
res.logs = all;
res.allFiles = files;
res.allMessages = messags;
//undoing some messs
all.clear();files.clear();messags.clear();
return res;
}
int main()
{
//if it does not exist, will create it
CreateDirectory(destfolder.c_str(),NULL);
cout <<" buscando todos os arquivos de entrada"<<endl;
vector<string> nomes= find_files("./src/",".txt");
vector<string> temp;
cout<<endl<<" feito! "<<endl<<endl;
for (unsigned int i = 0;i<nomes.size();i++)
{
temp.push_back(nomes.at(i));
allLogs theLogs = readAll(temp);
//generate the report
theLogs.report();
//remove the extension and path from filenames
theLogs.removeExtPath(destfolder);
//create a folder for each filename
theLogs.createFolders();
//set the destination path
theLogs.setDestpath(destfolder);
//all the names of the files
//create all files
theLogs.createfiles2(extension,separator);
temp.clear();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment