Skip to content

Instantly share code, notes, and snippets.

@otaks
Created June 26, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaks/d5d5f0ee2bf8585e5463b9323ca1b2ab to your computer and use it in GitHub Desktop.
Save otaks/d5d5f0ee2bf8585e5463b9323ca1b2ab to your computer and use it in GitHub Desktop.
option - object oriented
#include <stdio.h>
#include "option.h"
option opt; //グローバル空間に置くことで他モジュールからも参照可能
int main( int argc, char *argv[] ) {
opt = option_create( argc, argv );
printf( "option_is_ver( opt ) = %d\n", option_is_ver( opt ) );
printf( "option_is_help( opt ) = %d\n", option_is_help( opt ) );
option_delete( opt );
return 0;
}
#include <stdlib.h>
#include <string.h>
#include "option.h"
static analyze_option( option t, int argc, char *argv[] );
struct _option{
bool ver;
bool help;
};
option option_create( int argc, char *argv[] ) {
option t = calloc( sizeof( struct _option ), 1 );
analyze_option( t, argc, argv );
return t;
}
static analyze_option( option t, int argc, char *argv[] ) {
for( int i = 0; i < argc; i++ ) {
if( strcmp( argv[ i ], "-ver" ) == 0 ) {
t->ver = TRUE;
}else if( strcmp( argv[ i ], "-help" ) == 0 ) {
t->help = TRUE;
}
}
}
void option_delete( option t ) {
free( t );
}
bool option_is_ver( option t ) {
return ( t->ver == TRUE ? TRUE : FALSE );
}
bool option_is_help( option t ) {
return ( t->help == TRUE ? TRUE : FALSE );
}
#pragma once
typedef enum _bool {
FALSE = 0,
TRUE
}bool;
typedef struct _option* option;
option option_create( int argc, char *argv[] );
void option_delete( option t );
bool option_is_ver( option t );
bool option_is_help( option t );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment