Thread: Free program I'm sharing: ConvertEnumToStrings

  1. #76
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    Seriously? You have been reading these posts right?
    Yes, I have been reading them, and I am fairly certainly that all the data structures you mentioned (maps, etc...) were given as a way to replace using my program, not to replace using enums.
    Quote Originally Posted by C_ntua View Post
    I think we criticized enough. The points have been said.

    For the actual program, how can you compile this without an IDE? Tried, but doesn't work, just gives me the information.
    And what information is that?
    You should be able to compile with just a C++ compiler and a linker. But since every compiler and linker is different, I can't give instructions on how to do that for every variation.
    Based on the information ONLY
    1) Shouldn't it be better to support all possible formats? What if it is on a single line? If you have
    Code:
    enum A
    {
     a,
     b
    };
    will it work?
    Yes, I tested it with that example, and it works great.
    Like I mentioned in my first post, the format doesn't have to be EXACTLY the same as the example (but it can't be on the same line, like already mentioned). Just as long as its pretty close.
    The real important thing is that you always have at least one space in front of each enum value in the enum. Otherwise, it'll print out an error saying:

    terminate called after throwing an instance of 'std:ut_of_range'
    what(): basic_string::at
    Aborted
    Also, the 'enum' keyword must also have at least one space in front of it, or else the program will claim an enum doesn't exist in the file (the reason for that is, I check to make sure any line containing the 'enum' string has spaces on both sides of 'enum'). Yes, maybe I should have supported all possible formats, but seriously, who's going to try to use this program on a very long enum which is on a single line? In fact, the majority of posters haven't tried using this program at all, because of their prejudices against anything new.
    2) What if the enum is not on an header file? Will you program still work? If you have multiple enumerations on a single header file?
    Yes, my program works on any .h or .cpp file which contains an enum. It also works if said header or source contains multiple enums.
    3) I see nothing about support for class when you tell me on your post that it does support them!!! Before testing a program, I will read the instructions. If I feel it might not work, I won't use it. Wouldn't you do the same? Here is an example
    Code:
    class Day {
    	enum day {
    		Monday,
    		Tuesday
    	};
    }
    Now I am thinking that your program might do something like
    Code:
    class Day {
    	enum day {
    		Monday,
    		Tuesday
    	};
    	char* values[] = {"Monday", "Tuesday" }
    }
    My program works on any enum in any header or source, like already mentioned, so long as the format is similiar to what I already posted. My program doesn't care if the enum is in a class or not. In fact, obviously I wrote it with an enum in a class in mind, because that's what my own enum that I needed this program for is in.
    Which is invalid. Maybe your program does something different, but you have to specify this on the instructions.

    All this questions come in mind. THAT is why I said that a non-standard way has its problems.
    A macro version can still have this problem***

    Not trying to discredit this work. You just have to remember that your program can be extremely useful for yourself, but others don't know their implementation, so you would have to provide them with all the necessary information first in order for them to use them.

    You would need a simple script in order to run your program AND the program you want to run. And every time is a lot of times when you are testing! Lets say you change your enum. Don't you have to run your program again? You won't want to forget and mess things up.
    Yes, you would need to run the program again if you changed the enum. But, best case scenario, you would wait to run the program until the enum has already been written and is completed. But seriously...it takes about 4 seconds on my computer to open up a Terminal, cd to the directory my program is in, and run it, passing to it the filepath to the file which contains an enum. And then my program takes about 1 second to complete, and inform you where the results file is.
    **phantomotap might have in mind a version that supports classes, but that is not the case for ALL macro versions. Of course, you could try and add support for classes. But how are you going to initialize the strings?? You cannot initialize them in the class. You can create an initializer function for them, but then you have to call it somewhere. But then again, I am not a macro expert...
    Well, my program isn't using macros at the moment. And in the generated file which my program generates, all enum value names strings are initalized to their values in a function of the class (usually in the constructor, but sometimes in the getEnumValue() function which sets a single string to the correct value before returning a reference to it). So, anyone making use of the generated methods for retrieving the enum value names, wont have to worry about it.
    Last edited by Programmer_P; 06-07-2010 at 09:38 AM.

  2. #77
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Oh, and I would probably post updated versions here (which follows some of the advice posted in this thread), but I can't edit the first post.

    So feel free to change the CenumOperations::enumFound() function in the "CenumOperations.cpp" file to support all kinds of enum formats.

  3. #78
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Code:
    And before anyone else discovers it, I just learned my program doesn't handle well comments on the line which contains the 'enum' keyword, if you don't have the '{' on the same line as 'enum'. And I already know why. 
    I get the name of the enum by stripping the line containing 'enum' starting from 'm' and ending at '{', exclusive (and removing all spaces). Since the end character its expecting ('{') on that line is not there, it will end up saying the name of the 'enum' is the name of the 'enum' plus the comment.
    
    It also does not expect the last line of the enum to look like this:
    
    Code:
    } workday;
    It will end up thinking that's another enumeration. I'll have to fix that. And I don't need any other derogative comments. Its not like I claimed my program was perfect. If you don't want to use it, don't use it. But don't trash it just because you wont use it. It still might be useful for someone else.
    Stupid forum...it wouldn't let me post it without code tags around the entire post. It claimed I needed code tags, when I already put code tags around the code.
    Last edited by Programmer_P; 06-07-2010 at 10:15 AM.

  4. #79
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, fixed the second problem already.
    It will take a little more logic to workaround the first though.

  5. #80
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    First problem fixed also.

    New version:
    ConvertEnumToStrings.zip

  6. #81
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, here is an example of what the new version of my program does.

    I have a .cpp file which contains an enum (and nothing else, even though it work with lots of other stuff in it too). The enum looks like this:
    Code:
     enum DAY            /* Defines an enumeration type    */
    {
        saturday,       /* Names day and declares a       */
        sunday = 0,     /* variable named workday with    */ 
        monday,         /* that type                      */
        tuesday,
        wednesday,      /* wednesday is associated with 3 */
        thursday,
        friday
    } workday;
    This enum was copied from here.
    Ok, so I now pass this file containing enum to my program:

    Code:
    ./ConvertEnumToStrings "/home/gorilla/Documents/test.cpp" "results"
    The program generates a "results.h" file in the same directory as the file passed. The results file contains the following:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class CDAYToStrings {
    
      public:
        CDAYToStrings();
        //Methods/functions:
        const vector <string>& getEnumValues();
        const string& getNameOfEnum();
        const int& getNumOfEnumValues();
    
        //Enumeration type:
     enum DAY            /* Defines an enumeration type    */
    {
        saturday,       /* Names day and declares a       */
        sunday = 0,     /* variable named workday with    */ 
        monday,         /* that type                      */
        tuesday,
        wednesday,      /* wednesday is associated with 3 */
        thursday,
        friday
    } workday;
    
        //Method/function:
        const string& getEnumValue(DAY enumValue);
    
      private:
        vector <string> enumValueNames;
        string nameOfEnum;
        int numOfEnumValues;
        string enumValueName;
    
    };
    
    CDAYToStrings::CDAYToStrings() {
    
        nameOfEnum = "DAY"; //initialize to name of enum
        numOfEnumValues = 7; //initialize to number of enum values
        enumValueName = ""; //initialize to an empty string
        enumValueNames.push_back("saturday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("sunday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("monday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("tuesday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("wednesday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("thursday"); //add string name of enum value to vector of enum values
        enumValueNames.push_back("friday"); //add string name of enum value to vector of enum values
    
    }
    
    const vector <string>& CDAYToStrings::getEnumValues() {
    
        const vector <string>& rEnumValueNames = enumValueNames; //get a reference to the enum value names
        return rEnumValueNames; //return the reference
    
    }
    
    const string& CDAYToStrings::getNameOfEnum() {
    
        const string& rNameOfEnum = nameOfEnum; //get a reference to the name of enum
        return rNameOfEnum; //return the reference
    
    }
    
    const int& CDAYToStrings::getNumOfEnumValues() {
    
        const int& rNumOfEnumValues = numOfEnumValues; //get a reference to the number of enum values
        return rNumOfEnumValues; //return the reference
    
    }
    
    const string& CDAYToStrings::getEnumValue(DAY enumValue) {
    
      if (enumValue == saturday) {
          enumValueName = "saturday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == sunday) {
          enumValueName = "sunday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == monday) {
          enumValueName = "monday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == tuesday) {
          enumValueName = "tuesday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == wednesday) {
          enumValueName = "wednesday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == thursday) {
          enumValueName = "thursday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
      if (enumValue == friday) {
          enumValueName = "friday";
          const string& rEnumValueName = enumValueName; //get a reference to the enum value name
          return rEnumValueName; //return the reference
      }
    
    }
    As you can see, comments on the same line as the 'enum' keyword are now supported. Also, there is no longer a problem with an enum variable before the semi-colon of the enum.
    Next version will support the whole enum on one line.

  7. #82
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, latest version supports single-line enums:

    ConvertEnumToStrings.zip

    Whatever was posted before in regards to this program likely no longer applies. No spaces before 'enum' or before enum values necessary now.
    All formats are handled, AFAIK, and a combination of several enums with different formats in the same file should not cause any problems either.
    I guess I should have done that from the start.

    Let me know if you encounter any problems, though there shouldn't be any.

    Cheers.
    Last edited by Programmer_P; 06-08-2010 at 06:48 AM.

  8. #83
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Messed up my last post and wasn't posted...

    It didn't work for me. It gave me an error when I tried
    program C:\Users\Gift\Desktop\test.h "results" -c
    as stated. Doesn't accept the path as valid.

    I haven't compiled with an ide though and you have no makefile. Can you make one?
    I tried by making objects of everything and compiling the program with all objects. It should be ok like that, but I have no idea why I get an error.
    Last edited by C_ntua; 06-08-2010 at 04:30 PM.

  9. #84
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by C_ntua View Post
    Messed up my last post and wasn't posted...

    It didn't work for me. It gave me an error when I tried
    program C:\Users\Gift\Desktop\test.h "results" -c
    as stated. Doesn't accept the path as valid.

    I haven't compiled with an ide though and you have no makefile. Can you make one?
    I tried by making objects of everything and compiling the program with all objects. It should be ok like that, but I have no idea why I get an error.
    Yes, '\' is not valid in filepaths. Use '/' instead as a directory separator. And you're not supposed to have a space after the drive letter and colon. Also note that if you pass any filepath with spaces in a file or directory name, you'll need to surround the filepath with quotes. I surround all arguments with double quotes as a matter of habit.
    In regards to the makefile, I'll look into making one.

    EDIT: Nevermind. I see in your post that there is no space after the drive letter and colon. For some reason, when I wrapped the '\' in bold tags, the editor added a space before the first one.
    Last edited by Programmer_P; 06-08-2010 at 08:23 PM.

  10. #85
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I tried / as well, but same error. I will wait for makefile to make sure i am doing this correctly

  11. #86
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by C_ntua View Post
    I tried / as well, but same error. I will wait for makefile to make sure i am doing this correctly
    Ok, well that's probably due to the non-standard way you chose to use the program.
    Just curious: why arent you using an IDE?

    What error is it returning anyway? You say you managed to compile it by "creating objects"? That sounds like you changed the code. Unless you mean object files.

    What compiler are you using, and did you make sure to link all the .obj files into one executable?

  12. #87
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am sure. Using temporarily a PC that has only gcc/g++ installed on. So command prompt and notepad for a while.
    Yeah, meant object files. I am sure I included everything but I might try again to be sure.

  13. #88
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, I'm working on a CMakeLists.txt file right now, which I'll include in the next .zip I upload, along with a copy of the latest version of Cmake (both Linux and Windows versions). That way you can just unpack Cmake, run it, pass to it the path to the directory containing CMakeLists.txt, and Cmake will generate a makefile specific to your OS.

    I guess I prefer doing it that way, because I like for stuff to be as portable and cross-platform as possible.

    And you didn't specify what error it handed out. Please do so, so I can have an idea where the problem is generating from.

  14. #89
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, I did it.

    (For those who dont want to have to mess with CMake and whatnot, here is ConvertEnumToStrings bare minimum)

    Here is the new ConvertEnumToStrings.zip:
    http://dl.dropbox.com/u/8029532/Conv...mToStrings.zip

    And here is the cmake_blah_blah_blah.zip for Windows:
    http://dl.dropbox.com/u/8029532/cmak...-win32-x86.zip

    And then the Linux version of cmake just to be thorough:
    http://dl.dropbox.com/u/8029532/cmak...ux-i386.tar.gz

    And finally nmake.zip for Windows users:
    http://dl.dropbox.com/u/8029532/nmake.zip

    Instructions for ConvertEnumToStrings + CMake:

    If you're running Windows, download ConvertEnumToStrings.zip. Next download cmake_blah_blah_blah.zip (first cmake link above). And finally, download nmake.zip (last link above).

    When all those things are on hand, extract the contents of ConvertEnumToStrings.zip first (retaining all the directory structures by using an option called "Re-create folders" or something similar in whatever program you choose to use to extract the contents).
    Next, browse to the location you extracted to (the Desktop would be most convenient), and open up the "ConvertEnumToStrings" directory.
    You should see a "README.txt" file staring right back at you. Open it and read it to get instructions on what to do next.

    Cheers.

    EDIT: On second thought...
    The Mediafire uploader is acting up.

    EDIT again: Ok, used a different file host. All the download links are there now.
    Last edited by Programmer_P; 06-12-2010 at 06:41 PM.

  15. #90
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
                    if (inputFilePath.at(i) < 'a') {
                        if (inputFilePath.at(i) != '_') {
                            if (inputFilePath.at(i) < 'A') {
                                if (inputFilePath.at(i) != '/') {
                                    if (inputFilePath.at(i) != '.') {
                                        if (inputFilePath.at(i) != ' ') {
    I LOLed at this. Try using the && operator.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. help sharing a program with friends...
    By fingerlickin in forum C# Programming
    Replies: 1
    Last Post: 11-05-2005, 09:54 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Making a program to free up Ram memory
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 06:52 AM