Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <vector>
- #include <dirent.h>
- #include <fstream>
- #include <cstring>
- #include <sys/stat.h>
- #include <algorithm>
- #include <string.h>
- #include <map>
- #include <pqxx/pqxx>
- //#include <windows.h>
- #include <sys/stat.h> // stat
- #include <errno.h> // errno, ENOENT, EEXIST
- #if defined(_WIN32)
- #include <direct.h> // _mkdir
- #endif
- #include "operacoes.hpp" //BIBLIOTECA CRIADA
- //#include "dbOps.hpp"
- #define limite_cornea_estatica 0.05
- using namespace std;
- using namespace pqxx;
- //DBOps *dbop = new DBOps();
- typedef struct{
- float deformationAmplitude[140];
- float applanationLength[140];
- float cornealVelocity[140];
- float deflectionLength[140];
- float concavePower[140];
- float deflectionArea[140];
- char nomeArquivo[1000];
- bool ceratocone;
- int idade;
- float PIO;
- } patient;
- typedef struct{
- int idade;
- float PIO;
- } exam;
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////FUNÇÕES PRINCIPAIS///////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void formarPacientes(vector<patient>* pat, map<string, exam> exams, string pasta){
- vector<string> arquivos;
- int i = 0;
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir ((pasta +"/Diagrams "+ pasta).c_str())) != NULL) {
- while ((ent = readdir (dir)) != NULL) {
- if(strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) {
- string str = ent->d_name;
- arquivos.push_back(str);
- }
- }
- closedir (dir);
- } else {
- perror ("");
- }
- int size = (int) arquivos.size();
- for(i = 0; i < size; i++){
- ifstream arquivo ((pasta +"/Diagrams "+ pasta +"/"+ arquivos[i]).c_str());
- int lines = 0;
- string line;
- while (getline(arquivo, line))
- ++lines;
- patient aux;
- aux.nomeArquivo[0] = '\0';
- strcpy(aux.nomeArquivo, (arquivos[i]).c_str());
- FILE *file;
- file = fopen((pasta +"/Diagrams "+ pasta +"/"+ arquivos[i]).c_str(), "r");
- char str[1000], lixo[1000];
- int linha, inseridos;
- inseridos = 0;
- for(linha = 0; linha < lines; linha++){
- fscanf(file, "%[^;];", str);
- string strS = str;
- if(strcmp(str, "Deformation Amplitude [mm]\0") == 0){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.deformationAmplitude[k]);
- ++inseridos;
- //cout << "DefAmp ";
- }
- else if(strcmp(str, "Applanation Length [mm]\0") == 0){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.applanationLength[k]);
- ++inseridos;
- //cout << "AppLen ";
- }
- else if(strcmp(str, "Deflection Length [mm]\0") == 0){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.deflectionLength[k]);
- ++inseridos;
- //cout << "DefLen ";
- }
- else if(strcmp(str, "Corneal Velocity [m/s]\0") == 0){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.cornealVelocity[k]);
- ++inseridos;
- //cout << "CorVel ";
- }
- else if(strS.substr(0, 15) == "Deflection Area"){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.deflectionArea[k]);
- ++inseridos;
- //cout << "DefArea ";
- }
- else if(strcmp(str, "ConcavePower [mm]\0") == 0){
- for(int k = 0; k < 140; k++)
- fscanf(file, "%f;", &aux.concavePower[k]);
- ++inseridos;
- //cout << "ConPow ";
- }
- else
- fscanf(file, "%[^\n]", lixo);
- fscanf(file, "\n");
- }
- fclose(file);
- char strA[100], strB[100], strC[100], strD[100], strLeitura[1000];
- strcpy(strLeitura, arquivos[i].c_str());
- sscanf(strLeitura, "%[^_]_%[^_]_%[^_]_%[^_]_%[^_]_Diagrams.csv", strA, strB, strC, strD, lixo);
- string strng(strA); strng += "_";
- strng += strB; strng += "_";
- strng += strC; strng += "_";
- strng += strD; strng += "_Diagrams.csv";
- int idad = exams[strng].idade;
- //cout << inseridos << endl;
- if(inseridos == 6 && idad != 0){
- if(pasta == "Ceratocone")
- aux.ceratocone = true;
- else
- aux.ceratocone = false;
- aux.idade = exams[strng].idade;
- aux.PIO = exams[strng].PIO;
- pat->push_back(aux);
- }
- else if(inseridos == 6){
- strng = "";
- strng += strA; strng += "_";
- strng += strB; strng += "_";
- if(strcmp(strC, "Left") == 0) {
- strcpy(strC, "Right");
- cout << "MUDOUR ";
- }
- else {
- strcpy(strC, "Left");
- cout << "MUDOUL ";
- }
- strng += strC; strng += "_";
- strng += strD ; strng += "_Diagrams.csv";
- idad = exams[strng].idade;
- if(idad != 0) {
- if (pasta == "Ceratocone")
- aux.ceratocone = true;
- else
- aux.ceratocone = false;
- aux.idade = exams[strng].idade;
- aux.PIO = exams[strng].PIO;
- pat->push_back(aux);
- }
- else {
- cout << strng << " - " << idad << " - " << exams[strng].PIO << endl;
- FILE *inf;
- inf = fopen("Arquivos sem informacoes.txt", "a+");
- fprintf(inf, "%s\n", arquivos[i].c_str());
- fclose(inf);
- }
- }
- else {
- cout << "INVALIDO(" << inseridos << ") - " << strng << endl;
- FILE *inv;
- inv = fopen(("Arquivos invalidos - "+ pasta +".txt").c_str(), "a+");
- fprintf(inv, "%s\n", arquivos[i].c_str());
- fclose(inv);
- }
- }
- }
- int idadeAtual(int nascDia, int nascMes, int nascAno, int examDia, int examMes, int examAno){
- int idade = examAno - nascAno - 1;
- if(examMes > nascMes)
- idade++;
- else if(examMes == nascMes && examDia >= nascDia)
- idade++;
- return idade;
- }
- void preparaMapaExames(map<string, exam> &exams){
- FILE *arquivo;
- arquivo = fopen("Exams.csv", "r");
- char lixo, line[1000], line2[1000], ultimoNome[100], primeiroNome[100], exameData[100], exameHora[100], exameOlho[100];
- int nascDia = 0, nascMes = 0, nascAno = 0, exameDia = 0, exameMes = 0, exameAno = 0, exameHrs = 0, exameMin = 0, exameSeg = 0;
- fscanf(arquivo, "%[^\n]\n", line);
- while(fscanf(arquivo, " %[^;]%c", ultimoNome, &lixo) != EOF){
- exam aux;
- aux.idade = 0;
- aux.PIO = 0;
- for(int i = 0; i < 5; i++){
- line[0] = '\0';
- if(i != 4)
- fscanf(arquivo, "%[^;]%c", line, &lixo);
- else
- fscanf(arquivo, "%[^\n]%c", line, &lixo);
- if (i == 0) {
- strcpy(primeiroNome, line);
- } else if (i == 1) {
- sscanf(line, "%02d,%02d.%04d", &nascDia, &nascMes, &nascAno);
- } else if (i == 2) {
- sscanf(line, "%02d,%02d.%04d", &exameDia, &exameMes, &exameAno);
- sprintf(exameData, "%04d%02d%02d", exameAno, exameMes, exameDia);
- } else if (i == 3) {
- strcpy(exameOlho, line);
- } else if (i == 4) {
- string str = line;
- replace(str.begin(), str.end(), ',', '.');
- aux.PIO = (float) atof(str.c_str());
- aux.idade = idadeAtual(nascDia, nascMes, nascAno, exameDia, exameMes, exameAno);
- }
- }
- fscanf(arquivo, " ");
- string nomeArquivo(ultimoNome); nomeArquivo += "_";
- nomeArquivo += primeiroNome; nomeArquivo += "_";
- nomeArquivo += exameOlho; nomeArquivo += "_";
- nomeArquivo += exameData; nomeArquivo += "_Diagrams.csv";
- //cout << "/" << nomeArquivo << endl;
- exams[nomeArquivo] = aux;
- //printf("%d %.1f - %s\n", exams[nomeArquivo].idade, exams[nomeArquivo].PIO, nomeArquivo);
- line[0] = '\0'; line2[0] = '\0'; ultimoNome[0] = '\0'; primeiroNome[0] = '\0'; exameOlho[0] = '\0'; exameData[0] = '\0'; exameHora[0] = '\0';
- }
- fclose(arquivo);
- }
- inline bool fileExists (const std::string& name) {
- struct stat buffer;
- return (stat (name.c_str(), &buffer) == 0);
- }
- bool dirExists(const char* pzPath){
- if ( pzPath == NULL) return false;
- DIR *pDir;
- bool bExists = false;
- pDir = opendir (pzPath);
- if (pDir != NULL)
- {
- bExists = true;
- (void) closedir (pDir);
- }
- return bExists;
- }
- void imprimeNoCSV(bool ceratocone, int idade, float PIO, float DAm_Area,
- float DAm_AlturaMaxima,
- float ALe_Pico1, float ALe_Pico2, int ALe_DistanciaPicos,
- float CVe_AlturaPico1, float CVe_AlturaPico2, float CVe_Area,
- float CVe_InclinacaoReta,
- float DLe_Area, float DLe_AlturaMaxima,
- float CPo_Area, float CPo_AlturaMedia,
- float DAr_Area, float DAr_AlturaMaxima){
- bool result = fileExists("Results patients.csv");
- if(result) {
- ifstream arquivo("Results patients.csv");
- int lines = 0;
- string line;
- while (getline(arquivo, line))
- ++lines;
- if(lines == 0)
- result = false;
- }
- FILE* arquivo;
- arquivo = fopen("Results patients.csv", "a+");
- if(!result) {
- fprintf(arquivo, ""
- "Ceratocone;"
- "Idade;"
- "PIO;"
- "DAm_Area;"
- "DAm_AlturaMaxima;"
- "ALe_Pico1;"
- "ALe_Pico2;"
- "ALe_DistanciaPicos;"
- "CVe_AlturaPico1;"
- "CVe_AlturaPico2;"
- "CVe_Area;"
- "CVe_InclinacaoReta;"
- "DLe_Area;"
- "DLe_AlturaMaxima;"
- "CPo_Area;"
- "CPo_AlturaMedia;"
- "DAr_Area;"
- "DAr_AlturaMaxima\n");
- }
- fprintf(arquivo, "%c;"
- "%d;%.1f;"
- "%f;%f;"
- "%f;%f;%d;"
- "%f;%f;%f;%f;"
- "%f;%f;"
- "%f;%f;"
- "%f;%f\n",
- ceratocone ? 't' : 'f',
- idade, PIO,
- DAm_Area, DAm_AlturaMaxima,
- ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
- CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta,
- DLe_Area, DLe_AlturaMaxima,
- CPo_Area, CPo_AlturaMedia,
- DAr_Area, DAr_AlturaMaxima);
- fclose(arquivo);
- }
- void processaPaciente(connection *C, patient pat){
- //DECLARANDO OBJETO DE OPERAÇÕES PARA USAR SEUS MÉTODOS
- Operacoes *op = new Operacoes();
- float DAm_Area, DAm_AlturaMaxima,
- ALe_Pico1, ALe_Pico2,
- CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta, CVe_InclinacaoRL,
- DLe_Area, DLe_AlturaMaxima,
- CPo_Area, CPo_AlturaMedia,
- DAr_Area, DAr_AlturaMaxima;
- int ALe_DistanciaPicos;
- //MÉTODOS DA NOVA BIBLIOTECA SENDO USADOS
- op->deformationAmplitude(pat.deformationAmplitude, &DAm_Area, &DAm_AlturaMaxima);
- op->applanationLength(pat.applanationLength, &ALe_Pico1, &ALe_Pico2, &ALe_DistanciaPicos);
- op->cornealVelocity(pat.cornealVelocity, &CVe_AlturaPico1, &CVe_AlturaPico2, &CVe_Area, &CVe_InclinacaoReta, &CVe_InclinacaoRL);
- op->deflectionLenght(pat.deflectionLength, &DLe_Area, &DLe_AlturaMaxima);
- op->concavePower(pat.concavePower, &CPo_Area, &CPo_AlturaMedia);
- op->deflectionArea(pat.deflectionArea, &DAr_Area, &DAr_AlturaMaxima);
- /*dbop->inserirDados(&(*C), pat.ceratocone, DAm_Area, DAm_AlturaMaxima,
- ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
- CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta, CVe_InclinacaoRL,
- DLe_Area, DLe_AlturaMaxima,
- CPo_Area, CPo_AlturaMedia,
- DAr_Area, DAr_AlturaMaxima);
- */
- imprimeNoCSV(pat.ceratocone, pat.idade, pat.PIO, DAm_Area, DAm_AlturaMaxima,
- ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
- CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta,
- DLe_Area, DLe_AlturaMaxima,
- CPo_Area, CPo_AlturaMedia,
- DAr_Area, DAr_AlturaMaxima);
- string nomeArq = pat.nomeArquivo;
- string pasta = pat.ceratocone ? "Ceratocone" : "Normal";
- int res = rename((pasta +"/Diagrams "+ pasta +"/" + nomeArq).c_str(),
- (pasta +"/Diagrams "+ pasta +" Valids/" + nomeArq).c_str());
- if (res == 1)
- cout << "Error on moving file " + nomeArq << endl;
- }
- bool isDirExist(const std::string& path)
- {
- #if defined(_WIN32)
- struct _stat info;
- if (_stat(path.c_str(), &info) != 0)
- {
- return false;
- }
- return (info.st_mode & _S_IFDIR) != 0;
- #else
- struct stat info;
- if (stat(path.c_str(), &info) != 0)
- {
- return false;
- }
- return (info.st_mode & S_IFDIR) != 0;
- #endif
- }
- bool makePath(const std::string& path){
- #if defined(_WIN32)
- int ret = _mkdir(path.c_str());
- #else
- mode_t mode = 0755;
- int ret = mkdir(path.c_str(), mode);
- #endif
- if (ret == 0)
- return true;
- switch (errno)
- {
- case ENOENT:
- // parent didn't exist, try to create it
- {
- int pos = path.find_last_of('/');
- if (pos == std::string::npos)
- #if defined(_WIN32)
- pos = path.find_last_of('\\');
- if (pos == std::string::npos)
- #endif
- return false;
- if (!makePath( path.substr(0, pos) ))
- return false;
- }
- // now, try to create again
- #if defined(_WIN32)
- return 0 == _mkdir(path.c_str());
- #else
- return 0 == mkdir(path.c_str(), mode);
- #endif
- case EEXIST:
- // done!
- return isDirExist(path);
- default:
- return false;
- }
- }
- int main(){
- map<string, exam> exams;
- preparaMapaExames(exams);
- //Criar diretório para mover os arquivos válidos, caso não exista:
- bool validsFolderExists = dirExists("Ceratocone/Diagrams Ceratocone Valids");
- if(!validsFolderExists) {
- makePath("Ceratocone/Diagrams Ceratocone Valids");
- }
- validsFolderExists = dirExists("Normal/Diagrams Normal Valids");
- if(!validsFolderExists) {
- makePath("Normal/Diagrams Normal Valids");
- }
- vector<patient> patients;
- formarPacientes(&patients, exams, "Ceratocone");
- formarPacientes(&patients, exams, "Normal");
- try{
- connection C("dbname=testdb user=postgres password=123456 hostaddr=127.0.0.1 port=5432");
- if (C.is_open()) {
- cout << "Opened database successfully: " << C.dbname() << endl;
- } else {
- cout << "Can't open database" << endl;
- return 1;
- }
- //dbop->deletaTabela(&C);
- //dbop->criaTabela(&C);
- for(int i = 0; i < patients.size(); i++)
- processaPaciente(&C, patients[i]);
- C.disconnect ();
- }catch (const std::exception &e){
- cerr << e.what() << std::endl;
- return 1;
- }
- cout << "Processo concluido, aperte enter para finalizar." << endl;
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement