Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ** Name: Dr.* King
- ** Date: 02/08/11
- ** File: parse.c
- ** Desc: This file takes some input interactively from the user, and outputs
- ** relevant data based on the input commands and parameters. One of the
- ** more interesting aspects of this program is that string tokenizing
- ** is not allowed via standard libraries. So, I write my own.
- **/
- /**
- ** Includes
- **/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- /**
- ** Function prototypes
- **/
- // tokenizing functions
- char** strpart( char*, char, int* );
- int strcount( char*, char );
- char* strcut( char*, int, int );
- // helper functions
- char* l_getline();
- void helper( char* );
- // "built-in" functions
- void dumper( char*, char* );
- void assembler( char* );
- /**
- ** Main
- **/
- int main()
- {
- char** tokens;
- int arrsize;
- do
- {
- tokens = strpart( l_getline(), ' ', &arrsize );
- if( arrsize == 0 )
- {
- helper( NULL );
- }
- else
- {
- if( !strcmp( tokens[0], "dump" ) )
- {
- if( arrsize != 3 )
- {
- printf( "Too %s parameters!\n", ((arrsize < 3)?"few":"many") );
- helper( "dump" );
- }
- else
- dumper( tokens[1], tokens[2] );
- }
- else if( !strcmp( tokens[0], "assemble" )
- {
- if( len != 2 )
- {
- printf( "Too %s parameters!\n", ((arrsize < 2)?"few":"many") );
- helper( "assemble" );
- }
- else
- assembler( token[1] );
- }
- // etc etc.
- }
- } while( strcmp( tokens[0], "exit" ) );
- return 0;
- }
- /**
- ** Function definitions.
- **/
- /*
- * Tokenizing functions
- */
- char** strpart( char* in, char delim, int* arrsize )
- {
- *arrsize = strcount( in, delim )+1;
- char** out = (char**)malloc( sizeof(char*)*(*arrsize) );
- if( strlen( in ) == 0 )
- {
- free( out );
- *arrsize = 0;
- out = (char**)malloc( sizeof(char*)*1 );
- out[0] = "\0";
- return out;
- }
- int i = 0;
- int j = 0;
- int c = 0;
- for( i = 0; i < strlen( in ); i++ )
- {
- if( in[i] == delim )
- {
- out[c++] = strcut( in, j, i );
- j = i;
- }
- else if( i == strlen(in)-1 )
- out[c++] = strcut( in, j, i+1 );
- }
- return out;
- }
- int strcount( char* in, char delim )
- {
- int i =0;
- int out = 0;
- for( i = 0; i < strlen( in ); i++ )
- if( in[i] == delim )
- out++;
- return out;
- }
- char* strcut( char* in, int s, int e )
- {
- int i, j = 0;
- int w = e-s;
- char* out = (char*)malloc( sizeof(char)*(w+1) );
- for( i = s; i < e; i++ )
- out[j++] = in[i];
- out[w] = '\0';
- return out;
- }
- /**
- ** Helper functions
- **/
- char* l_getline()
- {
- char in;
- char* out = NULL;
- int c = 0; // need at least one for the null terminator
- while( (in = getchar()) != '\n' )
- {
- out = (char*)realloc( out, sizeof(char)*(++c) );
- out[c-1] = in;
- }
- if( c > 0 )
- out[c] = '\0';
- else
- {
- out = (char*)malloc( sizeof(char) );
- out[0] = '\0';
- }
- return out;
- }
- void helper( char* l )
- {
- if( l == NULL )
- printf( "I r useful\n" );
- }
- /**
- ** "built-in" functions
- **/
- void dumper( char* a, char* b )
- {
- //TODO:
- }
- void assembler( char* a )
- {
- //TODO:
- }
- // * (Not a real doctor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement