The Stack Overflow podcast is back! Listen to an interview with our new CEO.
6 deleted 1 character in body
source | link

I have been trying to write a sample code block to remove multiple backward or forward slashes from a path. E (e.g. //a//b//a//b should be /a/b;/a/b; on windows c:\a\b\\cWindows, c:\\a\\b\\\c should be c:\a\b\cc:\a\b\c).

  1. Code must be platform independent
  2. Should work in all cases  (or report a proper error)
  3. It should be readable and maintainable
  4. It shouldn't be over-complex and shouldn't have errors.

Please provide point-wise comments for the following code sample (Youyou may ignore main functionmain and assume that pathpath is Not NULLnot NULL - it is just for demonstration purposepurposes). Or else you may please provide a better mechanism for the same.  

Thanks!

I have been trying to write a sample code block to remove multiple backward or forward slashes from a path. E.g. //a//b should be /a/b; on windows c:\a\b\\c should be c:\a\b\c

  1. Code must be platform independent
  2. Should work in all cases(or report proper error)
  3. It should be readable and maintainable
  4. It shouldn't be over-complex and shouldn't have errors.

Please provide point-wise comments for the following code sample (You may ignore main function and assume that path is Not NULL- it is just for demonstration purpose). Or else you may please provide a better mechanism for same.  

Thanks!

I have been trying to write a sample code block to remove multiple backward or forward slashes from a path (e.g. //a//b should be /a/b; on Windows, c:\\a\\b\\\c should be c:\a\b\c).

  1. Code must be platform independent
  2. Should work in all cases  (or report a proper error)
  3. It should be readable and maintainable
  4. It shouldn't be over-complex and shouldn't have errors

Please provide point-wise comments for the following code sample (you may ignore main and assume that path is not NULL - it is just for demonstration purposes). Or else you may provide a better mechanism for the same.

    Tweeted twitter.com/StackCodeReview/status/789972069895331840
5 added explanation.
source | link

Please provide point-wise comments for the following code sample (You may ignore main function and assume that path is Not NULL- it is just for demonstration purpose). Or else you may please provide a better mechanism for same.

Please provide point-wise comments for the following code sample. Or else you may please provide a better mechanism for same.

Please provide point-wise comments for the following code sample (You may ignore main function and assume that path is Not NULL- it is just for demonstration purpose). Or else you may please provide a better mechanism for same.

4 added example usage.
source | link
    void
    die( int error_code, char* message )
    {
        fprintf( stderr, message );
        exit( error_code );
    }

    /*Following function removes duplicate slashes from a given path*/
    char*
    validate_path ( char* path )
    {
        char* path_copy = path;
        int dup_flag = 0;
        char* path_without_dup_slash = (char*)malloc(strlen(path)+1);
        if ( path_without_dup_slash == NULL ) {
             die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
        }
        char* copy_pwds = path_without_dup_slash;
        /* Travel through given path */
        while( *path_copy != '\0' ) {
              /* If there is '\' or '/' then copy a single '/'
               * , ignore others until a real char comes */
               if ( (( *path_copy == '\\') || ( *path_copy == '/' )) && 
                    (dup_flag == 0) ) {
                    *copy_pwds = *path_copy;
                    dup_flag = 1;
                    copy_pwds++;
                }else if (( *path_copy != '\\') && ( *path_copy != '/' ))  {
                    *copy_pwds = *path_copy;
                    dup_flag = 0;
                    copy_pwds++;
                }
                path_copy++;
         }
         *copy_pwds = '\0';
         path_without_dup_slash = (char *)realloc( path_without_dup_slash, 
                                  strlen( path_without_dup_slash ) + 1 );
         if ( path_without_dup_slash == NULL ) {
               die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
         }
         return path_without_dup_slash;
    }
    /*You please ignore the parts written in main - it is just for demonstration purpose. */
    int
    main( void ) {
         char* bad_path_1 = malloc(100);
         char* bad_path_2 = malloc(100);
         char* path_1,p1 = bad_path_1;
         char* p2 = bad_path_2;
         char* path_1;
         char* path_2;
         bad_path_1 = "//a//b//c";
         bad_path_2 = "c:\\a\\b\\c";\\\\a\\\\b\\\\c";
         path_1 = validate_path(bad_path_1);
         printf( "Good path:%s Bad Path:%s\n",path_1,bad_path_1);
         path_2 = validate_path(bad_path_2);
         printf( "Good path:%s Bad Path:%s\n",path_2,bad_path_2);
         free(path_1);
         free(path_2);
         free(bad_path_1p1);
         free(bad_path_2p2);
         return EXIT_SUCCESS; 

     }
    void
    die( int error_code, char* message )
    {
        fprintf( stderr, message );
        exit( error_code );
    }

    /*Following function removes duplicate slashes from a given path*/
    char*
    validate_path ( char* path )
    {
        char* path_copy = path;
        int dup_flag = 0;
        char* path_without_dup_slash = (char*)malloc(strlen(path)+1);
        if ( path_without_dup_slash == NULL ) {
             die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
        }
        char* copy_pwds = path_without_dup_slash;
        /* Travel through given path */
        while( *path_copy != '\0' ) {
              /* If there is '\' or '/' then copy a single '/'
               * , ignore others until a real char comes */
               if ( (( *path_copy == '\\') || ( *path_copy == '/' )) && 
                    (dup_flag == 0) ) {
                    *copy_pwds = *path_copy;
                    dup_flag = 1;
                    copy_pwds++;
                }else if (( *path_copy != '\\') && ( *path_copy != '/' ))  {
                    *copy_pwds = *path_copy;
                    dup_flag = 0;
                    copy_pwds++;
                }
                path_copy++;
         }
         *copy_pwds = '\0';
         path_without_dup_slash = (char *)realloc( path_without_dup_slash, 
                                  strlen( path_without_dup_slash ) + 1 );
         if ( path_without_dup_slash == NULL ) {
               die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
         }
         return path_without_dup_slash;
    }
    int
    main( void ) {
         char* bad_path_1 = malloc(100);
         char* bad_path_2 = malloc(100);
         char* path_1, path_2;
         bad_path_1 = "//a//b//c";
         bad_path_2 = "c:\\a\\b\\c";
         path_1 = validate_path(bad_path_1);
         printf( "Good path:%s Bad Path:%s\n",path_1,bad_path_1);
         path_2 = validate_path(bad_path_2);
         printf( "Good path:%s Bad Path:%s\n",path_2,bad_path_2);
         free(path_1);
         free(path_2);
         free(bad_path_1);
         free(bad_path_2);
         return EXIT_SUCCESS;
     }
    void
    die( int error_code, char* message )
    {
        fprintf( stderr, message );
        exit( error_code );
    }

    /*Following function removes duplicate slashes from a given path*/
    char*
    validate_path ( char* path )
    {
        char* path_copy = path;
        int dup_flag = 0;
        char* path_without_dup_slash = (char*)malloc(strlen(path)+1);
        if ( path_without_dup_slash == NULL ) {
             die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
        }
        char* copy_pwds = path_without_dup_slash;
        /* Travel through given path */
        while( *path_copy != '\0' ) {
              /* If there is '\' or '/' then copy a single '/'
               * , ignore others until a real char comes */
               if ( (( *path_copy == '\\') || ( *path_copy == '/' )) && 
                    (dup_flag == 0) ) {
                    *copy_pwds = *path_copy;
                    dup_flag = 1;
                    copy_pwds++;
                }else if (( *path_copy != '\\') && ( *path_copy != '/' ))  {
                    *copy_pwds = *path_copy;
                    dup_flag = 0;
                    copy_pwds++;
                }
                path_copy++;
         }
         *copy_pwds = '\0';
         path_without_dup_slash = (char *)realloc( path_without_dup_slash, 
                                  strlen( path_without_dup_slash ) + 1 );
         if ( path_without_dup_slash == NULL ) {
               die( EXIT_FAILURE, "Failed to allocate memory for processing path.");
         }
         return path_without_dup_slash;
    }
    /*You please ignore the parts written in main - it is just for demonstration purpose. */
    int
    main( void ) {
         char* bad_path_1 = malloc(100);
         char* bad_path_2 = malloc(100);
         char* p1 = bad_path_1;
         char* p2 = bad_path_2;
         char* path_1;
         char* path_2;
         bad_path_1 = "//a//b//c";
         bad_path_2 = "c:\\\\a\\\\b\\\\c";
         path_1 = validate_path(bad_path_1);
         printf( "Good path:%s Bad Path:%s\n",path_1,bad_path_1);
         path_2 = validate_path(bad_path_2);
         printf( "Good path:%s Bad Path:%s\n",path_2,bad_path_2);
         free(path_1);
         free(path_2);
         free(p1);
         free(p2);
         return EXIT_SUCCESS; 

     }
3 added correct function name.
source | link
2 added example usage.
source | link
1
source | link