Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Macro Preprocessor in C#

, 6 Apr 2005
This library supplies the same macro substitution facilities as the C/C++ preprocessor.
prepro_src.zip
using System;
using System.IO;

class PreproTest {
    static void Test(string s) {
        Console.WriteLine(ms.Substitute(s));
    }    
  
    static string Input() {
        Console.Write(">> ");
        return Console.In.ReadLine();
    }
    
    class MyMacroSubstitutor : MacroSubstitutor {
        public override string CustomReplacement(string s) {
            switch(s) {
            case "DATE": return DateTime.Now.ToString();  
            case "USER": return Environment.GetEnvironmentVariable("USERNAME"); 
            }
            return "";
        }
    }    
    
    static MacroSubstitutor ms = new MyMacroSubstitutor();    

    static void Main(string[] args) {                
        string line;
        ms.AddMacro("DATE",MacroEntry.Custom,null);
        ms.AddMacro("USER",MacroEntry.Custom,null);
        while((line = Input()) != null) {
            line = ms.ProcessLine(line);
            if (line != "")
                Console.WriteLine(line);
        }
    }
    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Steve Donovan
Web Developer
South Africa South Africa
Steve is the author of 'C++ By Example'
(Que 2002) and developer of the UnderC interactive C++ interpreter.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 7 Apr 2005
Article Copyright 2005 by Steve Donovan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid