tools/incparser/ParseWriter.cs
author David Anderson <dvander@alliedmods.net>
Sat Aug 10 21:23:02 2013 -0700 (2013-08-10)
changeset 3982 be7835a7c28f
parent 2019 9f473114a511
permissions -rw-r--r--
Move tracker related opcodes entirely to C++.

The tracker related opcodes:
GENARRAY
GENARRAY_Z
TRACKER_POP_SETHEAP
TRACKER_PUSH_C

All contain some vastly overcomplicated assembly containing logic that could
be implemented much easier in C++. If it were a performance concern, these
opcodes would be entirely in C++, but most of them call out to one or more
routines to do additional work. This patch just moves most of the logic out
to C++ to reduce complexity and fix reported bugs.
     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Text;
     5 using System.IO;
     6 
     7 namespace incparser
     8 {
     9     class ParseWriter
    10     {
    11         private int level;
    12         private StringBuilder data;
    13 
    14         public ArrayList enumList = new ArrayList();
    15         public ArrayList defineList = new ArrayList();
    16         public ArrayList enumTypeList = new ArrayList();
    17         public ArrayList forwardList = new ArrayList();
    18         public ArrayList nativeList = new ArrayList();
    19         public ArrayList stockList = new ArrayList();
    20         public ArrayList funcenumList = new ArrayList();
    21         public ArrayList functagList = new ArrayList();
    22         public ArrayList structList = new ArrayList();
    23 
    24         public int Level
    25         {
    26             get
    27             {
    28                 return level;
    29             }
    30         }
    31 
    32         public string Contents
    33         {
    34             get
    35             {
    36                 return data.ToString();
    37             }
    38         }
    39 
    40         public void Reset()
    41         {
    42             level = 0;
    43             data = new StringBuilder();
    44         }
    45 
    46         public ParseWriter()
    47         {
    48             level = 0;
    49             data = new StringBuilder();
    50         }
    51 
    52         public void BeginSection(string name)
    53         {
    54             WriteLine("\"" + PrepString(name) + "\"");
    55             WriteLine("{");
    56             level++;
    57         }
    58 
    59         public void WritePair(string key, string value)
    60         {
    61             WriteLine("\"" + PrepString(key) + "\"\t\t\"" + PrepString(value) + "\"");
    62         }
    63 
    64         public void EndSection()
    65         {
    66             if (--level < 0)
    67             {
    68                 throw new System.Exception("Writer nesting level went out of bounds");
    69             }
    70             WriteLine("}");
    71         }
    72 
    73         public void WriteFiles(string template, string outputfile)
    74         {
    75             StreamReader sr = null;
    76 
    77             try
    78             {
    79                 sr = File.OpenText(template);
    80             }
    81             catch (Exception e)
    82             {
    83                 Console.WriteLine("Failed to open template file: " + e.Message);
    84                 return;
    85             }
    86 
    87             string contents = sr.ReadToEnd();
    88 
    89             string replace = ToOutputString(defineList);
    90             contents = contents.Replace("$defines", replace);
    91 
    92             replace = ToOutputString(enumList);
    93             contents = contents.Replace("$enums", replace);
    94 
    95             replace = ToOutputString(enumTypeList);
    96             contents = contents.Replace("$enumtypes", replace);
    97 
    98             replace = ToOutputString(forwardList);
    99             contents = contents.Replace("$forwards", replace);
   100 
   101             replace = ToOutputString(nativeList);
   102             contents = contents.Replace("$natives", replace);
   103 
   104             replace = ToOutputString(stockList);
   105             contents = contents.Replace("$stocks", replace);
   106 
   107             replace = ToOutputString(funcenumList);
   108             contents = contents.Replace("$funcenums", replace);
   109 
   110             replace = ToOutputString(functagList);
   111             contents = contents.Replace("$functags", replace);
   112 
   113             replace = ToOutputString(structList);
   114             contents = contents.Replace("$structs", replace);
   115 
   116             StreamWriter sw;
   117             sw = File.CreateText(outputfile);
   118 
   119             sw.Write(contents);
   120 
   121             sr.Close();
   122             sw.Close();
   123         }
   124 
   125         private string ToOutputString(ArrayList a)
   126         {
   127             string defines = "";
   128             int count = 0;
   129 
   130             foreach (object o in a)
   131             {
   132               defines += o;
   133               defines += " ";
   134               count += o.ToString().Length;
   135 
   136               if (count > 180)
   137               {
   138                 defines += "\r\n";
   139                 count = 0;
   140               }
   141             }
   142 
   143             return defines;
   144         }
   145 
   146         private void WriteLine(string line)
   147         {
   148             Tabinate();
   149             data.Append(line + "\n");
   150         }
   151 
   152         private void Tabinate()
   153         {
   154             for (int i = 0; i < level; i++)
   155             {
   156                 data.Append("\t");
   157             }
   158         }
   159 
   160         private string PrepString(string text)
   161         {
   162             /* Escape all escaped newlines (so they can be unescaped later) */
   163             text = text.Replace("\\n", "\\\\n");
   164             /* Escape all literal newlines */
   165             text = text.Replace("\n", "\\n");
   166             text = text.Replace("\r", "");
   167             /* Remove escaped quotations */
   168             text = text.Replace("\\\"", "\"");
   169             /* Replace all quotations with escaped ones now */
   170             text = text.Replace("\"", "\\\"");
   171             return text;
   172         }
   173     }
   174 }