Thread: Advice changing a function please

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Advice changing a function please

    I have a function that is changing the first letter in the month to lower case and I want to make a change that stops this from occuring. I believe I found a if statement that is making this change. My question is should I just make a change in the code below to say toupper where the first letter of each month is shown or find another solution because the first letter is already in upper case? If more info is needed from this small function I will glady post. Thanks.

    [code]

    123 if ((tolower(tmp_mon[0]) == 'j') &&
    124 (tolower(tmp_mon[1]) == 'a') &&
    125 (tolower(tmp_mon[2]) == 'n'))
    126 ff_time.tm_mon = 0;
    127 if ((tolower(tmp_mon[0]) == 'f') &&
    128 (tolower(tmp_mon[1]) == 'e') &&
    129 (tolower(tmp_mon[2]) == 'b'))
    130 ff_time.tm_mon = 1;
    131 if ((tolower(tmp_mon[0]) == 'm') &&
    132 (tolower(tmp_mon[1]) == 'a') &&
    133 (tolower(tmp_mon[2]) == 'r'))
    134 ff_time.tm_mon = 2;
    135 if ((tolower(tmp_mon[0]) == 'a') &&
    136 (tolower(tmp_mon[1]) == 'p') &&
    137 (tolower(tmp_mon[2]) == 'r'))
    138 ff_time.tm_mon = 3;
    139 if ((tolower(tmp_mon[0]) == 'm') &&
    140 (tolower(tmp_mon[1]) == 'a') &&
    141 (tolower(tmp_mon[2]) == 'y'))
    142 ff_time.tm_mon = 4;
    143 if ((tolower(tmp_mon[0]) == 'j') &&
    144 (tolower(tmp_mon[1]) == 'u') &&
    145 (tolower(tmp_mon[2]) == 'n'))
    146 ff_time.tm_mon = 5;
    147 if ((tolower(tmp_mon[0]) == 'j') &&
    148 (tolower(tmp_mon[1]) == 'u') &&
    149 (tolower(tmp_mon[2]) == 'l'))
    150 ff_time.tm_mon = 6;
    151 if ((tolower(tmp_mon[0]) == 'a') &&
    152 (tolower(tmp_mon[1]) == 'u') &&
    153 (tolower(tmp_mon[2]) == 'g'))
    154 ff_time.tm_mon = 7;
    155 if ((tolower(tmp_mon[0]) == 's') &&
    156 (tolower(tmp_mon[1]) == 'e') &&
    157 (tolower(tmp_mon[2]) == 'p'))
    158 ff_time.tm_mon = 8;
    159 if ((tolower(tmp_mon[0]) == 'o') &&
    160 (tolower(tmp_mon[1]) == 'c') &&
    161 (tolower(tmp_mon[2]) == 't'))
    162 ff_time.tm_mon = 9;
    163 if ((tolower(tmp_mon[0]) == 'n') &&
    164 (tolower(tmp_mon[1]) == 'o') &&
    165 (tolower(tmp_mon[2]) == 'v'))
    166 ff_time.tm_mon = 10;
    167 if ((tolower(tmp_mon[0]) == 'd') &&
    168 (tolower(tmp_mon[1]) == 'e') &&
    169 (tolower(tmp_mon[2]) == 'c'))
    170 ff_time.tm_mon = 11;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just because a tolower() function is called on a character should not imply that the character is already in upper case.

    The code you posted above simply turns a 3 character month into an index value of 0 to 11 (and is quite inefficient too)

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    In addition to Todd's comment above, that bunch of stuff there doesn't actually convert the case of anything. tolower() and toupper() only return the desired case, since there is no assignment back to the array, the original character remains unaffected.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Ok so I need to find...

    I will need to find what ff_time.tm_mon = 0,1,2.... means? I am thinking based on your replys I should find in a header file something stating if ff_time.tm_mon = 0
    0 is somehow = jan

    Is this where you guys would start searching?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    This looks to me like it's just checking to make sure the month designations are right and then assigning some value to them. I haven't been programming for long, so please tell me, are those ff_time and tm_mon things yours or preprogrammed system commands? Or is ff_time a struct, that's what the dot looks like. But if that's so, you're reassigning a new value to tm_mon in ff_time every time the month designators are right.

    So, I guess my question is, what's going on here?

    -CG20

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CodeGeek20 View Post
    This looks to me like it's just checking to make sure the month designations are right and then assigning some value to them. I haven't been programming for long, so please tell me, are those ff_time and tm_mon things yours or preprogrammed system commands? Or is ff_time a struct, that's what the dot looks like. But if that's so, you're reassigning a new value to tm_mon in ff_time every time the month designators are right.

    So, I guess my question is, what's going on here?

    -CG20
    Todd already explained that it assigns tm_mon with the value of month, starting with 0 for "jan".

    the ff_time is a struct, probably the one described halfway down this page;
    http://www.hmug.org/man/3/ctime.php

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Little more info

    ff_time is a struct:

    [code]
    77 struct tm ff_time;
    78
    79 int rt_stat,tmp_year;
    80 char tmp_mon[4], tmp_date[13],tmp_time[9];
    [\code]

    And the purpose of this function is below:
    15 * The ssw_igtoit (Ingres display time to integer time) procedure returns
    16 * the long integer format time for the character display format
    17 * DD-MMM-YYYY HH:MM:SS input time.


    This function is being called from a 4GL file and changing my date from 12-May-2008 00:00:00 to 12-may-2008 00:00:00 and affecting other programs
    Last edited by cjohnman; 05-12-2008 at 09:08 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally would do something like this:
    Code:
    int i;
    int found = 0;
    const char *month[] = { "jan", "feb", "mar" ...  };
    for(i = 0; i < 3; i++) 
       tmp_mon[i] = tolower(tmp_mon);
    for(i = 0; i < 12; i++)
    {
       if (strcmp(month[i], tmp_mon)== 0)
       {
          found = 1;
          break;
       }
    }
    if (found)
       ff_time.tm_mon = i;
    else error("invalid month");   // You need to deal with this case somehow.
    This is probably marginally slower, but it's simpler to understand what it does.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    I guess this stuff is preprogammed then, forgive me, I haven't worked with calendar programs.

    I would, however, write a function to go to the 0 element of tmp_mon, and set it to upper case. You will probably also have to write your own uppercase function if there's isn't a header for such things. I may be wrong, but from what I gather from the code, I think that may work.

    If I can think of a function to do this I will post it. (And it might work )

    -CG20

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by matsp View Post
    Code:
    if (found)
    What's if(found) for? There's no conditional check.

    -CG20

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CodeGeek20 View Post
    What's if(found) for? There's no conditional check.

    -CG20
    ?? It checks if found is true (which is "non-zero" in C). In this case, found is initialized to zero, and when we find a matching month-name, we set found = 1, and break the loop. So i will be the month number (starting from zero). If found is not true, the month name wasn't correct, so we need to deal with that in some way... [That probably shouldn't happen if the data is coming from a database, but...]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    This date is coming from a database

    This date comes from a database column called entry_dt_tm and then the 4gl file trys to reset this date based on certain events. Thats where the problem lies because it just seems to call this function and then my date is out of proper case. I will look into this more and post back later. Thanks for all the help thus far...

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Carl, you could code up an UPDATE clause to fix all the dates. You'll need to use LIKE and substring, but it can be done.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Was thinkin that

    Todd thanks for your help. I was thinkin that would work but I have been told to find where the problem originates and correct it there. Are you thinking I should do an update in the 4GL file where my date is getting changed? I suppose I could try that. I just would like to find where it is altered and stop it there. Anyway I will look into your advice. You are usually rite. Thanks.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    There are two things to do.

    1) Fix it at the source so it doesn't happen again.
    2) Fix the data already in the database. This is what I was referring to.

    Probably best to fix it at the source first, then fix the data in the database.

    I'm not sure of the capabilities of your RDBMS, but perhaps you can define a Trigger to alter the incoming data (as it is INSERTED or LOADED) to be the proper format, thereby perhaps satisfying the requirement of "fixing it at the source"

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM