LOGIN / SIGN UP
Style cleanup of CfgParserSource.
2008-10-03 10:33 8307a..9d1ba

src/CfgParserSource.cc
 
25 @@ -25,43 +25,43 @@
25
26 unsigned int CfgParserSourceCommand::_sigaction_counter = 0;
27
28 //! @brief
29 //! @return
30 /**
31 * Open file based configuration source.
32 */
33 bool
34 CfgParserSourceFile::open (void)
35 throw (std::string&)
36 CfgParserSourceFile::open(void)
37 throw (std::string&)
38 {
39 if (_op_file) {
40 throw string ("TRYING TO OPEN ALLREADY OPEN SOURCE");
41 if (_file) {
42 throw string("TRYING TO OPEN ALLREADY OPEN SOURCE");
43 }
44
45 _op_file = fopen (_or_name.c_str (), "r");
46 if (! _op_file) {
47 throw string("FAILED TO OPEN " + _or_name);
48 _file = fopen(_name.c_str(), "r");
49 if (! _file) {
50 throw string("failed to open file " + _name);
51 }
52
53 return true;
54 }
55
56 //! @brief
57 void
58 CfgParserSourceFile::close (void)
59 throw (std::string&)
60 CfgParserSourceFile::close(void)
61 throw (std::string&)
62 {
63 if (! _op_file) {
64 throw string("TRYING TO CLOSE ALLREADY CLOSED SOURCE");
65 if (! _file) {
66 throw string("trying to close already closed source");
67 }
68
69 fclose (_op_file);
70 _op_file = NULL;
71 fclose(_file);
72 _file = 0;
73 }
74
75 //! @brief
76 //! @param
77 //! @return
78 /**
79 * Run command and treat output as configuration source.
80 */
81 bool
82 CfgParserSourceCommand::open (void)
83 throw (std::string&)
84 CfgParserSourceCommand::open(void)
85 throw (std::string&)
86 {
87 int fd[2];
88 int status;
...
83 @@ -83,17 +83,17 @@
83 sigaction(SIGCHLD, &action, &_sigaction);
84 }
85
86 _o_pid = fork ();
87 if (_o_pid == -1) { // Error
88 _pid = fork();
89 if (_pid == -1) { // Error
90 return false;
91
92 } else if (_o_pid == 0) { // Child
93 dup2 (fd[1], STDOUT_FILENO);
94 } else if (_pid == 0) { // Child
95 dup2(fd[1], STDOUT_FILENO);
96
97 ::close (fd[0]);
98 ::close (fd[1]);
99 ::close(fd[0]);
100 ::close(fd[1]);
101
102 execlp ("/bin/sh", "sh", "-c", _or_name.c_str(), (char *) NULL);
103 execlp("/bin/sh", "sh", "-c", _name.c_str(), (char *) 0);
104
105 // PRINT ERROR
106
...
105 @@ -105,15 +105,17 @@
105
106 ::close (fd[1]);
107
108 _op_file = fdopen (fd[0], "r");
109 _file = fdopen(fd[0], "r");
110 }
111 return true;
112 }
113
114 //! @brief
115 /**
116 * Close source, wait for child process to finish.
117 */
118 void
119 CfgParserSourceCommand::close (void)
120 throw (std::string&)
121 CfgParserSourceCommand::close(void)
122 throw (std::string&)
123 {
124 if (_sigaction_counter < 1) {
125 return;
...
123 @@ -121,21 +123,21 @@
123 _sigaction_counter--;
124
125 // Close source.
124 fclose (_op_file);
127 fclose(_file);
128
129 // Wait for process.
130 int status;
131 int pid_status;
132
131 status = waitpid(_o_pid, &pid_status, 0);
134 status = waitpid(_pid, &pid_status, 0);
135
136 // If no other open CfgParserSourceCommand open, restore sigaction.
137 if (_sigaction_counter == 0) {
136 sigaction(SIGCHLD, &_sigaction, NULL);
139 sigaction(SIGCHLD, &_sigaction, 0);
140 }
141
142 // Wait failed, throw error
143 if (status == -1) {
142 throw string("FAILED TO WAIT FOR PID " + Util::to_string<int> (_o_pid));
145 throw string("failed to wait for pid " + Util::to_string<int>(_pid));
146 }
147 }
...

src/CfgParserSource.hh
 
1 @@ -1,10 +1,5 @@
1 //! @file
2 //! @author Claes Nasten <pekdon{@}pekdon{.}net
3 //! @date 2005-06-15
4 //! @brief Configuration parser source handler.
5
6 //
7 // Copyright (C) 2005 Claes Nasten <pekdon{@}pekdon{.}net>
8 // Copyright © 2005-2008 Claes Nästén <me@pekdon.net>
9 //
10 // This program is licensed under the GNU GPL.
11 // See the LICENSE file for more information.
...
27 @@ -32,73 +27,73 @@
27 SOURCE_VIRTUAL //!< Source base type.
28 };
29
35 CfgParserSource (const std::string &or_source) :
36 _op_file (NULL), _or_name(or_source),
37 _type(SOURCE_VIRTUAL), _i_line (0)
33 CfgParserSource(const std::string &source)
34 : _file(0), _name(source),
35 _type(SOURCE_VIRTUAL), _line(0)
36 {
37 }
38 virtual ~CfgParserSource (void) { }
39
45 //! @brief Gets a character from _op_file, increments line count if \n.
46 inline int getc (void) {
47 int i_c = fgetc (_op_file);
48 if (i_c == '\n') {
49 ++_i_line;
45 //! @brief Gets a character from _file, increments line count if \n.
46 inline int getc(void) {
47 int c = fgetc(_file);
48 if (c == '\n') {
49 ++_line;
50 }
56 return i_c;
52 return c;
53 }
54
55 //! @brief Returns a character to _op_file, decrements line count if \n.
61 inline void ungetc (int i_c) {
62 ::ungetc (i_c, _op_file);
63 if (i_c == '\n') {
64 --_i_line;
60 inline void ungetc(int c) {
61 ::ungetc (c, _file);
62 if (c == '\n') {
63 --_line;
64 }
65 }
66
72 const std::string &get_name(void) { return _or_name; }
68 const std::string &get_name(void) { return _name; }
69 CfgParserSource::Type get_type(void) { return _type; }
75 int get_line (void) { return _i_line; }
71 int get_line(void) { return _line; }
72
78 virtual bool open (void) throw (std::string&) { return false; }
79 virtual void close (void) throw (std::string&) { }
75 virtual bool open(void) throw (std::string&) { return false; }
76 virtual void close(void) throw (std::string&) { }
77
78 protected:
84 FILE *_op_file;
85 const std::string &_or_name;
81 FILE *_file;
82 const std::string &_name;
83 CfgParserSource::Type _type;
89 int _i_line;
85 int _line;
86 };
87
88 class CfgParserSourceFile : public CfgParserSource
89 {
90 public:
96 CfgParserSourceFile (const std::string &or_source)
97 : CfgParserSource (or_source)
93 CfgParserSourceFile (const std::string &source)
94 : CfgParserSource(source)
95 {
96 _type = SOURCE_FILE;
97 }
98 virtual ~CfgParserSourceFile (void) { }
99
105 virtual bool open (void) throw (std::string&);
106 virtual void close (void) throw (std::string&);
102 virtual bool open(void) throw (std::string&);
103 virtual void close(void) throw (std::string&);
104 };
105
106 class CfgParserSourceCommand : public CfgParserSource
107 {
108 public:
114 CfgParserSourceCommand (const std::string &or_source)
115 : CfgParserSource (or_source)
111 CfgParserSourceCommand(const std::string &source)
112 : CfgParserSource (source)
113 {
114 _type = SOURCE_COMMAND;
115 }
121 virtual ~CfgParserSourceCommand (void) { }
117 virtual ~CfgParserSourceCommand(void) { }
118
124 virtual bool open (void) throw (std::string&);
125 virtual void close (void) throw (std::string&);
121 virtual bool open(void) throw (std::string&);
122 virtual void close(void) throw (std::string&);
123
124 private:
130 pid_t _o_pid;
126 pid_t _pid;
127
128 struct sigaction _sigaction; //!< sigaction for restore.
129 static unsigned int _sigaction_counter; //!< Counts open.
...