Find the answer to your Linux question:
Results 1 to 10 of 10
Hi all! I use ubuntu 9.04; I have a function called "isLogEnabled" in my project on windows, I wanna port it to linux. I know that I should have a ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    27

    Problem in "porting" a function on windows to linux?

    Hi all!

    I use ubuntu 9.04;
    I have a function called "isLogEnabled" in my project on windows, I wanna port it to linux.
    I know that I should have a "config.h" file in linux to get log from a specific Shared object and ..., but because I'm new to linux,
    I don't know how this should be done, this is my function:

    Code:
    //This function return True if log policy is set in registry and False otherwise
       int isLogEnabled()
       {
         HKEY hKey;
         LONG lRes;
         DWORD dwType, dwSize = 0;
         int retVal = 0;
     
         if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyCorp", 0, KEY_ALL_ACCESS, &hKey)) == ERROR_SUCCESS)
         {
            lRes = RegQueryValueEx(hKey, "SpecialMode", 0, &dwType, NULL, &dwSize );
     
            if(lRes == ERROR_SUCCESS)
               retVal = 1;
            RegCloseKey(hKey);
         }
         return retVal;
      }
    Since I think we don't have registry concept in linux and this function uses registry APIs, I'm confused to change it to be compatible
    with linux;

    Could you help me with this please?!

    TIA.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    if this is some kind of configuration value, usually config files are stored in /etc for applications on linux, then you should store the config values in a file there and look into the file for specific values

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Your code is very specific to Windows functionality and Visual-C programming. You need to determine what you want to do, and figure out how to do that in standard C or C++ without dependencies on Windows code and characteristics. This may be simple, or not, depending upon the complexity of the problem.

    So, what do you MEAN by the function name 'isLogEnabled'?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Sep 2009
    Posts
    27
    Hi and thanks for your help!

    My config file would only contain "retVal = 1", because this is the return value of "isLogEnabled" function and in my main function it's checked as below:

    if(isLogEnabled())
    initLogger(LOG_FILE);

    If I don't use the libconfig functions and just want to load the config file content(retval), what should I do?

    I mean:
    I should open a gedit text and only write "retval=1" in it, I name it "Mainconf" but I don't know what would I define it's extension(.cfg or .config or ...)?

    I should save it in /etc directory, now how can I load it to my program to check it and what would be the alternative for the part:

    if(isLogEnabled())
    initLogger(LOG_FILE);

    in linux?

    I'm new to software developing in linux, Please guide me!!

    THX again.

  5. #5
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    you could use syslog - <syslog.h>

    i'm not a c/c++ guy, so this was just a 5 second google thing

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Well, your code:
    Code:
    if(isLogEnabled())
    {
        initLogger(LOG_FILE);
    }
    is perfectly resonable syntax for any C or C++ application. I am still unclear on what your problem is. Linux doesn't use a registry, but it often uses configuration files in /etc such as /etc/samba/smb.conf as an example, which will contain this sort of information. When the associated program starts, it reads this configuration file and sets its internal variables accordingly.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Note that a lot of a program's configuration information may also be found in /opt/program-name or /usr/share/program-name instead of /etc. /etc is usually reserved for more system-level servers and such. So for example, the configuration information for OpenOffice can usually be found in /opt/openoffice.org/share/config or /opt/openoffice.org/share/registry. Where precisely these configuration files are found, what format they are in, and what data they contain, is entirely up to the application in question.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Sep 2009
    Posts
    27
    Quote Originally Posted by Rubberman View Post
    When the associated program starts, it reads this configuration file and sets its internal variables accordingly.
    I exactly want this!

    when my program .exe is run, how does it read the config file and check the "retval" value in the program to see if it is "1" to run the next line!

    Should I use an API to open this config file in the "main" function and an API to read and check "retval" instead of the code:
    Code:
    if(isLogEnabled())
    {
        initLogger(LOG_FILE);
    }
    or not?

    I don't know the process of this operation:
    When the associated program starts, it reads this configuration file and sets its internal variables accordingly
    And could you guide me what the apprpriate APIs are to open a file, read it's content, etc... for my purpose please?!

    And also what's the answer of this question:
    what would I define it's extension(.cfg or .config or ...)?
    I'll highly appreciate if you reply completely again.

    THX so much.

  9. #9
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    extension doesn't matter, you can do whatever you want

  10. #10
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Opening/reading files are standard Unix/Linux operations. You need to review the standard C API functions for file handling - basic Computer Science 101 stuff. You can find most of that in Kernighan and Ritchie.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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