把#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(@"#defines+(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},
", item.Value, item.Key);
}
tbTarget.Text = result;
原文地址:https://www.cnblogs.com/wzwyc/p/6811096.html