Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
messages.cpp
Go to the documentation of this file.
1
29#include "def.h" // for Def
30#include "diagnostic.h" // for issue_diag()
31#include "path.h" // for get_pathname()
32#include "scanner.h" // for scanner
33
34#include <cassert> // for assert()
35#include <fstream> // for ofstream
36
37
38static const char lc_name[] = "LC_MESSAGES";
39
40
41void Def::process_messages()
42{
43 issue_diag (I_STAGE, false, 0, "processing %s section\n", lc_name);
44
45 // nesting level
46 int nesting_level = 0;
47
48 messages_def_found_ = true;
49
50 std::string name;
51
52 while ((next = scanner_.next_token()).token != Scanner::tok_messages) {
53
54 switch(next.token) {
55
57 next = scanner_.next_token();
58 if (next.token == Scanner::tok_messages) {
59 // end of numeric block
60 if (nesting_level == 0)
61 return;
62
63 nesting_level--;
64 scanner_.close ();
65 } else
66 issue_diag (E_SYNTAX, true, &next,
67 "wrong section name in END directive\n");
68
69 break;
70
71 case Scanner::tok_copy: {
72 next = scanner_.next_token();
73 if (next.token != Scanner::tok_string)
74 issue_diag (E_SYNTAX, true, &next,
75 "expected string following \"copy\" directive\n");
76
77 // bump up the nesting level
78 nesting_level++;
79
80 issue_diag (I_STAGE, false, 0, "processing copy directive\n");
81
82 // open the file
83 scanner_.open (get_pathname (strip_quotes (next.name), next.file));
84
85 // get comment char and escape char;
86 // these informations are stored by the scanner
87 while ((next = scanner_.next_token ()).token
89 // the LC_IDENTIFICATION section may also have a
90 // LC_MESSAGES token that will mess up the parsing
91 if (next.token == Scanner::tok_ident) {
92 while ((next = scanner_.next_token()).token
94 next = scanner_.next_token();
95 }
96 }
97 break;
98 }
99
101 next = scanner_.next_token();
102 messages_st_.yesexpr = convert_string (next.name);
103 messages_st_.wyesexpr = convert_wstring (next);
104 break;
105 }
106
107 case Scanner::tok_noexpr: {
108 next = scanner_.next_token();
109 messages_st_.noexpr = convert_string (next.name);
110 messages_st_.wnoexpr = convert_wstring (next);
111 break;
112 }
113
114 default:
115 break;
116 }
117 }
118}
119
120
121void Def::write_messages(std::string dir_name)
122{
123 assert (!dir_name.empty());
124
125 if (messages_written_)
126 return;
127
128 if (!messages_def_found_) {
129 issue_diag (I_SKIP, false, 0,
130 "%s section not found, skipping\n", lc_name);
131 return;
132 }
133
134 (dir_name += _RWSTD_PATH_SEP) += lc_name;
135
136 issue_diag (I_OPENWR, false, 0, "writing %s\n", dir_name.c_str ());
137
138 std::ofstream out (dir_name.c_str(), std::ios::binary);
139 out.exceptions (std::ios::failbit | std::ios::badbit);
140
141 // now calculate the offsets for the wide string representations
142 messages_out_.yesexpr_off[1] = 0;
143 messages_out_.noexpr_off[1] = unsigned (messages_out_.yesexpr_off[1]
144 + (messages_st_.wyesexpr.size() + 1) * sizeof (wchar_t));
145
146 // now calculate the offsets for the narrow string representations
147 messages_out_.yesexpr_off[0] = unsigned (messages_out_.noexpr_off[1]
148 + (messages_st_.wnoexpr.size() + 1) * sizeof (wchar_t));
149 messages_out_.noexpr_off[0] = unsigned (messages_out_.yesexpr_off[0]
150 + (messages_st_.yesexpr.size() + 1) * sizeof (char));
151
152 messages_out_.codeset_off = unsigned (messages_out_.noexpr_off[0]
153 + (messages_st_.noexpr.size() + 1) * sizeof (char));
154 messages_out_.charmap_off = unsigned (messages_out_.codeset_off
155 + (charmap_.get_code_set_name().size() + 1) * sizeof (char));
156
157 // first write out the messages structure
158 out.write ((char*)&messages_out_, sizeof(messages_out_));
159
160 // now write out all the strings
161 out.write ((const char*)messages_st_.wyesexpr.c_str(),
162 (messages_st_.wyesexpr.size() + 1) * sizeof (wchar_t));
163 out.write ((const char*)messages_st_.wnoexpr.c_str(),
164 (messages_st_.wnoexpr.size() + 1) * sizeof (wchar_t));
165
166 out << messages_st_.yesexpr << std::ends;
167 out << messages_st_.noexpr << std::ends;
168 out << charmap_.get_code_set_name() << std::ends;
169 out << charmap_.get_charmap_name() << std::ends;
170 out.close();
171}
const int out
#define assert(expr)
Definition _cassert.h:40
#define _RWSTD_PATH_SEP
Definition _config.h:297
const std::string & get_code_set_name() const
Definition charmap.h:112
std::string get_charmap_name() const
Definition charmap.cpp:475
void write_messages(std::string dir_name)
Definition messages.cpp:121
void open(std::string, char='#', char='\\')
Definition scanner.cpp:142
void close()
Definition scanner.cpp:169
token_t next_token()
Definition scanner.cpp:352
@ tok_end
Definition scanner.h:66
@ tok_string
Definition scanner.h:99
@ tok_messages
Definition scanner.h:54
@ tok_yesexpr
Definition scanner.h:152
@ tok_copy
Definition scanner.h:166
@ tok_ident
Definition scanner.h:60
@ tok_noexpr
Definition scanner.h:153
const char * name
Definition codecvt1.cpp:72
std::string strip_quotes(const std::string &str)
Definition def.h:604
bool issue_diag(int type, bool, const Scanner::token_t *token, const char *fmt,...)
@ I_OPENWR
Definition diagnostic.h:83
@ I_SKIP
Definition diagnostic.h:86
@ I_STAGE
Definition diagnostic.h:81
@ E_SYNTAX
Definition diagnostic.h:49
std::string get_pathname(const std::string &fname, const std::string &other)
Definition path.cpp:129
std::string name
Definition scanner.h:178
token_id token
Definition scanner.h:179
const char * file
Definition scanner.h:186