📄 vk_popt.cpp
字号:
if ( con->os->nextArg ) { sprintf( arg_val, "%s", con->os->nextArg ); } } /* end if ! VkOPTION::ARG_NONE */ if ( opt->optKey >= 0 ) { /* is-a leaf */ done = 1; } if ( (con->finalArgvCount + 2) >= (con->finalArgvAlloced) ) { con->finalArgvAlloced += 10; con->finalArgv = (const char**)realloc(con->finalArgv, sizeof(*con->finalArgv) * con->finalArgvAlloced); } if ( con->finalArgv != NULL ) { char *s = (char*)malloc((opt->longFlag ? strlen(opt->longFlag) : 0) + 3); if ( s != NULL ) { if ( opt->longFlag ) { sprintf(s, "--%s", opt->longFlag); } else { sprintf(s, "-%c", opt->shortFlag); } con->finalArgv[con->finalArgvCount++] = s; } else con->finalArgv[con->finalArgvCount++] = NULL; } if ((opt->arg == NULL/* leaf */) && (opt->argType != VkOPTION::ARG_NONE)) { if (con->finalArgv != NULL && con->os->nextArg) { char* s = (char*)malloc( strlen(con->os->nextArg)+1 ); if (s == NULL) { vkPrintErr("virtual memory exhausted."); exit(EXIT_FAILURE); } else { con->finalArgv[con->finalArgvCount++] = strcpy( s, con->os->nextArg ); } } } } /* end while ( !done ) */ // vk_assert( opt != NULL ); *opt_ret = opt; return PARSED_OK;}vkPoptContext vkPoptFreeContext( vkPoptContext con ){ if ( con == NULL ) return con; /* poptResetContext( con ); */ int i; while ( con->os > con->optionStack ) { vkCleanOSE(con->os--); } con->os->nextCharArg = NULL; con->os->nextArg = NULL; con->os->next = 1; /* skip argv[0] */ con->numLeftovers = 0; con->nextLeftover = 0; con->restLeftover = 0; if ( con->finalArgv != NULL ) for (i = 0; i < con->finalArgvCount; i++) con->finalArgv[i] = (const char*)_free(con->finalArgv[i]); con->finalArgvCount = 0; con->leftovers = (const char**)_free( con->leftovers ); con->finalArgv = (const char**)_free( con->finalArgv ); con = (vkPoptContext)_free( con ); return con;}const char * vkPoptBadOption( vkPoptContext con ){ struct optionStackEntry * os = NULL; if ( con != NULL ) os = con->optionStack; return ( os && os->argv ? os->argv[os->next - 1] : NULL );}/*------ help printing stuff --------------------------------*//* set in poptPrintHelp */static int leftColWidth = -1;static const char * vkGetHelpDesc( const vkPoptOption * opt ){ /* if ARG_NONE: no arguments to describe */ if ( opt->argType == VkOPTION::ARG_NONE ) return NULL; if ( opt->helpdesc ) return opt->helpdesc; return NULL;}/* called from vkTableHelp() for leaf table */static void vkSingleOptionHelp( FILE * fp, const vkPoptOption * opt ){ int indentLength = leftColWidth + 2; int lineLength = 80 - indentLength; const char * help = opt->helptxt; const char * helpdesc = vkGetHelpDesc( opt ); int helpLength; char * defs = NULL; char * left; int nb = leftColWidth + 1; /* make sure there's more than enough room in target buffer. */ if ( opt->longFlag ) nb += strlen( opt->longFlag ); if ( helpdesc ) nb += strlen( helpdesc ); left = (char*)malloc( nb ); if ( left == NULL ) return; left[0] = '\0'; left[leftColWidth] = '\0'; if ( opt->longFlag && opt->shortFlag ) { sprintf( left, "-%c, --%s", opt->shortFlag, opt->longFlag); } else if (opt->shortFlag != '\0') { sprintf( left, "-%c", opt->shortFlag); } else if (opt->longFlag) { sprintf( left, "--%s", opt->longFlag ); } if ( !*left ) { goto out; } if ( helpdesc ) { char * le = left + strlen( left ); *le++ = ' '; strcpy( le, helpdesc ); le += strlen(le); *le = '\0'; } if ( help ) { fprintf( fp," %-*s", leftColWidth, left ); } else { fprintf( fp," %s\n", left ); goto out; } left = (char*)_free(left); if ( defs ) { help = defs; defs = NULL; } helpLength = strlen( help ); while ( helpLength > lineLength ) { const char * ch; char format[100]; ch = help + lineLength - 1; while ( ch > help && !isspace(*ch) ) ch--; if ( ch == help ) break; /* give up */ while ( ch > (help + 1) && isspace(*ch) ) ch--; ch++; sprintf( format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength ); fprintf( fp, format, help, " " ); help = ch; while ( isspace(*help) && *help ) help++; helpLength = strlen( help ); } if ( helpLength ) fprintf( fp, "%s\n", help ); out: defs = (char*)_free(defs); left = (char*)_free(left);}static int vkMaxArgWidth( const vkPoptOption * opt ){ int max = 0; int len = 0; const char * s; if ( opt == NULL ) return 0; for (; opt->longFlag || opt->shortFlag || opt->arg; opt++ ) { if ( opt->arg != NULL ) { /* is-a table */ /* recurse on included sub-tables. */ len = vkMaxArgWidth( opt->arg ); if ( len > max ) max = len; } else { /* is-a leaf */ len = sizeof(" ") - 1; if ( opt->shortFlag != '\0' ) len += sizeof("-X")-1; if ( opt->shortFlag != '\0' && opt->longFlag ) len += sizeof(", ")-1; if (opt->longFlag) { len += sizeof("--") - 1; len += strlen( opt->longFlag ); } s = vkGetHelpDesc( opt ); if ( s ) len += sizeof("=") - 1 + strlen( s ); if ( len > max ) max = len; } } return max;}/* this version prints nested tables first. swap 'em over if this isn't the behaviour you want. */static void vkTableHelp( FILE * fp, const vkPoptOption * opt ){ if ( opt == NULL ) return; /* recurse over all tables */ for (; opt->longFlag || opt->shortFlag || opt->arg; opt++) { if ( opt->arg != NULL ) { /* is-a table */ /* print table title */ fprintf( fp, "\n%s options:\n", opt->helptxt ); /* recurse on included sub-tables. */ vkTableHelp( fp, opt->arg ); } else { /* is-a leaf */ /* print options */ vkSingleOptionHelp( fp, opt ); } }}/* if tableName == NULL, recurses over all tables; else just prints contents of the specified table. */void vkPoptPrintHelp( vkPoptContext con, FILE * fp, const char * tableName ){ const char * fn; fprintf( fp, "\nUsage:" ); fn = con->optionStack->argv[0]; if ( fn != NULL ) { if ( strchr(fn, '/') ) fn = strrchr( fn, '/' ) + 1; fprintf( fp, " %s", fn ); } fprintf( fp, " %s", "[valkyrie-opts] [valgrind-opts] [prog-and-args]\n" ); leftColWidth = vkMaxArgWidth( con->options ); if ( tableName == NULL ) { /* print all tables */ vkTableHelp( fp, con->options ); } else { /* trawl through con->options till we find the right table */ const vkPoptOption * opt; for ( opt = con->options; (opt != NULL) && (opt->helptxt != NULL); opt++ ) { if ( strcmp( opt->helptxt, tableName ) == 0 ) { break; } } if ( (opt != NULL) && (opt->helptxt) != NULL ) { if ( opt->helptxt ) fprintf( fp, "\n%s options:\n", opt->helptxt ); else vkPrintErr("Error: vkPoptPrintHelp(): No match found for table '%s'.", tableName); vkTableHelp( fp, opt->arg ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -