2

私はフォーマット文字列とchar *format = "hello %d world %d"配列int array[2] = {10, 20};を持っています。これは単なる例です。配列に出力される値の数は任意であり、出力される値の数のカウントを持つ最大サイズの配列です。( int array[MAX]; int array_count)。

したがって、明らかに、そのために標準を使用することはできませんprinf(format, ..)。だから私が考えているのは、フォーマット文字列をたどり、そこからフォーマット指定子を1つだけ含む部分文字列を抽出し、その部分文字列を使用printf(sub-string, array[index]);して次のフォーマット指定子に進めることです-printfが内部的に行うことのようなものです。

したがって、特定の印刷フォーマット文字列がその文字列の最初のフォーマット指定子のオフセットを返すライブラリがあるかどうかを知りたいので、作業を節約できますか?

4

2 に答える 2

0

私は1つを持っている。ただし、ここに投稿するには少し大きいです。メールでお問い合わせください - 私のプロフィールをご覧ください。

これがヘッダーなので、何をしようとしているのかがわかります。

/*
@(#)File:           $RCSfile: printfmt.h,v $
@(#)Version:        $Revision: 2.1 $
@(#)Last changed:   $Date: 2011/07/18 16:30:54 $
@(#)Purpose:        Parse printf() format string for conversion specification
@(#)Author:         J Leffler
@(#)Copyright:      (C) JLSS 2011
@(#)Product:        :PRODUCT:
*/

/*TABSTOP=4*/

#ifndef JLSS_ID_PRINTFMT_H
#define JLSS_ID_PRINTFMT_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

#ifdef MAIN_PROGRAM
#ifndef lint
/* Prevent over-aggressive optimizers from eliminating ID string */
extern const char jlss_id_printfmt_h[];
const char jlss_id_printfmt_h[] = "@(#)$Id: printfmt.h,v 2.1 2011/07/18 16:30:54 jleffler Exp $";
#endif /* lint */
#endif /* MAIN_PROGRAM */

typedef enum PFP_Errno
{
    PFE_NoError,            /* No error */
    PFE_InvalidFormat,      /* Found % with no valid conversion specifier */
    PFE_NumberOverflow,     /* Number too big (max: 16,383) (should not be seen by users) */
    PFE_WidthOverflow,      /* Field width too big (max: 16,383)     */
    PFE_PrecOverflow,       /* Field precision too big (max: 16,383) */
    PFE_InvalidModifier,    /* Invalid modifier characters */
    PFE_MissingNDollar,     /* One n$ was present and others were missing */
    PFE_RepeatedFlag,       /* One of the flags was repeated */
    PFE_DollarOverflow,     /* Value in n$ too big (max: 16,383) */
    PFE_InvalidError,       /* Error number is not recognized */
    PFE_BufferTooSmall,     /* Buffer too small */
    PFE_BogusWidth,         /* Faulty width specification */
    PFE_BogusPrecision,     /* Faulty precision specification */
} PFP_Errno;

enum
{
    FWP_None   = -1,
    FWP_Star   = -2,
};

typedef enum PFP_Status
{
    PFP_Found  = +1,
    PFP_Error  = -1,
    PFP_Finish =  0,
} PFP_Status;

typedef struct PrintFormat
{
    const char *start;          /* Pointer to % symbol */
    const char *end;            /* Pointer to conversion specifier */
    PFP_Errno   error;          /* Conversion error number */
    short       width;          /* Field width (FPW_None for none, FPW_Star for *) */
    short       precision;      /* Field precision (FPW_None for none, FPW_Star for *) */
    short       conv_num;       /* n of %n$ (0 for none) */
    short       width_num;      /* n of *n$ for width (0 for none) */
    short       prec_num;       /* n of *n$ for precision (0 for none) */
    char        flags[6];       /* [+-0# ] */
    char        modifier[3];    /* hh|h|l|ll|j|z|t|L */
    char        convspec;       /* [diouxXfFeEgGAascp] */
} PrintFormat;

/*
** print_format_parse() - isolate and parse next printf() conversion specification
**
**  PrintFormat pf;
**  PFP_Status rc;
**  const char *format = "...%3$+-*2$.*1$llX...";
**  const char *start = format;
**  while ((rc = print_format_parse(start, &pf)) == PFP_Found)
**  {
**      ...use filled in pf to identify format...
**      start = pf.end + 1;
**  }
**  if (rc == PFP_Error)
**      ...report error, possibly using print_format_error(pf.error)...
*/
extern PFP_Status  print_format_parse(const char *src, PrintFormat *pf);
extern const char *print_format_error(PFP_Errno err);
extern PFP_Status  print_format_create(PrintFormat *pf, char *buffer, size_t buflen);

#ifdef __cplusplus
}
#endif

#endif /* JLSS_ID_PRINTFMT_H */
于 2012-02-10T05:55:40.663 に答える
0

さて、 printf-parseの GNU libc 実装はオープン ソースです。ライセンスがプロジェクトに適合することを確認する必要があります。

別のオプションは、外部関数インターフェイスライブラリを使用して、動的な数の引数で関数呼び出しを行うことです。avcallはこれを許可する必要があり、おそらくlibffiでも同様に実行できます。

于 2012-02-10T05:56:43.543 に答える