Try not to break utf-8 while writing a config file, to help with future hand-editing

This commit is contained in:
Jean-Francois Dockes 2016-01-05 09:21:11 +01:00
parent f67e5b1428
commit f5f936ff4e

View File

@ -251,28 +251,45 @@ int ConfSimple::get(const string &nm, string &value, const string &sk) const
} }
// Appropriately output a subkey (nm=="") or variable line. // Appropriately output a subkey (nm=="") or variable line.
// Splits long lines // We can't make any assumption about the data except that it does not
// contain line breaks.
// Avoid long lines if possible (for hand-editing)
// We used to break at arbitrary places, but this was ennoying for
// files with pure UTF-8 encoding (some files can be binary anyway),
// because it made later editing difficult, as the file would no
// longer have a valid encoding.
// Any ASCII byte would be a safe break point for utf-8, but could
// break some other encoding with, e.g. escape sequences? So break at
// whitespace (is this safe with all encodings?).
// Note that the choice of break point does not affect the validity of
// the file data (when read back by conftree), only its ease of
// editing with a normal editor.
static ConfSimple::WalkerCode varprinter(void *f, const string &nm, static ConfSimple::WalkerCode varprinter(void *f, const string &nm,
const string &value) const string &value)
{ {
ostream *output = (ostream *)f; ostream& output = *((ostream *)f);
if (nm.empty()) { if (nm.empty()) {
*output << "\n[" << value << "]\n"; output << "\n[" << value << "]\n";
} else { } else {
string value1; output << nm << " = ";
if (value.length() < 60) { if (nm.length() + value.length() < 75) {
value1 = value; output << value;
} else { } else {
string::size_type pos = 0; string::size_type ll = 0;
while (pos < value.length()) { for (string::size_type pos = 0; pos < value.length(); pos++) {
string::size_type len = MIN(60, value.length() - pos); string::value_type c = value[pos];
value1 += value.substr(pos, len); output << c;
pos += len; ll++;
if (pos < value.length()) // Break at whitespace if line too long and "a lot" of
value1 += "\\\n"; // remaining data
if (ll > 50 && (value.length() - pos) > 10 &&
(c == ' ' || c == '\t')) {
ll = 0;
output << "\\\n";
}
} }
} }
*output << nm << " = " << value1 << "\n"; output << "\n";
} }
return ConfSimple::WALK_CONTINUE; return ConfSimple::WALK_CONTINUE;
} }