把#define宏转换成指定格式

之前在弄一个东西的,有一大堆的宏,需要把它转换成其它的形式。遇到这种大批量的东西,我特别没有耐心去一个一个的弄,于是写了一段代码。

估计大家平常比较难用得上,不过可以平常相似的情况用来参考。

SortedDictionary<int, string> enumMap = new SortedDictionary<int, string>();
string fileName = "tmp.txt";
File.WriteAllText(fileName, tbSource.Text, Encoding.Default);
string[] items = File.ReadAllLines(fileName, Encoding.Default);
Regex reg = new Regex(@"#define\s+(\S+)\s+(\d+)");
foreach(string item in items)
{
    Match match = reg.Match(item);
    string value = match.Groups[1].Value;
    int key = Convert.ToInt32(match.Groups[2].Value);
    if(enumMap.ContainsKey(key))
    {
        Debug.WriteLine(item);
        Debug.Assert(false);
    }
    else
    {
        enumMap.Add(key, value);
    }
}
string result = "";
foreach(var item in enumMap)
{
    result += string.Format("{0} = {1},\n", item.Value, item.Key);
}
tbTarget.Text = result;

 

posted @ 2017-05-05 08:16  wzwyc  阅读(194)  评论(0编辑  收藏  举报