Next version of JABA
[jabaws.git] / binaries / src / clustalw / src / general / OutputFile.cpp
1 /**
2  * Author: Mark Larkin
3  * 
4  * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
5  */
6 #ifdef HAVE_CONFIG_H
7     #include "config.h"
8 #endif
9 #include "OutputFile.h"
10 #include "utils.h"
11 #include "userparams.h"
12
13 namespace clustalw
14 {
15
16 OutputFile::OutputFile()
17 {
18
19 }
20
21 OutputFile::~OutputFile()
22 {
23     // If it is open, close it and say that a file has been created!!!!!
24     if(file.get())
25     {
26         file->close();
27         utilityObject->info("%s file created:   [%s]\n", typeOfFileMsg.c_str(),
28                                 name.c_str());                         
29     }
30 }
31
32 bool OutputFile::openFile(std::string* fileName, const std::string msg, const std::string* path, 
33                       const std::string ext, const std::string fileType)
34 {
35     if (fileName->empty())
36     {
37         *fileName = getOutputFileName(msg, *path, ext);
38             
39         if(fileName->empty())
40         {
41             return false;
42         }
43     }
44
45     file.reset(new std::ofstream(fileName->c_str(), std::ofstream::trunc));
46                 
47     if(!file->is_open()) 
48     {
49         utilityObject->error("Cannot open output file [%s]\n", fileName->c_str()); 
50         return false;
51     }
52     name = *fileName; 
53     typeOfFileMsg = fileType;
54     
55     return true;
56 }
57
58 bool OutputFile::isOpen()
59 {
60     return file->is_open();
61 }
62
63 std::ofstream* OutputFile::getPtrToFile()
64 {
65     return file.get();
66 }
67                       
68 std::string OutputFile::getOutputFileName(const std::string prompt, std::string path, 
69                                           const std::string fileExtension)
70 {
71     std::string temp;
72     std::string _fileName; // Will return this name.
73     std::string message;
74     _fileName = path + fileExtension;
75
76     if(_fileName.compare(userParameters->getSeqName()) == 0) 
77     {
78         cerr << "WARNING: Output file name is the same as input file.\n";
79         if (userParameters->getMenuFlag()) 
80         {
81             message = "\n\nEnter new name to avoid overwriting  [" + _fileName + "]: ";
82             utilityObject->getStr(message, temp);
83             if(temp != "")
84             {
85                 _fileName = temp;
86             }
87         }
88     }
89     else if (userParameters->getMenuFlag()) 
90     {
91
92         message = prompt + " [" + _fileName + "]";
93         utilityObject->getStr(message, temp);
94         if(temp != "")
95         {
96             _fileName = temp;
97         }
98     }   
99     return _fileName;
100
101 }
102
103 }
104
105