NCBI C++ ToolKit
configurable_file.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: configurable_file.cpp 51489 2011-10-06 14:50:39Z gouriano $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Ivanov
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include "configurable_file.hpp"
32 #include "proj_builder_app.hpp"
33 #include "msvc_prj_defines.hpp"
34 #include "msvc_project_context.hpp"
35 #include "ptb_err_codes.hpp"
36 #include <corelib/ncbistr.hpp>
37 
39 
40 
41 bool CreateConfigurableFile(const string& src_path, const string& dst_path,
42  const string& config_name)
43 {
44  string dst(dst_path);
45  dst += ".candidate";
46 
47  // Create dir if no such dir...
48  string dir;
49  CDirEntry::SplitPath(dst_path, &dir);
50  CDir project_dir(dir);
51  if ( !project_dir.Exists() ) {
52  CDir(dir).CreatePath();
53  }
54 
55  CNcbiIfstream is(src_path.c_str(), IOS_BASE::in | IOS_BASE::binary);
56  if ( !is ) {
57  NCBI_THROW(CProjBulderAppException, eFileOpen, src_path);
58  }
59  CNcbiOfstream os(dst.c_str(),
60  IOS_BASE::out | IOS_BASE::binary | IOS_BASE::trunc);
61  if ( !os ) {
62  NCBI_THROW(CProjBulderAppException, eFileCreation, dst_path);
63  }
64 
65  // Read source file
66  string str;
67  char buf[1024*16];
68  while ( is ) {
69  is.read(buf, sizeof(buf));
70  if ( is.gcount() > 0 && str.size() == str.capacity()) {
71  str.reserve(str.size() +
72  max((SIZE_TYPE)is.gcount(), str.size() / 2));
73  }
74  str.append(buf, is.gcount());
75  }
76 
77  // Replace some variables:
78 
79  // ---------- @c_ncbi_runpath@ ----------
80 
81  string run_path = GetApp().GetEnvironment().Get("NCBI_INSTALL_PATH");
82  // For installing toolkit the path like to
83  // "$NCBI_INSTALL_PATH/lib/static/DebugDLL",
84  // otherwise the path like to
85  // ".../compilers/msvc710_prj/static/bin/DebugDLL".
86  if ( run_path.empty() ) {
87  run_path = GetApp().GetProjectTreeRoot();
88  run_path = CDirEntry::ConcatPath(run_path,
89  GetApp().GetBuildType().GetTypeStr());
90  run_path = CDirEntry::ConcatPath(run_path, "bin");
91  } else {
92  run_path = CDirEntry::ConcatPath(run_path, "lib");
93  run_path = CDirEntry::ConcatPath(run_path,
94  GetApp().GetBuildType().GetTypeStr());
95  }
96  run_path = CDirEntry::ConcatPath(run_path, config_name);
97  run_path = NStr::Replace(run_path, "\\", "\\\\");
98  str = NStr::Replace(str, "@c_ncbi_runpath@", run_path);
99 
100  // ---------- @SYBASE_PATH@ ----------
101  string sb_path;
102  if ( GetApp().GetSite().ResolveDefine("SYBASE_PATH", sb_path )) {
103  if (!CDirEntry(sb_path).Exists()) {
104  sb_path.erase();
105  }
106  }
107  sb_path = NStr::Replace(sb_path, "\\", "\\\\");
108  str = NStr::Replace(str, "@SYBASE_PATH@", sb_path);
109  // ------------------------------------
110 
111  // ---------- @FEATURES@ ----------
112  {
113  string features;
114  const set<string>& epackages =
116  ITERATE(set<string>, e, epackages) {
117  if (!features.empty()) {
118  features += " ";
119  }
120  features += *e;
121  }
122 
123  list<string> std_features;
124  GetApp().GetSite().GetStandardFeatures(std_features);
125  ITERATE(list<string>, s, std_features) {
126  if (!features.empty()) {
127  features += " ";
128  }
129  features += *s;
130  }
131 
132  const set<string>& dpackages =
134  ITERATE(set<string>, d, dpackages) {
135  if (!features.empty()) {
136  features += " ";
137  }
138  features += "-";
139  features += *d;
140  }
141  str = NStr::Replace(str, "@FEATURES@", features);
142  }
143  // ------------------------------------
144 
145  // Write result
146  os.write(str.data(), str.size());
147  if ( !os ) {
148  NCBI_THROW(CProjBulderAppException, eFileCreation, dst_path);
149  }
150  os.close();
151 
152  if (PromoteIfDifferent(dst_path,dst)) {
154  "Configuration file modified");
155  } else {
156  PTB_INFO_EX(dst_path, ePTB_NoError,
157  "Configuration file unchanged");
158  }
159  return true;
160 }
161 
162 
163 string ConfigurableFileSuffix(const string& config_name)
164 {
165  return config_name;
166  //string delim = "__";
167  //return delim + config_name + delim;
168 }
169 
170 
CDirEntry –.
Definition: ncbifile.hpp:262
CDir –.
Definition: ncbifile.hpp:1695
static const set< string > & GetEnabledPackages(const string &config_name)
static const set< string > & GetDisabledPackages(const string &config_name)
void GetStandardFeatures(list< string > &features) const
Definition: msvc_site.cpp:727
CProjBulderAppException –.
const CMsvcSite & GetSite(void)
string GetProjectTreeRoot(void) const
bool CreateConfigurableFile(const string &src_path, const string &dst_path, const string &config_name)
string ConfigurableFileSuffix(const string &config_name)
The NCBI C++ standard methods for dealing with std::string.
std::ofstream out("events_result.xml")
main entry point for tests
static int trunc
Definition: array_out.c:8
static const char * str(char *buf, int n)
Definition: stats.c:84
const CNcbiEnvironment & GetEnvironment(void) const
Get the application's cached environment.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
const string & Get(const string &name, bool *found=NULL) const
Get environment value by name.
Definition: ncbienv.cpp:109
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
bool CreatePath(TCreateFlags flags=fCreate_Default) const
Create the directory path recursively possibly more than one at a time.
Definition: ncbifile.cpp:4106
virtual bool Exists(void) const
Check if directory "dirname" exists.
Definition: ncbifile.hpp:4065
static string ConcatPath(const string &first, const string &second)
Concatenate two parts of the path for the current OS.
Definition: ncbifile.cpp:776
static void SplitPath(const string &path, string *dir=0, string *base=0, string *ext=0)
Split a path string into its basic components.
Definition: ncbifile.cpp:358
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
static string & Replace(const string &src, const string &search, const string &replace, string &dst, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3314
char * buf
bool PromoteIfDifferent(const string &present_path, const string &candidate_path, const string &ignore)
T max(T x_, T y_)
std::istream & in(std::istream &in_, double &x_)
CProjBulderApp & GetApp(void)
access to App singleton
#define PTB_INFO_EX(file, err_code, msg)
@ ePTB_FileModified
@ ePTB_NoError
#define PTB_WARNING_EX(file, err_code, msg)
Modified on Fri Jun 14 16:48:51 2024 by modify_doxy.py rev. 669887