libzypp  17.25.0
RequestedLocalesFile.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <fstream>
14 
15 #include <zypp/base/LogTools.h>
16 #include <zypp/base/IOStream.h>
17 #include <zypp/base/String.h>
18 
19 #include <zypp/PathInfo.h>
20 #include <zypp/TmpPath.h>
21 #include <zypp/Date.h>
22 
24 
25 using std::endl;
26 
28 namespace zypp
29 {
30  namespace target
32  {
33 
34  void RequestedLocalesFile::load( const Pathname & file_r, LocaleSet & locales_r )
35  {
36  PathInfo pi( file_r );
37  if ( ! pi.isFile() )
38  {
39  WAR << "Can't read " << pi << endl;
40  return;
41  }
42  std::ifstream infile( file_r.c_str() );
43  for( iostr::EachLine in( infile ); in; in.next() )
44  {
45  std::string l( str::trim(*in) );
46  if ( ! l.empty() && l[0] != '#' )
47  {
48  locales_r.insert( Locale(l) );
49  }
50  }
51  MIL << "Read " << pi << endl;
52  }
53 
54  void RequestedLocalesFile::store( const Pathname & file_r, const LocaleSet & locales_r )
55  {
57  filesystem::chmod( tmp.path(), 0644 );
58 
59  std::ofstream outs( tmp.path().c_str() );
60  outs << "# zypp::RequestedLocales generated " << Date::now() << endl;
61  dumpRange( outs, locales_r.begin(), locales_r.end(), "#", "\n", "\n", "\n", "#\n" );
62  outs.close();
63 
64  if ( outs.good() )
65  {
66  filesystem::rename( tmp.path(), file_r );
67  MIL << "Wrote " << PathInfo(file_r) << endl;
68  }
69  else
70  {
71  ERR << "Can't write " << PathInfo(tmp.path()) << endl;
72  }
73  }
74 
75  /******************************************************************
76  **
77  ** FUNCTION NAME : operator<<
78  ** FUNCTION TYPE : std::ostream &
79  */
80  std::ostream & operator<<( std::ostream & str, const RequestedLocalesFile & obj )
81  {
82  str << obj.file() << ' ';
83  if ( obj._localesPtr )
84  str << obj.locales();
85  else
86  str << "(unloaded)";
87  return str;
88  }
89 
91  } // namespace target
94 } // namespace zypp
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition: IOStream.cc:71
TmpPath.h
PathInfo.h
zypp::filesystem::TmpPath::path
Pathname path() const
Definition: TmpPath.cc:146
zypp::dumpRange
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:91
zypp::filesystem::rename
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition: PathInfo.cc:690
zypp::target::RequestedLocalesFile::_localesPtr
scoped_ptr< LocaleSet > _localesPtr
Definition: RequestedLocalesFile.h:100
MIL
#define MIL
Definition: Logger.h:79
zypp::target::RequestedLocalesFile::load
static void load(const Pathname &file_r, LocaleSet &locales_r)
Read LocaleSet from file_r.
Definition: RequestedLocalesFile.cc:34
zypp::filesystem::chmod
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition: PathInfo.cc:1040
IOStream.h
LogTools.h
zypp::target::RequestedLocalesFile::file
const Pathname & file() const
Return the file path.
Definition: RequestedLocalesFile.h:46
zypp::filesystem::PathInfo
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:220
zypp::target::operator<<
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
Definition: CommitPackageCache.cc:155
zypp::filesystem::TmpFile
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:127
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition: Pathname.h:110
RequestedLocalesFile.h
WAR
#define WAR
Definition: Logger.h:80
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition: PathInfo.h:289
Date.h
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::LocaleSet
std::unordered_set< Locale > LocaleSet
Definition: Locale.h:27
zypp::Date::now
static Date now()
Return the current time.
Definition: Date.h:78
String.h
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
ERR
#define ERR
Definition: Logger.h:81
zypp::target::RequestedLocalesFile::locales
const LocaleSet & locales() const
Return the loacale set.
Definition: RequestedLocalesFile.h:53
str
String related utilities and Regular expression matching.
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:216
zypp::Locale
'Language[_Country]' codes.
Definition: Locale.h:49
zypp::filesystem::TmpFile::makeSibling
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition: TmpPath.cc:218
zypp::target::RequestedLocalesFile::store
static void store(const Pathname &file_r, const LocaleSet &locales_r)
Write LocaleSet to file_r.
Definition: RequestedLocalesFile.cc:54
zypp::target::RequestedLocalesFile
Save and restore locale set from file.
Definition: RequestedLocalesFile.h:35