📄 save.c
字号:
/* * Get rid of the old file */ unlink( pzDir ); *p_free_name = free_dir_name; return pzDir;}static voidprintEntry( FILE * fp, tOptDesc * p, tCC* pzLA ){ /* * There is an argument. Pad the name so values line up. * Not disabled *OR* this got equivalenced to another opt, * then use current option name. * Otherwise, there must be a disablement name. */ { char const * pz; if (! DISABLED_OPT(p) || (p->optEquivIndex != NO_EQUIVALENT)) pz = p->pz_Name; else pz = p->pz_DisableName; fprintf(fp, "%-18s", pz); } /* * IF the option is numeric only, * THEN the char pointer is really the number */ if (OPTST_GET_ARGTYPE(p->fOptState) == OPARG_TYPE_NUMERIC) fprintf( fp, " %d\n", (int)(t_word)pzLA ); /* * OTHERWISE, FOR each line of the value text, ... */ else if (pzLA == NULL) fputc( '\n', fp ); else { fputc( ' ', fp ); fputc( ' ', fp ); for (;;) { tCC* pzNl = strchr( pzLA, '\n' ); /* * IF this is the last line * THEN bail and print it */ if (pzNl == NULL) break; /* * Print the continuation and the text from the current line */ (void)fwrite( pzLA, (size_t)(pzNl - pzLA), (size_t)1, fp ); pzLA = pzNl+1; /* advance the Last Arg pointer */ fputs( "\\\n", fp ); } /* * Terminate the entry */ fputs( pzLA, fp ); fputc( '\n', fp ); }}/*=export_func optionSaveFile * * what: saves the option state to a file * * arg: tOptions*, pOpts, program options descriptor * * doc: * * This routine will save the state of option processing to a file. The name * of that file can be specified with the argument to the @code{--save-opts} * option, or by appending the @code{rcfile} attribute to the last * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it * will default to @code{.@i{programname}rc}. If you wish to specify another * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro. * * err: * * If no @code{homerc} file was specified, this routine will silently return * and do nothing. If the output file cannot be created or updated, a message * will be printed to @code{stderr} and the routine will return.=*/voidoptionSaveFile( tOptions* pOpts ){ tOptDesc* pOD; int ct; FILE* fp; { int free_name = 0; tCC* pzFName = findFileName( pOpts, &free_name ); if (pzFName == NULL) return; fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG ); if (fp == NULL) { fprintf( stderr, zWarn, pOpts->pzProgName ); fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName ); if (free_name) AGFREE((void*) pzFName ); return; } if (free_name) AGFREE( (void*)pzFName ); } { char const* pz = pOpts->pzUsageTitle; fputs( "# ", fp ); do { fputc( *pz, fp ); } while (*(pz++) != '\n'); } { time_t timeVal = time( NULL ); char* pzTime = ctime( &timeVal ); fprintf( fp, zPresetFile, pzTime );#ifdef HAVE_ALLOCATED_CTIME /* * The return values for ctime(), localtime(), and gmtime() * normally point to static data that is overwritten by each call. * The test to detect allocated ctime, so we leak the memory. */ AGFREE( (void*)pzTime );#endif } /* * FOR each of the defined options, ... */ ct = pOpts->presetOptCt; pOD = pOpts->pOptDesc; do { int arg_state; tOptDesc* p; /* * IF the option has not been defined * OR it does not take an initialization value * OR it is equivalenced to another option * THEN continue (ignore it) */ if (UNUSED_OPT( pOD )) continue; if ((pOD->fOptState & (OPTST_NO_INIT|OPTST_DOCUMENT|OPTST_OMITTED)) != 0) continue; if ( (pOD->optEquivIndex != NO_EQUIVALENT) && (pOD->optEquivIndex != pOD->optIndex)) continue; /* * Set a temporary pointer to the real option description * (i.e. account for equivalencing) */ p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0) ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD; /* * IF no arguments are allowed * THEN just print the name and continue */ if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) { char const * pznm = (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name; /* * If the option was disabled and the disablement name is NULL, * then the disablement was caused by aliasing. * Use the name as the string to emit. */ if (pznm == NULL) pznm = p->pz_Name; fprintf(fp, "%s\n", pznm); continue; } arg_state = OPTST_GET_ARGTYPE(p->fOptState); switch (arg_state) { case 0: case OPARG_TYPE_NUMERIC: printEntry( fp, p, (void*)(p->optArg.argInt)); break; case OPARG_TYPE_STRING: if (p->fOptState & OPTST_STACKED) { tArgList* pAL = (tArgList*)p->optCookie; int uct = pAL->useCt; tCC** ppz = pAL->apzArgs; /* * Disallow multiple copies of disabled options. */ if (uct > 1) p->fOptState &= ~OPTST_DISABLED; while (uct-- > 0) printEntry( fp, p, *(ppz++) ); } else { printEntry( fp, p, p->optArg.argString ); } break; case OPARG_TYPE_ENUMERATION: case OPARG_TYPE_MEMBERSHIP: { uintptr_t val = p->optArg.argEnum; /* * This is a magic incantation that will convert the * bit flag values back into a string suitable for printing. */ (*(p->pOptProc))( (tOptions*)2UL, p ); printEntry( fp, p, (void*)(p->optArg.argString)); if ( (p->optArg.argString != NULL) && (arg_state != OPARG_TYPE_ENUMERATION)) { /* * set membership strings get allocated */ AGFREE( (void*)p->optArg.argString ); p->fOptState &= ~OPTST_ALLOC_ARG; } p->optArg.argEnum = val; break; } case OPARG_TYPE_BOOLEAN: printEntry( fp, p, p->optArg.argBool ? "true" : "false" ); break; default: break; /* cannot handle - skip it */ } } while ( (pOD++), (--ct > 0)); fclose( fp );}/* * Local Variables: * mode: C * c-file-style: "stroustrup" * indent-tabs-mode: nil * End: * end of autoopts/save.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -