Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <vector>
  6. #include <dirent.h>
  7. #include <fstream>
  8. #include <cstring>
  9. #include <sys/stat.h>
  10. #include <algorithm>
  11. #include <string.h>
  12. #include <map>
  13. #include <pqxx/pqxx>
  14. //#include <windows.h>
  15. #include <sys/stat.h> // stat
  16. #include <errno.h>    // errno, ENOENT, EEXIST
  17. #if defined(_WIN32)
  18. #include <direct.h>   // _mkdir
  19. #endif
  20. #include "operacoes.hpp" //BIBLIOTECA CRIADA
  21. //#include "dbOps.hpp"
  22.  
  23. #define limite_cornea_estatica 0.05
  24.  
  25. using namespace std;
  26. using namespace pqxx;
  27.  
  28. //DBOps *dbop = new DBOps();
  29.  
  30. typedef struct{
  31.     float deformationAmplitude[140];
  32.     float applanationLength[140];
  33.     float cornealVelocity[140];
  34.     float deflectionLength[140];
  35.     float concavePower[140];
  36.     float deflectionArea[140];
  37.     char nomeArquivo[1000];
  38.     bool ceratocone;
  39.     int idade;
  40.     float PIO;
  41. } patient;
  42.  
  43. typedef struct{
  44.     int idade;
  45.     float PIO;
  46. } exam;
  47.  
  48.  
  49. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /////////////////////////////////////////////////FUNÇÕES PRINCIPAIS///////////////////////////////////////////////////////////////////////////
  51. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. void formarPacientes(vector<patient>* pat, map<string, exam> exams, string pasta){
  53.     vector<string> arquivos;
  54.     int i = 0;
  55.  
  56.     DIR *dir;
  57.     struct dirent *ent;
  58.     if ((dir = opendir ((pasta +"/Diagrams "+ pasta).c_str())) != NULL) {
  59.         while ((ent = readdir (dir)) != NULL) {
  60.             if(strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) {
  61.                 string str = ent->d_name;
  62.                 arquivos.push_back(str);
  63.             }
  64.         }
  65.         closedir (dir);
  66.     } else {
  67.         perror ("");
  68.     }
  69.  
  70.     int size = (int) arquivos.size();
  71.  
  72.     for(i = 0; i < size; i++){
  73.         ifstream arquivo ((pasta +"/Diagrams "+ pasta +"/"+ arquivos[i]).c_str());
  74.         int lines = 0;
  75.         string line;
  76.         while (getline(arquivo, line))
  77.             ++lines;
  78.  
  79.             patient aux;
  80.             aux.nomeArquivo[0] = '\0';
  81.             strcpy(aux.nomeArquivo, (arquivos[i]).c_str());
  82.  
  83.             FILE *file;
  84.             file = fopen((pasta +"/Diagrams "+ pasta +"/"+ arquivos[i]).c_str(), "r");
  85.             char str[1000], lixo[1000];
  86.             int linha, inseridos;
  87.             inseridos = 0;
  88.             for(linha = 0; linha < lines; linha++){
  89.                 fscanf(file, "%[^;];", str);
  90.  
  91.                 string strS = str;
  92.  
  93.                 if(strcmp(str, "Deformation Amplitude [mm]\0") == 0){
  94.                     for(int k = 0; k < 140; k++)
  95.                         fscanf(file, "%f;", &aux.deformationAmplitude[k]);
  96.                     ++inseridos;
  97.                     //cout << "DefAmp ";
  98.                 }
  99.                 else if(strcmp(str, "Applanation Length [mm]\0") == 0){
  100.                     for(int k = 0; k < 140; k++)
  101.                         fscanf(file, "%f;", &aux.applanationLength[k]);
  102.                     ++inseridos;
  103.                     //cout << "AppLen ";
  104.                 }
  105.                 else if(strcmp(str, "Deflection Length [mm]\0") == 0){
  106.                     for(int k = 0; k < 140; k++)
  107.                         fscanf(file, "%f;", &aux.deflectionLength[k]);
  108.                     ++inseridos;
  109.                     //cout << "DefLen ";
  110.                 }
  111.                 else if(strcmp(str, "Corneal Velocity [m/s]\0") == 0){
  112.                     for(int k = 0; k < 140; k++)
  113.                         fscanf(file, "%f;", &aux.cornealVelocity[k]);
  114.                     ++inseridos;
  115.                     //cout << "CorVel ";
  116.                 }
  117.                 else if(strS.substr(0, 15) == "Deflection Area"){
  118.                     for(int k = 0; k < 140; k++)
  119.                         fscanf(file, "%f;", &aux.deflectionArea[k]);
  120.                     ++inseridos;
  121.                     //cout << "DefArea ";
  122.                 }
  123.                 else if(strcmp(str, "ConcavePower [mm]\0") == 0){
  124.                     for(int k = 0; k < 140; k++)
  125.                         fscanf(file, "%f;", &aux.concavePower[k]);
  126.                     ++inseridos;
  127.                     //cout << "ConPow ";
  128.                 }
  129.                 else
  130.                     fscanf(file, "%[^\n]", lixo);
  131.                 fscanf(file, "\n");
  132.             }
  133.             fclose(file);
  134.  
  135.             char strA[100], strB[100], strC[100], strD[100], strLeitura[1000];
  136.             strcpy(strLeitura, arquivos[i].c_str());
  137.             sscanf(strLeitura, "%[^_]_%[^_]_%[^_]_%[^_]_%[^_]_Diagrams.csv", strA, strB, strC, strD, lixo);
  138.             string strng(strA); strng += "_";
  139.             strng += strB; strng += "_";
  140.             strng += strC; strng += "_";
  141.             strng += strD; strng += "_Diagrams.csv";
  142.  
  143.             int idad = exams[strng].idade;
  144.             //cout << inseridos << endl;
  145.             if(inseridos == 6 && idad != 0){
  146.                 if(pasta == "Ceratocone")
  147.                     aux.ceratocone = true;
  148.                 else
  149.                     aux.ceratocone = false;
  150.                 aux.idade = exams[strng].idade;
  151.                 aux.PIO = exams[strng].PIO;
  152.                 pat->push_back(aux);
  153.             }
  154.             else if(inseridos == 6){
  155.                 strng = "";
  156.                 strng += strA; strng += "_";
  157.                 strng += strB; strng += "_";
  158.                 if(strcmp(strC, "Left") == 0) {
  159.                     strcpy(strC, "Right");
  160.                     cout << "MUDOUR ";
  161.                 }
  162.                 else {
  163.                     strcpy(strC, "Left");
  164.                     cout << "MUDOUL ";
  165.                 }
  166.                 strng += strC; strng += "_";
  167.                 strng += strD ; strng += "_Diagrams.csv";
  168.  
  169.                 idad = exams[strng].idade;
  170.                 if(idad != 0) {
  171.                     if (pasta == "Ceratocone")
  172.                         aux.ceratocone = true;
  173.                     else
  174.                         aux.ceratocone = false;
  175.                     aux.idade = exams[strng].idade;
  176.                     aux.PIO = exams[strng].PIO;
  177.                     pat->push_back(aux);
  178.                 }
  179.                 else {
  180.                     cout << strng << " - " << idad << " - " << exams[strng].PIO << endl;
  181.  
  182.                     FILE *inf;
  183.                     inf = fopen("Arquivos sem informacoes.txt", "a+");
  184.  
  185.                     fprintf(inf, "%s\n", arquivos[i].c_str());
  186.                     fclose(inf);
  187.                 }
  188.             }
  189.             else {
  190.                 cout << "INVALIDO(" << inseridos << ") - " << strng << endl;
  191.  
  192.                 FILE *inv;
  193.                 inv = fopen(("Arquivos invalidos - "+ pasta +".txt").c_str(), "a+");
  194.  
  195.                 fprintf(inv, "%s\n", arquivos[i].c_str());
  196.                 fclose(inv);
  197.             }
  198.         }
  199.  
  200. }
  201.  
  202. int idadeAtual(int nascDia, int nascMes, int nascAno, int examDia, int examMes, int examAno){
  203.     int idade = examAno - nascAno - 1;
  204.     if(examMes > nascMes)
  205.         idade++;
  206.     else if(examMes == nascMes && examDia >= nascDia)
  207.         idade++;
  208.     return idade;
  209. }
  210.  
  211. void preparaMapaExames(map<string, exam> &exams){
  212.     FILE *arquivo;
  213.     arquivo = fopen("Exams.csv", "r");
  214.  
  215.     char lixo, line[1000], line2[1000], ultimoNome[100], primeiroNome[100], exameData[100], exameHora[100], exameOlho[100];
  216.     int nascDia = 0, nascMes = 0, nascAno = 0, exameDia = 0, exameMes = 0, exameAno = 0, exameHrs = 0, exameMin = 0, exameSeg = 0;
  217.  
  218.     fscanf(arquivo, "%[^\n]\n", line);
  219.     while(fscanf(arquivo, " %[^;]%c", ultimoNome, &lixo) != EOF){
  220.         exam aux;
  221.         aux.idade = 0;
  222.         aux.PIO = 0;
  223.  
  224.         for(int i = 0; i < 5; i++){
  225.             line[0] = '\0';
  226.  
  227.             if(i != 4)
  228.                 fscanf(arquivo, "%[^;]%c", line, &lixo);
  229.             else
  230.                 fscanf(arquivo, "%[^\n]%c", line, &lixo);
  231.             if (i == 0) {
  232.                 strcpy(primeiroNome, line);
  233.             } else if (i == 1) {
  234.                 sscanf(line, "%02d,%02d.%04d", &nascDia, &nascMes, &nascAno);
  235.             } else if (i == 2) {
  236.                 sscanf(line, "%02d,%02d.%04d", &exameDia, &exameMes, &exameAno);
  237.                 sprintf(exameData, "%04d%02d%02d", exameAno, exameMes, exameDia);
  238.             } else if (i == 3) {
  239.                 strcpy(exameOlho, line);
  240.             } else if (i == 4) {
  241.                 string str = line;
  242.                 replace(str.begin(), str.end(), ',', '.');
  243.                 aux.PIO = (float) atof(str.c_str());
  244.                 aux.idade = idadeAtual(nascDia, nascMes, nascAno, exameDia, exameMes, exameAno);
  245.             }
  246.         }
  247.         fscanf(arquivo, " ");
  248.  
  249.         string nomeArquivo(ultimoNome); nomeArquivo += "_";
  250.         nomeArquivo += primeiroNome; nomeArquivo += "_";
  251.         nomeArquivo += exameOlho; nomeArquivo += "_";
  252.         nomeArquivo += exameData; nomeArquivo += "_Diagrams.csv";
  253.  
  254.         //cout << "/" << nomeArquivo << endl;
  255.         exams[nomeArquivo] = aux;
  256.         //printf("%d %.1f - %s\n", exams[nomeArquivo].idade, exams[nomeArquivo].PIO, nomeArquivo);
  257.  
  258.         line[0] = '\0'; line2[0] = '\0'; ultimoNome[0] = '\0'; primeiroNome[0] = '\0'; exameOlho[0] = '\0'; exameData[0] = '\0'; exameHora[0] = '\0';
  259.     }
  260.     fclose(arquivo);
  261. }
  262.  
  263.  
  264.  
  265.  
  266. inline bool fileExists (const std::string& name) {
  267.     struct stat buffer;
  268.     return (stat (name.c_str(), &buffer) == 0);
  269. }
  270.  
  271. bool dirExists(const char* pzPath){
  272.  
  273.     if ( pzPath == NULL) return false;
  274.  
  275.     DIR *pDir;
  276.     bool bExists = false;
  277.  
  278.     pDir = opendir (pzPath);
  279.  
  280.     if (pDir != NULL)
  281.     {
  282.         bExists = true;
  283.         (void) closedir (pDir);
  284.     }
  285.  
  286.     return bExists;
  287.  
  288. }
  289.  
  290. void imprimeNoCSV(bool ceratocone, int idade, float PIO, float DAm_Area,
  291.                   float DAm_AlturaMaxima,
  292.                   float ALe_Pico1, float ALe_Pico2, int ALe_DistanciaPicos,
  293.                   float CVe_AlturaPico1, float CVe_AlturaPico2, float CVe_Area,
  294.                   float CVe_InclinacaoReta,
  295.                   float DLe_Area, float DLe_AlturaMaxima,
  296.                   float CPo_Area, float CPo_AlturaMedia,
  297.                   float DAr_Area, float DAr_AlturaMaxima){
  298.     bool result = fileExists("Results patients.csv");
  299.  
  300.     if(result) {
  301.         ifstream arquivo("Results patients.csv");
  302.         int lines = 0;
  303.         string line;
  304.         while (getline(arquivo, line))
  305.             ++lines;
  306.         if(lines == 0)
  307.             result = false;
  308.     }
  309.  
  310.     FILE* arquivo;
  311.     arquivo = fopen("Results patients.csv", "a+");
  312.  
  313.     if(!result) {
  314.         fprintf(arquivo, ""
  315.                 "Ceratocone;"
  316.                 "Idade;"
  317.                 "PIO;"
  318.                 "DAm_Area;"
  319.                 "DAm_AlturaMaxima;"
  320.                 "ALe_Pico1;"
  321.                 "ALe_Pico2;"
  322.                 "ALe_DistanciaPicos;"
  323.                 "CVe_AlturaPico1;"
  324.                 "CVe_AlturaPico2;"
  325.                 "CVe_Area;"
  326.                 "CVe_InclinacaoReta;"
  327.                 "DLe_Area;"
  328.                 "DLe_AlturaMaxima;"
  329.                 "CPo_Area;"
  330.                 "CPo_AlturaMedia;"
  331.                 "DAr_Area;"
  332.                 "DAr_AlturaMaxima\n");
  333.     }
  334.  
  335.     fprintf(arquivo, "%c;"
  336.                     "%d;%.1f;"
  337.                     "%f;%f;"
  338.                     "%f;%f;%d;"
  339.                     "%f;%f;%f;%f;"
  340.                     "%f;%f;"
  341.                     "%f;%f;"
  342.                     "%f;%f\n",
  343.             ceratocone ? 't' : 'f',
  344.             idade, PIO,
  345.             DAm_Area, DAm_AlturaMaxima,
  346.             ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
  347.             CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta,
  348.             DLe_Area, DLe_AlturaMaxima,
  349.             CPo_Area, CPo_AlturaMedia,
  350.             DAr_Area, DAr_AlturaMaxima);
  351.  
  352.     fclose(arquivo);
  353. }
  354.  
  355. void processaPaciente(connection *C, patient pat){
  356.     //DECLARANDO OBJETO DE OPERAÇÕES PARA USAR SEUS MÉTODOS
  357.     Operacoes *op = new Operacoes();
  358.  
  359.     float DAm_Area, DAm_AlturaMaxima,
  360.             ALe_Pico1, ALe_Pico2,
  361.             CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta, CVe_InclinacaoRL,
  362.             DLe_Area, DLe_AlturaMaxima,
  363.             CPo_Area, CPo_AlturaMedia,
  364.             DAr_Area, DAr_AlturaMaxima;
  365.  
  366.     int     ALe_DistanciaPicos;
  367.  
  368.     //MÉTODOS DA NOVA BIBLIOTECA SENDO USADOS
  369.     op->deformationAmplitude(pat.deformationAmplitude, &DAm_Area, &DAm_AlturaMaxima);
  370.     op->applanationLength(pat.applanationLength, &ALe_Pico1, &ALe_Pico2, &ALe_DistanciaPicos);
  371.     op->cornealVelocity(pat.cornealVelocity, &CVe_AlturaPico1, &CVe_AlturaPico2, &CVe_Area, &CVe_InclinacaoReta, &CVe_InclinacaoRL);
  372.     op->deflectionLenght(pat.deflectionLength, &DLe_Area, &DLe_AlturaMaxima);
  373.     op->concavePower(pat.concavePower, &CPo_Area, &CPo_AlturaMedia);
  374.     op->deflectionArea(pat.deflectionArea, &DAr_Area, &DAr_AlturaMaxima);
  375.  
  376.     /*dbop->inserirDados(&(*C), pat.ceratocone, DAm_Area, DAm_AlturaMaxima,
  377.                        ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
  378.                        CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta, CVe_InclinacaoRL,
  379.                        DLe_Area, DLe_AlturaMaxima,
  380.                        CPo_Area, CPo_AlturaMedia,
  381.                        DAr_Area, DAr_AlturaMaxima);
  382. */
  383.     imprimeNoCSV(pat.ceratocone, pat.idade, pat.PIO, DAm_Area, DAm_AlturaMaxima,
  384.                  ALe_Pico1, ALe_Pico2, ALe_DistanciaPicos,
  385.                  CVe_AlturaPico1, CVe_AlturaPico2, CVe_Area, CVe_InclinacaoReta,
  386.                  DLe_Area, DLe_AlturaMaxima,
  387.                  CPo_Area, CPo_AlturaMedia,
  388.                  DAr_Area, DAr_AlturaMaxima);
  389.  
  390.     string nomeArq = pat.nomeArquivo;
  391.     string pasta = pat.ceratocone ? "Ceratocone" : "Normal";
  392.     int res = rename((pasta +"/Diagrams "+ pasta +"/" + nomeArq).c_str(),
  393.                      (pasta +"/Diagrams "+ pasta +" Valids/" + nomeArq).c_str());
  394.     if (res == 1)
  395.         cout << "Error on moving file " + nomeArq << endl;
  396. }
  397.  
  398. bool isDirExist(const std::string& path)
  399. {
  400. #if defined(_WIN32)
  401.     struct _stat info;
  402.     if (_stat(path.c_str(), &info) != 0)
  403.     {
  404.         return false;
  405.     }
  406.     return (info.st_mode & _S_IFDIR) != 0;
  407. #else
  408.     struct stat info;
  409.     if (stat(path.c_str(), &info) != 0)
  410.     {
  411.         return false;
  412.     }
  413.     return (info.st_mode & S_IFDIR) != 0;
  414. #endif
  415. }
  416.  
  417. bool makePath(const std::string& path){
  418. #if defined(_WIN32)
  419.     int ret = _mkdir(path.c_str());
  420. #else
  421.     mode_t mode = 0755;
  422.     int ret = mkdir(path.c_str(), mode);
  423. #endif
  424.     if (ret == 0)
  425.         return true;
  426.  
  427.     switch (errno)
  428.     {
  429.         case ENOENT:
  430.             // parent didn't exist, try to create it
  431.         {
  432.             int pos = path.find_last_of('/');
  433.             if (pos == std::string::npos)
  434. #if defined(_WIN32)
  435.                 pos = path.find_last_of('\\');
  436.             if (pos == std::string::npos)
  437. #endif
  438.                 return false;
  439.             if (!makePath( path.substr(0, pos) ))
  440.                 return false;
  441.         }
  442.             // now, try to create again
  443. #if defined(_WIN32)
  444.             return 0 == _mkdir(path.c_str());
  445. #else
  446.             return 0 == mkdir(path.c_str(), mode);
  447. #endif
  448.  
  449.         case EEXIST:
  450.             // done!
  451.             return isDirExist(path);
  452.  
  453.         default:
  454.             return false;
  455.     }
  456. }
  457.  
  458. int main(){
  459.     map<string, exam> exams;
  460.     preparaMapaExames(exams);
  461.  
  462.     //Criar diretório para mover os arquivos válidos, caso não exista:
  463.     bool validsFolderExists = dirExists("Ceratocone/Diagrams Ceratocone Valids");
  464.     if(!validsFolderExists) {
  465.         makePath("Ceratocone/Diagrams Ceratocone Valids");
  466.     }
  467.     validsFolderExists = dirExists("Normal/Diagrams Normal Valids");
  468.     if(!validsFolderExists) {
  469.         makePath("Normal/Diagrams Normal Valids");
  470.     }
  471.  
  472.     vector<patient> patients;
  473.     formarPacientes(&patients, exams, "Ceratocone");
  474.     formarPacientes(&patients, exams, "Normal");
  475.  
  476.     try{
  477.         connection C("dbname=testdb user=postgres password=123456 hostaddr=127.0.0.1 port=5432");
  478.         if (C.is_open()) {
  479.             cout << "Opened database successfully: " << C.dbname() << endl;
  480.         } else {
  481.             cout << "Can't open database" << endl;
  482.             return 1;
  483.         }
  484.  
  485.         //dbop->deletaTabela(&C);
  486.         //dbop->criaTabela(&C);
  487.         for(int i = 0; i < patients.size(); i++)
  488.             processaPaciente(&C, patients[i]);
  489.  
  490.  
  491.         C.disconnect ();
  492.     }catch (const std::exception &e){
  493.         cerr << e.what() << std::endl;
  494.         return 1;
  495.     }
  496.  
  497.     cout << "Processo concluido, aperte enter para finalizar." << endl;
  498.     getchar();
  499.     return 0;
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement