Thread: Really basic string operation

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Wink Really basic string operation

    I am having a little trouble in writing a sort function! ...I need to sort an array containing file / directory names for my personal ls command. At the moment, it sorts the array into alphabetical order, which is perfect, however, if i then add the switch -a the program ouputs all files beginning with . alphabetically and then all files with no leading . alphabetically, example:

    Code:
    [root@localhost shell]# ./myls -la /tmp
    list of contents for: /tmp
    -rwxrwxrwx     13 root     root          4096 Sun Nov 27 21:53:55 2005 .
    -rwxr-xr-x     20 root     root          4096 Sun Nov 27 20:06:26 2005 ..
    -rwx------      1 root     nobody           0 Sun Nov 27 20:07:26 2005 .fam_socket
    -rwxrwxrwx      2 xfs      xfs           4096 Sun Nov 27 20:06:59 2005 .font-unix
    -rw-rw-rw-      1 root     root             0 Sun Nov 27 20:07:01 2005 .gdm_socket
    -rwxrwxrwx      2 root     root          4096 Sun Nov 27 20:07:24 2005 .ICE-unix
    -r--r--r--      1 root     root            11 Sun Nov 27 20:07:01 2005 .X0-lock
    -rwxrwxrwx      2 root     root          4096 Sun Nov 27 20:07:01 2005 .X11-unix
    -rwx------      2 root     root          4096 Wed Nov  9 16:57:21 2005 mcop-root
    -rwx------      2 matt     matt          4096 Tue Nov  8 21:12:41 2005 orbit-matt
    -rwx------      2 root     root         12288 Sun Nov 27 20:08:50 2005 orbit-root
    -rwx------      2 root     root          4096 Mon Nov 21 18:08:18 2005 plugtmp
    -rwx------      2 root     root          4096 Fri Nov 25 12:27:53 2005 plugtmp-1
    -rwx------      2 root     root          4096 Sun Nov 27 21:16:46 2005 plugtmp-2
    -rwx------      2 root     root          4096 Mon Nov  7 13:35:34 2005 ssh-XX9iLygl
    -rwx------      2 root     root          4096 Sun Nov 27 20:07:23 2005 ssh-XXNXMs4V
    Basically, i need to add to my sort compare function an operation that ignores leading dots when sorting, but also a way to put the leading dots back onto the names once sorting is complete!!!

    Does anyone know how to ignore the leading char when copying or comparing a string, because i cannot find it anywhere!

    Code:
    /**********************************************************************************************/
    /**                                                                                          **/
    /** FUNCTION           : sortCompare                                                         **/
    /** DESCRIPTION        : function to compare 2 strings and returns an integer less than,     **/
    /**                      equal to, or greater than zero if str1 is found, respectively, to   **/
    /**                      be less than, to mathc, or be greater than str2.                    **/
    /**                      NOTE: This function ignores leading zeros and case                  **/
    /** PARAMETERS         : a pointer to the first string to be compared                        **/
    /**                      a pointer to the second string to be compared                       **/
    /** AUTHOR             : Matt Conway (03500546)                                              **/
    /** DATE               : 21/11/2005                                                          **/
    /**                                                                                          **/
    /**********************************************************************************************/
    int sortCompare(const void *str1, const void *str2){
    
      char tmpStr1[strlen(str1)];
      char tmpStr2[strlen(str2)];
    
      /* Check to see if str1 name is not . */
      if (!strcmp(str1, ".") == 0){
        /* so check for leading zero on str1 */
        if (strncmp(str1, ".", 1) == 0){
          /* copy str1 to tmpStr1 ignoring leading . */
        }    
      }
      /* DO THE SAME FOR str2 */
    
      /* return the result of the copy */
      return strcasecmp(str1,str2);
    
    }
    I know that the dynamic array assignment is bad programming, but i will change this to malloc space once it is in working order!!!!!!

    Thanks for any ideas or hints!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    int sortCompare(const void *str1, const void *str2){
      const char *p1 = str1;
      const char *p2 = str2;
      if ( *p1 == '.' ) {
        if ( *p2 == '.' ) {
          return strcmp ( p1, p2 );
        } else {
          return -1;  /* any dot comes before any non-dot */
        }
      } else
      if ( *p2 == '.' ) {
        /* something similar */
      } else {
        return strcmp ( p1, p2 );
      }
    }

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Code:
    /* if both strings have leading '.'s */
    return strcmp( &((char *)name1)[1], &((char *)name2)[1]);
    No need for copying.

    And just use conditions similar to Salems to determine what you need to return.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Basically, i need to add to my sort compare function an operation that ignores leading dots when sorting, but also a way to put the leading dots back onto the names once sorting is complete!!!
    Code:
    int sortCompare(const void *str1, const void *str2){
      const char *p1 = str1;
      const char *p2 = str2;
      if ( *p1 == '.' )
        P1++;
      if ( *p2 == '.' )
        p2++;
      return strcmp ( p1, p2 );
    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int sortCompare(const void *str1, const void *str2)
    {
      return strcmp(*str1 == '.' ? str1 + 1 : str1, *str2 == '.' ? str2 + 1 : str2);
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by itsme86
    Code:
    int sortCompare(const void *str1, const void *str2)
    {
      return strcmp(*str1 == '.' ? str1 + 1 : str1, *str2 == '.' ? str2 + 1 : str2);
    }
    Bad compiler!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yeah, umm...change the voids to chars. I copied cdave's parameter list for some reason.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM