home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 10 / amigaformatcd10.iso / -in_the_mag- / html_tutorial / o_parse.cpp < prev    next >
C/C++ Source or Header  |  1996-09-03  |  3KB  |  153 lines

  1. //
  2. // Split the components of a CGI result string into
  3. //  individual sub strings
  4. //
  5. // For example the string
  6. //   name=Your+name&action=%2B10%25&log=~mas/log
  7. //
  8. // is composed of two named elements:
  9. //
  10. //     Element    String associated with element
  11. //     name       Your name
  12. //     action     +10%
  13. //     log        /usr/staff/mas/log
  14. //
  15. // (C) M.A.Smith University of Brighton
  16. // Permission is granted to use this code
  17. //   provided this declaration and copyright notice remains intact.
  18. // 26 August 1995
  19. //
  20. //
  21. // I m p l e m e n t a t i o n
  22.  
  23. #ifndef CLASS_PARSE_IMP
  24. #define CLASS_PARSE_IMP
  25.  
  26. #define "parse.h"
  27.  
  28. #include <string.h>
  29. #include <ctype.h>
  30. #ifndef NO_MAP
  31. # include <pwd.h>
  32. #endif
  33.  
  34. Parse::Parse( char list[] )
  35. {
  36.   the_str = NULL;
  37.   set( list );
  38. }
  39.  
  40. Parse::~Parse()
  41. {
  42.   if ( the_str != NULL ) {    // Release storage
  43.     delete the_str;
  44.   }
  45. }
  46.  
  47. void Parse::set( char list[] )
  48. {
  49.   if ( the_str != NULL ) {          // Release storage
  50.     delete the_str;
  51.   }
  52.   the_length = strlen( list );      // Length of string
  53.   the_str = new char[the_length+1]; // Allocate area
  54.   strcpy( the_str, list );          // copy to
  55. }
  56.  
  57. char *Parse::get_item( char name[], bool file )
  58. {
  59.   int len = strlen( name );
  60.   for( int i=0; i<the_length-len; i++ )
  61.   {
  62.     if ( the_str[i] == name[0]  &&
  63.          strncmp( &the_str[i], name, len ) == 0 )
  64.     {
  65.       if ( the_str[i+len] == '=' )
  66.       {
  67.         int start = i+len+1; int j = start;
  68.         while ( the_str[j] != SEP && the_str[j] != '\0' ) j++;
  69.         int str_len = j-start;
  70.         char *mes = new char[ str_len+1 ];
  71.         strncpy( mes, &the_str[start], str_len );
  72.         mes[str_len] = '\0';
  73.         remove_escape( mes );
  74. #       ifndef NO_MAP
  75.         return file ? map_uname(mes) : mes;
  76. #       else
  77.         return file ? mes : mes;
  78. #       endif
  79.       }
  80.     }
  81.   }
  82.   return NULL;
  83. }
  84.  
  85. char *Parse::get_item_n( char name[], bool file )
  86. {
  87.   char *res = get_item( name, file );
  88.   return res == NULL ? "" : res;
  89. }
  90.  
  91. void Parse::remove_escape(char str[])
  92. {
  93.   char * from = &str[0];
  94.   char * to   = &str[0];
  95.   while ( *from != '\0' )
  96.   {
  97.     char ch = *from++;
  98.     switch ( ch )
  99.     {
  100.       case '%' :
  101.     ch = (hex(*from++)<<4) | hex(*from++);
  102.         break;
  103.       case '+' :
  104.     ch = ' '; break;
  105.     }
  106.     *to++ = ch;
  107.   }
  108.   *to = '\0';
  109. }
  110.  
  111. int Parse::hex( char c )
  112. {
  113.   if ( isdigit( c ) )
  114.     return c-'0';
  115.   if ( isalpha( c ) )
  116.     return tolower(c)-'a'+10;
  117.   return 0;
  118. }
  119.  
  120. char* Parse::map_uname( char *path )
  121. {
  122. #ifndef NO_MAP
  123.   if ( path[0] == '~' )
  124.   {
  125.      char uname[255];
  126.      char *rest = &path[1], *p = &uname[0];
  127.  
  128.      while ( *rest != '/' && *rest != '\0' ) {
  129.        *p++ = *rest++;
  130.      }
  131.      *p = '\0';
  132.      char *root = uname;              // If fails
  133.      passwd *pw = getpwnam( uname );  // Structure about user
  134.      if ( pw != NULL )
  135.      {
  136.        root = pw->pw_dir;             // Users home directory
  137.      }
  138.      int len_root = strlen(root);
  139.      int len_path  = len_root + strlen(rest);
  140.      char *new_path = new char[len_path+1];
  141.  
  142.      strcpy( &new_path[0], root );       // Build abs path
  143.      strcpy( &new_path[len_root], rest );
  144.      new_path[ len_path ] = '\0';
  145.      return new_path;
  146.   } else {
  147.     return path;
  148.   }
  149. #endif
  150.  return path;
  151. }
  152. #endif
  153.