Thread: General How to go about this question?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    21

    General How to go about this question?

    I'm working on this code that I'm hoping can take a C file and rearrange bad indentation, ignore comments, remove whitespace etc to get it to an agreed format.

    Heres what I have so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *check=0;
    char ch=!EOF;
    char* compare="int main";
    int i, c, a, indent;
    FILE *input;
    FILE *output;
    
    void Print()
    {
    fprintf(output, "%c", ch);
    }
    
    
    void Check()
    {
    check = realloc( check, i+1 );
    check[i] = fgetc(input);
    ch = check[i];
    if(ch == '\t')
    {
    Check();
    }
    }
    
    void EndofLine()
    {
    for(i = i; ch != '\n'; i++)
    {
    check = realloc( check, i+1 );
    check[i] = fgetc(input);
    ch = check[i];
    fprintf(output, "%c", ch);
    }
    }
    
    void EndofComment()
    {
    i++;
    check = realloc( check, i+1 );
    check[i] = fgetc(input);
    ch = check[i];
    
    if(ch == '*')
    {
    Print();
    EndofLine();
    }else{
    i--;
    }
    }
    
    void MainCheck()
    {
    i++;
    check[i] = '\0';
    int strcmp(
       const char *check,
       const char *compare
    );
    i--;
    }
    
    int main(int argc, char *argv[])
    {
    input = fopen(argv[1], "r");
    output = fopen("output.c", "w");
    
    
            for(i = 0; ch != EOF; i++)
                                    {
                                    Check();
    
    								if(ch == '/')
    								{
    								Print();
    								EndofComment();
    								}
                                    
    								if(ch == 'i')
    								{
    								MainCheck();
    								}
    								
    								if(a > 0)
    								{
    									if((ch != '\n') || (ch != '\r'))
    									{
    									fprintf(output, "\n");
    									a--;
    									}
    								}
    								if(ch=='{')
    								{
    								a++;
    								}
    								if((ch=='}')&&(c != 0))
    								{
    								fprintf(output, "\n");
    								a--;
    								}
    								
    								if(ch == ';')
    								{
    								c++;
    								}else{
    								c = 0;
    								}
    
                                    Print();
    															
    
                                    }
    
    fclose(input);
    fclose(output);
    
    return 0;
    }

    I can't help but think I'm going about it in the wrong way. I'm currently reading it character by character but what I need to be able to do is see what the character before and after it is to impose my rules.

    Thanks in Advance.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here are my comments to what you have so far.

    1) Comment it
    2) Fix the indentation

    Then, I'll consider looking at it.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your indentation program --- needs much better indentation.

    There is a program called "astyle" that already does this, btw.

    For a program that needs to react to the text it's importing, it's almost always better to pull in a whole row of text at a time, using fgets(). Then use sscanf() on the char buffer which now holds the line of text:

    Code:
    while((fgets(buffer, sizeof(buffer), filePointer) != NULL) { //reads all data in the file,row by row
      //rest of your code in here
      sscanf(buffer, "%d %s, %c", &myNumberVar, mystringVar, &mycharVar); //e.g
    }
    There is no overflow problem (it's safe). Make char buffer[about 80 char's].

    Joy abounds.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    There is no overflow problem (it's safe). Make char buffer[about 80 char's].
    Just a thought... 80 characters might not be enough. I typically work on 100 character margins in my IDE and I've seen people using up to 128... Perhaps 256 would make more sense, the extra is a small price to pay to eliminate the risk.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Thank for all that pointers.

    Sorry the indentation is so bad, its not normally its just been a case of deleting and restarting and I work in Notepad++ then copy it over to the server via Putty and lots of the formatting is lost.


    Someone mentioned in an earlier thread "dont use global variables thats noob stuff", now I am a noob. Is it a global variable if you declare it before the main? What difference does it make? I only do it this way as thats the way I've been taught at school.

    Second question, also we don't indent functions above the main, is that the same in the usual C programming etiquette?

    Thank for the clarification, sometimes lectures just aren't enough an its good to hear the view point of others in the know.

    I'll try to re-write it tommorrow using fgets() as it seems ideal because the problem with using fgetc() is that you can't compare the character before and after to apply the right rules.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Chems View Post
    Thank for all that pointers.

    Sorry the indentation is so bad, its not normally its just been a case of deleting and restarting and I work in Notepad++ then copy it over to the server via Putty and lots of the formatting is lost.

    Someone mentioned in an earlier thread "dont use global variables thats noob stuff", now I am a noob. Is it a global variable if you declare it before the main? What difference does it make? I only do it this way as thats the way I've been taught at school.
    Yes, global variables are the one's you see above the main function. And yes, they're for noobs, in general. They're only for struct and function prototypes, and occasionally, a variable that is used in (almost) every single function.

    Why are they bad? It breaks the encapsulation of the functions, adds to the potential for much harder to understand logic, and increases the likelihood that a local variable will wind up with the same name as one of the global variables - "covering up" the global version of the varriable. First time you see that, you'll swear your compiler has gone insane.

    The purpose of functions is to "break down" the functionality of a program, into smaller "chunks". Global variables, aside from definitions, chip away at that concept.

    Quote Originally Posted by Chems View Post
    Second question, also we don't indent functions above the main, is that the same in the usual C programming etiquette?
    Yes, indentation starts with the first line of code that is subordinate:

    Code:
    int main(void) {
      //first subordinate line of code
      int i, j, n; //i,j, and n are totally encapsulated, in function main
      
    
      //last subordinate line of code
      return 0; //returning to the OS
    }
    Quote Originally Posted by Chems View Post
    Thank for the clarification, sometimes lectures just aren't enough an its good to hear the view point of others in the know.

    I'll try to re-write it tommorrow using fgets() as it seems ideal because the problem with using fgetc() is that you can't compare the character before and after to apply the right rules.
    You'll like fgets() once you see what it can do.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Thank Adat, very informative. I will try to stop using global variables from now on!

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    First off, thanks for putting me onto fgets, it so easy to use a few of the strings library functions to check for all the things that were such a hassle before.

    I'm nearly there now but I want to be able to do the output like before. So my question is this,

    Although I'm reading the input line by line, how do I input a character into the output string once the function has given me a pointer for instance:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
            FILE *input;
            FILE *output;
            char line[256];
            int ptr;
            output = fopen("output.c", "w");
    
                    if((input = fopen(argv[1], "r")) != NULL )
                    {
                            while(fgets(line, 256, input) != NULL)
                            {
    												
    				fprintf(output, "%s", line);
    						
    					char *ptr = strchr(line, '{');
                            
    						if(ptr)
    						        {
    							   /*Code here to insert \n after the { in the string*/
    							 }
    
                            }
                    }
    
    fclose(input);
    fclose(output);
    
    }
    TIA, and I hope the indentation is better an the use of non global variables.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well I think it's a mistake to immediately output the input like that. You have no idea if it's well formed or not.

    As for indentation, you basically need to know what level you're going into. Then you can dump the line that opens the block like

    fprintf(output, "%s\n", statement);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-26-2010, 01:12 PM
  2. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  3. General Programming Question!!
    By chottachatri in forum C# Programming
    Replies: 7
    Last Post: 07-29-2009, 10:51 AM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM