CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2011
    Posts
    59

    Help! StreamReader read one line up

    Hi there,
    I'm working on a parser/scaffolding proggy to insert a .cs File and automatically read out all the scopes (new namespace, new class, new methos, attributes, fields, constructor etc.) and for noe I'm reading the input line by line and check wether the line starts with a "{". If it does, that means there's a new scope starting and whatever follows defines what is in the scope.
    BUT:
    the TYPE of scope (wether it's a class, or a method or a namespace etc) is written in the line above the "{". At least if we go by auto formatted c# code in VS 2012.
    so what i need it so actually read line by line, and if i hit a "{", read the line above that and add it to my list of lines for that scope.

    this is what i got so for for that particular part

    Code:
      while ((line = input.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (line == "{")
                    {
                        Scope s = new Scope();
    
                        //add the line above the opened scope as line to the scope
                        ScopeBlock.Add(File.ReadLines(path).Take(lineNumber-1).First());
    
                        while (line != "}")
                        {
                            ScopeBlock.Add(line);
                        }
                    }
                    foreach (string zeile in ScopeBlock)
                    {
                        ReadScope(line);
                    }
                    lineNumber++;
                }
    it throws an exception cause of course it can't find anything at the line specified.

    anyone got an idea? Anything is greatly appreciated

    Thanks
    Lisa

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Help! StreamReader read one line up

    Well

    1. you don't show the code where you allocate the new stream reader.
    2. You don't show the code where you handle the exception.
    3. If the file in #1 doesn't exist....

    Please poste a complete code listing if you hope to get help here. Additionally
    please post your input file.


    Best of Luck,
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    I just wanted to focus on the part where I'm trying to access the line above the "{" but alright, here´s the two main methods for reading and writing

    Code:
      public void Read(string path)
            {
                string line = "";
                int lineNumber = 0;
    
               this.ScopeBlock = new List<string>();
    
                //.cs File einlesen
                StreamReader input = new StreamReader(path);
    
                //read each line
                while ((line = input.ReadLine()) != null)
                {
                    line = line.Trim();
    
                    if (line == "{")
                    {
                        Scope s = new Scope();
    
                        //add the line above the opened scope as line to the scope
                        ScopeBlock.Add(File.ReadLines(path).Take(lineNumber-1).First());
    
                        while (line != "}")
                        {
                            ScopeBlock.Add(line);
                        }
                    }
    
                    foreach (string zeile in ScopeBlock)
                    {
                        ReadScope(line);
                    }
    
                    lineNumber++;
                }
                input.Close();
                //  writeEntityFile(n, c);
            }
    
            private void ReadScope(string line)
            {
                Class c = new Class();
                Namespace n = new Namespace();
                c.Attributes = new List<Attribute>();
                c.Interfaces = new List<Interface>();
                c.Usings = new List<Using>();
                int attributeCounter = c.Attributes.Count;
    
                string[] words = line.Split(' ');
    
                if (words.Length >= 2 && words[1] != "{" && words[1] != "}")
                {
                    switch (words[0])
                    {
                        case "using":
                            {
                                Scope s = new Scope();
                                s.Type = "using";
    
                                Using u = new Using();
                                u.Name = words[0] + " ";
                                u.Value = words[1];
    
                                c.Usings.Add(u);
    
                            } break;
    
                        case "namespace":
                            {
                                Scope s = new Scope();
                                s.Type = "namespace";
    
                                n.Name = words[words.Length - 1];
                            } break;
                    };
    
                    switch (words[1])
                    {
                        case "partial":
                            {
                                Scope s = new Scope();
                                s.Type = "Class";
    
                                c.Modifier = new Modifier();
                                c.Modifier.Value = words[0] + " " + words[1] + " class ";
                                c.Name = words[words.Length - 1];
    
                            } break;
                    };
    
                    if (words[1] == "System.DateTime" || words[1] == "bool")
                    {
                        Attribute att = new Attribute();
    
                        att.Modifier = new Modifier();
                        att.Modifier.Value = words[0] + " ";
                        att.Type = words[1] + " ";
                        att.Value = words[2];
                        c.Attributes.Add(att);
    
                        if (words[2] == "isDeleted")
                        {
                            Interface i = new Interface();
                            i.Name = "ISoftDeletable";
                            c.Interfaces.Add(i);
                        }
    
                        else if (words[2] == "CreateDate")
                        {
                            Interface i = new Interface();
                            i.Name = "IHasCreateDate";
                            c.Interfaces.Add(i);
                        }
    
                        else if (words[2] == "LastChange")
                        {
                            Interface i = new Interface();
                            i.Name = "IHasLastChange";
                            c.Interfaces.Add(i);
                        }
                    }
    
                    else if (words[1] == "string" || words[1] == "int" || words[1] == "System.Guid" || words[1] == "Nullable<System.DateTime>")
                    {
                        Attribute att = new Attribute();
    
                        att.Modifier = new Modifier();
                        att.Modifier.Value = words[0] + " ";
                        att.Type = words[1] + " ";
                        att.Value = words[2];
                        c.Attributes.Add(att);
    
                        if (words[2] == "NiceName")
                        {
                            Interface i = new Interface();
                            i.Name = "IHasNiceName";
                            c.Interfaces.Add(i);
                        }
    
                        if (words[2] == "Id")
                        {
                            Interface i = new Interface();
                            i.Name = "IEntityPOCO";
                            c.Interfaces.Add(i);
                        }
                    }
                }
    here's a basic example input file
    I want to "take out" all information and write it into my classes to automatically create mapper files, .js models etc. depending on which fields, attributes, methods etc. are in the input file

    Customer.cs

  4. #4
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    Oh and ofc this is the line where the erros occurs
    Code:
    
    //add the line above the opened scope as line to the scope
     ScopeBlock.Add(File.ReadLines(path).Take(lineNumber-1).First());
    it was just a test and no error handling implemented so far

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Help! StreamReader read one line up

    You should handle the exeption in code, and display the Message in order to understand it.

    Exception handling is different than error handling. An exception contains descriptions
    of its self, and an exception handler provides facilities to recover from the error.

    It is easy to run the debugger on your code, and display the exeption and the message,
    without much effort or any additional code.

    Here have a look at this use of the stream reader with exception handling
    from Microsoft.

    Note: It is possible to put a loop inside a try() block.
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    Last edited by ahoodin; November 18th, 2014 at 03:54 PM.
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    put it in a try catch now, see attachment.
    exception type of InvalidOperationException says "Sequence does not contain any elements"
    Attached Images Attached Images  

  7. #7
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Help! StreamReader read one line up

    Ok now where is the input file?
    Please poste a complete code listing if you hope to get help here. Additionally
    please post your input file
    .
    ahoodin
    To keep the plot moving, that's why.

  8. #8
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    attached in post #3 customer.cs

  9. #9
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    No reply now? D:
    I was wondering if i can start 2 parallel readers, and one of them stores the line above an occuring "{" but I don't know how to accomplish that.
    Any ideas?

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help! StreamReader read one line up

    Just STORE the previous line. Assume it won't START with {
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Help! StreamReader read one line up

    Not sure what your end goal is (to parse files or to extract class information). If the latter, I would take a different approach and load up the file as an assembly and then use reflection to extract the information.

    Use CodeDomProvider.CompileAssemblyFromFile to load up the file. Then use Reflection to get the specifics.

  12. #12
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    that would be someting completely new for me to get into. I thought i could make it easier.
    my end goal is, to read the customer.cs file and auto generate mappers, .js model files etc. depending on different occurences of attributes, methods, fields and so on.
    when the field "CreateDate" of type Datetime is set, i want to auto generate the interfae IHasCreateDate in the mapper file.
    when the field "IsDeleted" of type bool is set i want to auto generate the Interface ISoftDeletable in the mapper File.
    when the user is asked if additional data is supposed to be added, i want the mapper file to contain certain lines, creating a new dictionary..
    and so on and so forth.
    many dependencies.

    I'll read up a bit on what you posted Arjay, thanks a bunch

  13. #13
    Join Date
    Jan 2011
    Posts
    59

    Re: Help! StreamReader read one line up

    Hey, so i read up on Reflection and it´s looking pretty good so far
    Unfortunately there is one method I can't find or are too fuzzy by now to identify as the one I need
    I need to find out the Accessor / modifier of my type I'm passing.
    If we stay in my example, I'm passing the Class Customer of type Customer. When i read the class information with reflection I need to find the value of "public partial class" of Customer. Any hint on where i can find that?
    same for the properties. what i can get it System.int (some int value) {get;set;} but i also need if it`s private or public and print that out at well.
    There must be something I'm missing regarding property / class Access modifiert.

    can anyone help?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured