tools/incparser/ParseWriter.cs
author Scott Ehlert <ds@alliedmods.net>
Tue May 13 08:22:49 2008 +0000 (2008-05-13)
changeset 2051 568678a2e847
parent 2019 9f473114a511
permissions -rw-r--r--
Trunk build fixes
Fixed up IGameHelpers.h such that it no longer requires HL2SDK headers if a function doesn't need them
ds@1952
     1
using System;
pred@2019
     2
using System.Collections;
ds@1952
     3
using System.Collections.Generic;
ds@1952
     4
using System.Text;
pred@2019
     5
using System.IO;
ds@1952
     6
ds@1952
     7
namespace incparser
ds@1952
     8
{
ds@1952
     9
    class ParseWriter
ds@1952
    10
    {
ds@1952
    11
        private int level;
ds@1952
    12
        private StringBuilder data;
ds@1952
    13
pred@2019
    14
        public ArrayList enumList = new ArrayList();
pred@2019
    15
        public ArrayList defineList = new ArrayList();
pred@2019
    16
        public ArrayList enumTypeList = new ArrayList();
pred@2019
    17
        public ArrayList forwardList = new ArrayList();
pred@2019
    18
        public ArrayList nativeList = new ArrayList();
pred@2019
    19
        public ArrayList stockList = new ArrayList();
pred@2019
    20
        public ArrayList funcenumList = new ArrayList();
pred@2019
    21
        public ArrayList functagList = new ArrayList();
pred@2019
    22
        public ArrayList structList = new ArrayList();
pred@2019
    23
ds@1952
    24
        public int Level
ds@1952
    25
        {
ds@1952
    26
            get
ds@1952
    27
            {
ds@1952
    28
                return level;
ds@1952
    29
            }
ds@1952
    30
        }
ds@1952
    31
ds@1952
    32
        public string Contents
ds@1952
    33
        {
ds@1952
    34
            get
ds@1952
    35
            {
ds@1952
    36
                return data.ToString();
ds@1952
    37
            }
ds@1952
    38
        }
ds@1952
    39
pred@2019
    40
        public void Reset()
pred@2019
    41
        {
pred@2019
    42
            level = 0;
pred@2019
    43
            data = new StringBuilder();
pred@2019
    44
        }
pred@2019
    45
ds@1952
    46
        public ParseWriter()
ds@1952
    47
        {
ds@1952
    48
            level = 0;
ds@1952
    49
            data = new StringBuilder();
ds@1952
    50
        }
ds@1952
    51
ds@1952
    52
        public void BeginSection(string name)
ds@1952
    53
        {
ds@1952
    54
            WriteLine("\"" + PrepString(name) + "\"");
ds@1952
    55
            WriteLine("{");
ds@1952
    56
            level++;
ds@1952
    57
        }
ds@1952
    58
ds@1952
    59
        public void WritePair(string key, string value)
ds@1952
    60
        {
ds@1952
    61
            WriteLine("\"" + PrepString(key) + "\"\t\t\"" + PrepString(value) + "\"");
ds@1952
    62
        }
ds@1952
    63
ds@1952
    64
        public void EndSection()
ds@1952
    65
        {
ds@1952
    66
            if (--level < 0)
ds@1952
    67
            {
ds@1952
    68
                throw new System.Exception("Writer nesting level went out of bounds");
ds@1952
    69
            }
ds@1952
    70
            WriteLine("}");
ds@1952
    71
        }
ds@1952
    72
pred@2019
    73
        public void WriteFiles(string template, string outputfile)
pred@2019
    74
        {
pred@2019
    75
            StreamReader sr = null;
pred@2019
    76
pred@2019
    77
            try
pred@2019
    78
            {
pred@2019
    79
                sr = File.OpenText(template);
pred@2019
    80
            }
pred@2019
    81
            catch (Exception e)
pred@2019
    82
            {
pred@2019
    83
                Console.WriteLine("Failed to open template file: " + e.Message);
pred@2019
    84
                return;
pred@2019
    85
            }
pred@2019
    86
pred@2019
    87
            string contents = sr.ReadToEnd();
pred@2019
    88
pred@2019
    89
            string replace = ToOutputString(defineList);
pred@2019
    90
            contents = contents.Replace("$defines", replace);
pred@2019
    91
pred@2019
    92
            replace = ToOutputString(enumList);
pred@2019
    93
            contents = contents.Replace("$enums", replace);
pred@2019
    94
pred@2019
    95
            replace = ToOutputString(enumTypeList);
pred@2019
    96
            contents = contents.Replace("$enumtypes", replace);
pred@2019
    97
pred@2019
    98
            replace = ToOutputString(forwardList);
pred@2019
    99
            contents = contents.Replace("$forwards", replace);
pred@2019
   100
pred@2019
   101
            replace = ToOutputString(nativeList);
pred@2019
   102
            contents = contents.Replace("$natives", replace);
pred@2019
   103
pred@2019
   104
            replace = ToOutputString(stockList);
pred@2019
   105
            contents = contents.Replace("$stocks", replace);
pred@2019
   106
pred@2019
   107
            replace = ToOutputString(funcenumList);
pred@2019
   108
            contents = contents.Replace("$funcenums", replace);
pred@2019
   109
pred@2019
   110
            replace = ToOutputString(functagList);
pred@2019
   111
            contents = contents.Replace("$functags", replace);
pred@2019
   112
pred@2019
   113
            replace = ToOutputString(structList);
pred@2019
   114
            contents = contents.Replace("$structs", replace);
pred@2019
   115
pred@2019
   116
            StreamWriter sw;
pred@2019
   117
            sw = File.CreateText(outputfile);
pred@2019
   118
pred@2019
   119
            sw.Write(contents);
pred@2019
   120
pred@2019
   121
            sr.Close();
pred@2019
   122
            sw.Close();
pred@2019
   123
        }
pred@2019
   124
pred@2019
   125
        private string ToOutputString(ArrayList a)
pred@2019
   126
        {
pred@2019
   127
            string defines = "";
pred@2019
   128
            int count = 0;
pred@2019
   129
pred@2019
   130
            foreach (object o in a)
pred@2019
   131
            {
pred@2019
   132
              defines += o;
pred@2019
   133
              defines += " ";
pred@2019
   134
              count += o.ToString().Length;
pred@2019
   135
pred@2019
   136
              if (count > 180)
pred@2019
   137
              {
pred@2019
   138
                defines += "\r\n";
pred@2019
   139
                count = 0;
pred@2019
   140
              }
pred@2019
   141
            }
pred@2019
   142
pred@2019
   143
            return defines;
pred@2019
   144
        }
pred@2019
   145
ds@1952
   146
        private void WriteLine(string line)
ds@1952
   147
        {
ds@1952
   148
            Tabinate();
ds@1952
   149
            data.Append(line + "\n");
ds@1952
   150
        }
ds@1952
   151
ds@1952
   152
        private void Tabinate()
ds@1952
   153
        {
ds@1952
   154
            for (int i = 0; i < level; i++)
ds@1952
   155
            {
ds@1952
   156
                data.Append("\t");
ds@1952
   157
            }
ds@1952
   158
        }
ds@1952
   159
ds@1952
   160
        private string PrepString(string text)
ds@1952
   161
        {
ds@1952
   162
            /* Escape all escaped newlines (so they can be unescaped later) */
ds@1952
   163
            text = text.Replace("\\n", "\\\\n");
ds@1952
   164
            /* Escape all literal newlines */
ds@1952
   165
            text = text.Replace("\n", "\\n");
ds@1952
   166
            text = text.Replace("\r", "");
ds@1952
   167
            /* Remove escaped quotations */
ds@1952
   168
            text = text.Replace("\\\"", "\"");
ds@1952
   169
            /* Replace all quotations with escaped ones now */
ds@1952
   170
            text = text.Replace("\"", "\\\"");
ds@1952
   171
            return text;
ds@1952
   172
        }
ds@1952
   173
    }
ds@1952
   174
}