Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. /**
  2. ** Name: Dr.* King
  3. ** Date: 02/08/11
  4. ** File: parse.c
  5. ** Desc: This file takes some input interactively from the user, and outputs
  6. **       relevant data based on the input commands and parameters. One of the
  7. **       more interesting aspects of this program is that string tokenizing
  8. **       is not allowed via standard libraries. So, I write my own.
  9. **/
  10.  
  11. /**
  12. ** Includes
  13. **/
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. /**
  20. ** Function prototypes
  21. **/
  22. // tokenizing functions
  23. char** strpart( char*, char, int* );
  24. int strcount( char*, char );
  25. char* strcut( char*, int, int );
  26.  
  27. // helper functions
  28. char* l_getline();
  29. void helper( char* );
  30.  
  31. // "built-in" functions
  32. void dumper( char*, char* );
  33. void assembler( char* );
  34.  
  35. /**
  36. ** Main
  37. **/
  38. int main()
  39. {
  40.     char** tokens;
  41.     int arrsize;       
  42.  
  43.    
  44.     do
  45.     {
  46.         tokens = strpart( l_getline(), ' ', &arrsize );
  47.        
  48.         if( arrsize == 0 )
  49.         {
  50.             helper( NULL );
  51.         }
  52.         else
  53.         {
  54.             if( !strcmp( tokens[0], "dump" ) )
  55.             {
  56.                 if( arrsize != 3 )
  57.                 {
  58.                     printf( "Too %s parameters!\n", ((arrsize < 3)?"few":"many") );
  59.                     helper( "dump" );
  60.                 }
  61.                 else
  62.                     dumper( tokens[1], tokens[2] );
  63.             }
  64.             else if( !strcmp( tokens[0], "assemble" )
  65.             {
  66.                 if( len != 2 )
  67.                 {
  68.                     printf( "Too %s parameters!\n", ((arrsize < 2)?"few":"many") );
  69.                     helper( "assemble" );
  70.                 }
  71.                 else
  72.                     assembler( token[1] );
  73.             }
  74.             // etc etc.
  75.         }
  76.  
  77.        
  78.     } while( strcmp( tokens[0], "exit" ) );
  79.    
  80.     return 0;
  81. }
  82.  
  83. /**
  84. ** Function definitions.
  85. **/
  86.  
  87. /*
  88. * Tokenizing functions
  89. */
  90.  
  91. char** strpart( char* in, char delim, int* arrsize )
  92. {  
  93.     *arrsize = strcount( in, delim )+1;
  94.     char** out = (char**)malloc( sizeof(char*)*(*arrsize) );
  95.    
  96.     if( strlen( in ) == 0 )
  97.     {
  98.         free( out );
  99.         *arrsize = 0;
  100.         out = (char**)malloc( sizeof(char*)*1 );
  101.         out[0] = "\0";
  102.        
  103.         return out;
  104.     }
  105.    
  106.     int i = 0;
  107.     int j = 0;
  108.     int c = 0;
  109.    
  110.     for( i = 0; i < strlen( in ); i++ )
  111.     {
  112.         if( in[i] == delim )
  113.         {
  114.             out[c++] = strcut( in, j, i );
  115.             j = i;
  116.         }
  117.         else if( i == strlen(in)-1 )
  118.             out[c++] = strcut( in, j, i+1 );
  119.     }
  120.    
  121.     return out;
  122. }
  123.  
  124. int strcount( char* in, char delim )
  125. {
  126.     int i =0;
  127.     int out = 0;
  128.    
  129.     for( i = 0; i < strlen( in ); i++ )
  130.         if( in[i] == delim )
  131.             out++;
  132.    
  133.     return out;
  134. }
  135.  
  136. char* strcut( char* in, int s, int e )
  137. {
  138.     int i, j = 0;
  139.     int w = e-s;
  140.     char* out = (char*)malloc( sizeof(char)*(w+1) );
  141.    
  142.     for( i = s; i < e; i++ )
  143.         out[j++] = in[i];
  144.     out[w] = '\0';
  145.    
  146.     return out;
  147. }
  148.  
  149. /**
  150. ** Helper functions
  151. **/
  152.  
  153. char* l_getline()
  154. {  
  155.     char in;
  156.     char* out = NULL;
  157.     int c = 0; // need at least one for the null terminator
  158.    
  159.     while( (in = getchar()) != '\n' )
  160.     {
  161.         out = (char*)realloc( out, sizeof(char)*(++c) );
  162.         out[c-1] = in;
  163.     }
  164.     if( c > 0 )
  165.         out[c] = '\0';
  166.     else
  167.     {
  168.         out = (char*)malloc( sizeof(char) );
  169.         out[0] = '\0';
  170.     }
  171.    
  172.     return out;
  173. }
  174.  
  175.  
  176. void helper( char* l )
  177. {
  178.     if( l == NULL )
  179.         printf( "I r useful\n" );
  180. }
  181.  
  182. /**
  183. ** "built-in" functions
  184. **/
  185.  
  186. void dumper( char* a, char* b )
  187. {
  188.     //TODO:
  189. }
  190.  
  191. void assembler( char* a )
  192. {
  193.     //TODO:
  194. }
  195.                    
  196. // * (Not a real doctor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement