Created
June 26, 2016 00:14
-
-
Save otaks/d5d5f0ee2bf8585e5463b9323ca1b2ab to your computer and use it in GitHub Desktop.
option - object oriented
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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