summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2011-07-26 10:49:28 +0200
committerMichael Vogt <michael.vogt@ubuntu.com>2011-07-26 10:49:28 +0200
commita513ace2d2e3b71d607257990893199c6105b072 (patch)
tree71610b3a2034dd7cd2d06156baea8df2d8bc989c /apt-pkg/contrib
parentf748b4760d0f8247001c0b46e9eaf02b379bc3c4 (diff)
* apt-pkg/contrib/strutl.{h,cc}, test/libapt/strutil_test.cc:
- add new DeEscapeString() similar to DeQuoteQuotedWord but unescape charackter escapes like \0XXX and \xXX (plus add test)
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/cdromutl.cc8
-rw-r--r--apt-pkg/contrib/strutl.cc63
-rw-r--r--apt-pkg/contrib/strutl.h4
3 files changed, 69 insertions, 6 deletions
diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc
index 551efa7d9..e25caf1a5 100644
--- a/apt-pkg/contrib/cdromutl.cc
+++ b/apt-pkg/contrib/cdromutl.cc
@@ -258,13 +258,9 @@ string FindMountPointForDevice(const char *devnode)
if(TokSplitString(' ', buf, out, 10))
{
fclose(f);
- // unescape \040 and return the path
- size_t pos;
+ // unescape the \0XXX chars in the path
string mount_point = out[1];
- static const char *needle = "\\040";
- while ((pos = mount_point.find(needle)) != string::npos)
- mount_point.replace(pos, strlen(needle), " ");
- return mount_point;
+ return DeEscapeString(mount_point);
}
}
}
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index 072dda3ac..a97dd30e5 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -1240,7 +1240,70 @@ bool CheckDomainList(const string &Host,const string &List)
return false;
}
/*}}}*/
+// ProcessEscapeSequences /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+string DeEscapeString(string &input)
+{
+ char tmp[5];
+ string::const_iterator it, escape_start;
+ string output, octal, hex;
+ for (it = input.begin(); it != input.end(); it++)
+ {
+ // just copy non-escape chars
+ if (*it != '\\')
+ {
+ output += *it;
+ continue;
+ }
+
+ // deal with double escape
+ if (*it == '\\' &&
+ (it + 1 < input.end()) && it[1] == '\\')
+ {
+ // copy
+ output += *it;
+ // advance iterator one step further
+ it += 1;
+ continue;
+ }
+
+ // ensure we have a char to read
+ if (it + 1 == input.end())
+ continue;
+ // read it
+ it++;
+ switch (*it)
+ {
+ case '0':
+ if (it + 3 <= input.end()) {
+ tmp[0] = it[1];
+ tmp[1] = it[2];
+ tmp[2] = it[3];
+ tmp[3] = 0;
+ output += (char)strtol(tmp, 0, 8);
+ it += 2;
+ }
+ break;
+ case 'x':
+ if (it + 2 <= input.end()) {
+ tmp[0] = it[1];
+ tmp[1] = it[2];
+ tmp[2] = 0;
+ output += (char)strtol(tmp, 0, 16);
+ it += 2;
+ }
+ break;
+ default:
+ // FIXME: raise exception here?
+ std::cerr << "lala" << *it << endl;
+ break;
+ }
+ }
+ return output;
+}
+ /*}}}*/
// URI::CopyFrom - Copy from an object /*{{{*/
// ---------------------------------------------------------------------
/* This parses the URI into all of its components */
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index 89cbf0370..43bbfe366 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/apt-pkg/contrib/strutl.h
@@ -39,6 +39,10 @@ bool ParseCWord(const char *&String,string &Res);
string QuoteString(const string &Str,const char *Bad);
string DeQuoteString(const string &Str);
string DeQuoteString(string::const_iterator const &begin, string::const_iterator const &end);
+
+// unescape (\0XXX and \xXX) from a string
+string DeEscapeString(string &input);
+
string SizeToStr(double Bytes);
string TimeToStr(unsigned long Sec);
string Base64Encode(const string &Str);