在运行时传递给sscanf()的可变数量的参数 [英] variable number of parameters passed to sscanf() at runtime

查看:227
本文介绍了在运行时传递给sscanf()的可变数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个场景,其中在运行时*选择了* b $ b sscanf()调用的源和格式字符串*。格式的数量

转换/替换已知以及每个的最大长度为
,因此分配动态所需的内存非常容易。 />

问题是,如何调用sscanf()?


实现这一点的唯一方法我能想到的是弄乱堆栈

使用嵌入式asm并自己调用该函数,但我不确定我是如何使这个相当安全和便携的
。我真正需要的是这样定义的

函数:


int asscanf(const char * source,const char * fmt,void ** storage,int

storage_size);


其中storage是一个void *指针数组,用于存储转换后的

数据。


有没有人在遇到这个问题之前或建议

解决它?


- Andrew

Hi,

I have a scenario in which both the source and format string for a
sscanf() call is selected *at runtime*. The number of format
conversions/substitutions is known as well as the maximum length of
each, so it''s easy enough to allocate the memory required dynamically.

The problem is, how do I call sscanf()?

The only way to acheive this I can think of is to mess with the stack
using embedded asm and call the function myself, but i''m unsure how I
could make this reasonably safe and portable. What I really need is a
function defined like this:

int asscanf(const char* source, const char* fmt, void** storage, int
storage_size);

where storage is an array of void* pointers for storing the converted
data.

Has anybody come up against this problem before or suggest anyway to
tackle it?

-- Andrew

推荐答案

安德鲁写道:
我有一个场景,其中源和格式字符串为
在运行时*选择sscanf()调用。


一个包含类似printf(和类似scanf)功能的解释器吗?

我能想到没有其他需要它的场景,这时候。

已知格式转换/替换的数量以及每个转换/替换的最大长度,因此分配动态所需的内存非常容易。 br />
问题是,如何调用sscanf()?

实现这一点的唯一方法我可以想到的是使用嵌入式asm来搞乱堆栈
并且我自己打电话给这个功能,但我不确定我是如何才能使这个相当安全和便携。我真正需要的是这样定义的函数:

int asscanf(const char * source,const char * fmt,void ** storage,int
storage_size);

其中storage是一个void *指针数组,用于存储转换后的
数据。

有没有人反对这个问题或建议无论如何
解决它?
I have a scenario in which both the source and format string for a
sscanf() call is selected *at runtime*.
An interpreter that contains printf-like (and scanf-like) functionality?
I can think of no other scenario that would require it, at this time.
The number of format
conversions/substitutions is known as well as the maximum length of
each, so it''s easy enough to allocate the memory required dynamically.

The problem is, how do I call sscanf()?

The only way to acheive this I can think of is to mess with the stack
using embedded asm and call the function myself, but i''m unsure how I
could make this reasonably safe and portable. What I really need is a
function defined like this:

int asscanf(const char* source, const char* fmt, void** storage, int
storage_size);

where storage is an array of void* pointers for storing the converted
data.

Has anybody come up against this problem before or suggest anyway to
tackle it?



是的,处理它的方法是编写自己的sscanf。使用

字符串函数和istringstreams。


V

-

请删除资金来自通过邮件回复我的地址


Yes, and the way to deal with it is to write your own "sscanf" using
string functions and istringstreams.

V
--
Please remove capital As from my address when replying by mail


这个场景是一个简单但相当冗长的文本协议,带有消息

,如下所示:


1<格式化文字> \ n

2<格式不同的文字> \ n

....


第一个数字用于识别(在事物之中)格式

字符串用于从字符串的其余部分提取更多参数。

这样的想法将使得需要在几十个处理函数中执行sscanf()(或其他此类
解释)调用(对于每种消息类型至少有
1) )。

The scenario is a simple but quite lengthy text protocol with messages
like so:

1 <formatted text>\n
2 <differently formatted text>\n
....

The first number is used to identify (amongst over things) the format
string used to extract further parameters from the rest of the string.
The idea being this will mute the need to do sscanf() (or other such
interpretation) calls in dozens of handler functions (there is atleast
1 for each message type).


" Andrew" <一个****** @ gmail.com>在留言中写道

news:11 ********************** @ u72g2000cwu.googlegr oups.com ...
"Andrew" <an******@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...


我有一个场景,其中在运行时*选择了
sscanf()调用的源和格式字符串*。已知格式
转换/替换的数量以及每个的最大长度,因此很容易分配动态所需的内存。

问题是的,我如何调用sscanf()?

实现这一点的唯一方法我能想到的是使用嵌入式asm混乱堆栈并自己调用函数,但是我我不确定我如何才能使这个相当安全和便携。
Hi,

I have a scenario in which both the source and format string for a
sscanf() call is selected *at runtime*. The number of format
conversions/substitutions is known as well as the maximum length of
each, so it''s easy enough to allocate the memory required dynamically.

The problem is, how do I call sscanf()?

The only way to acheive this I can think of is to mess with the stack
using embedded asm and call the function myself, but i''m unsure how I
could make this reasonably safe and portable.




我会说asm块是最短的方法。

我制作了一个非常快速的样本,在vc ++ 7中编译,

显示如何在一个简单的asm块中调用一个函数。

你可以用一个asm块有一个循环,将你阵列中变量的数量推到堆栈上

然后调用sscanf,然后弹出推送的
$ b来自堆栈的$ b变量并继续。

你可以用很少的装配知识来做到这一点。


// er ic


- FILE stdafx.h -

#pragma一次


#include< iostream>

#include< tchar.h>

- EOF -

- 文件asmtest.cpp -

#include" stdafx.h"

void testfunc(int i){

std :: cout<< " void testfunc(int i):" << i<< std :: endl;

}

void testfunc2(int i,int j){

std :: cout<< " void testfunc2(int i,int j):" << i<< " " << j<<

std :: endl;

}

int _tmain(int argc,_TCHAR * argv [])

{

//呼叫1 arg

int i = 10,j = 20;

__asm {

PUSH我

CALL testfunc

POP i

}


//用2个args打电话

__asm {

PUSH j // arg 2 - 如果你想知道为什么arg 2

是第一个,请阅读调用会话。 )

PUSH我// arg 1

CALL testfunc2 //打电话

POP i //从堆栈中删除

POP j //从堆栈中删除

}

返回0;

}


- EOF -



I''d say a asm block would be the shortest way to do this.
I made a very fast sample that compiles in vc++ 7 that
shows howto call a function inside a simple asm block.
You could with a asm block have a loop that pushes
the number of variables in your array onto the stack
and then calls sscanf, and afterwards pop the pushed
variables from the stack and continues.
You can do this with very little knowledge of assembly.

//eric

-- FILE stdafx.h --
#pragma once

#include <iostream>
#include <tchar.h>
-- EOF --
-- FILE asmtest.cpp --
#include "stdafx.h"
void testfunc(int i) {
std::cout << "void testfunc(int i): " << i << std::endl;
}
void testfunc2(int i, int j) {
std::cout << "void testfunc2(int i, int j): " << i << " " << j <<
std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
// calling with 1 arg
int i = 10, j = 20;
__asm {
PUSH i
CALL testfunc
POP i
}

// calling with 2 args
__asm {
PUSH j // arg 2 - read about calling convensions if you wonder why arg 2
is first ;)
PUSH i // arg 1
CALL testfunc2 // do the call
POP i // remove from stack
POP j // remove from stack
}
return 0;
}

-- EOF --


这篇关于在运行时传递给sscanf()的可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆