| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file FileWriter.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @date Jan 15, 2012 | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "FileWriter.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							| 
									
										
										
										
											2012-02-07 01:18:25 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace wrap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  | FileWriter::FileWriter(const string& filename, bool verbose, const string& comment_str) | 
					
						
							|  |  |  | : verbose_(verbose),filename_(filename), comment_str_(comment_str) | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  | void FileWriter::emit(bool add_header, bool force_overwrite) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   if (verbose_) cerr << "generating " << filename_ << " "; | 
					
						
							|  |  |  |   // read in file if it exists
 | 
					
						
							|  |  |  |   string existing_contents; | 
					
						
							|  |  |  |   bool file_exists = true; | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     existing_contents = file_contents(filename_.c_str(), add_header); | 
					
						
							| 
									
										
										
										
											2014-06-01 23:30:04 +08:00
										 |  |  |   } catch (CantOpenFile) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     file_exists = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Only write a file if it is new, an update, or overwrite is forced
 | 
					
						
							|  |  |  |   string new_contents = oss.str(); | 
					
						
							|  |  |  |   if (force_overwrite || !file_exists || existing_contents != new_contents) { | 
					
						
							|  |  |  |     ofstream ofs(filename_.c_str(), ios::binary); // Binary to use LF line endings instead of CRLF
 | 
					
						
							|  |  |  |     if (!ofs) throw CantOpenFile(filename_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // dump in stringstream
 | 
					
						
							|  |  |  |     ofs << new_contents; | 
					
						
							|  |  |  |     ofs.close(); | 
					
						
							|  |  |  |     if (verbose_) cerr << " ...complete" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Add small message whenever writing a new file and not running in full verbose mode
 | 
					
						
							|  |  |  |     if (!verbose_) | 
					
						
							|  |  |  |       cout << "wrap: generating " << filename_ << endl; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     if (verbose_) cerr << " ...no update" << endl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |