Replacing vsscanf with msvc

I ran into the problem of porting code from linux (gcc) to windows (msvc). C99 vsscanf seems to be unavailable and has no obvious replacement.

I read about the solution using the _input_l internal function and statically linking to the crt runtime, but unfortunately I cannot link statically, since it will be useless with all plugins (like DLLs) loaded by the application.

So, is there any replacement or way to write a shell for vsscanf?

Update 2016-02-24 :

When it was first asked, there was no native replacement, but since then MSVC has implemented support for this and much more.

  • VS2013 and later implements vsscanf and friends.
  • C ++ 11 also includes support.
+4
source share
6 answers

Hacking that should work:

 int vsscanf(const char *s, const char *fmt, va_list ap) { void *a[20]; int i; for (i=0; i<sizeof(a)/sizeof(a[0]); i++) a[i] = va_arg(ap, void *); return sscanf(s, fmt, a[0], a[1], a[2], a[3], a[4], a[5], a[6], /* etc... */); } 

Replace 20 with the maximum number of arguments you think you might need. This code is not very portable, but it is intended only for use on one faulty system without vsscanf , so it does not really matter.

+3
source

A quick search showed several suggestions, including http://www.flipcode.net/archives/vsscanf_for_Win32.shtml

0
source

As noted in C ++, did you think you were just biting a bullet and completely moving away from the scanf function line? The idiomatic way of C ++ would be to use std::istringstream . Rewriting to use this instead of looking for a vsscanf replacement vsscanf be easier and more portable, not to mention much more type safety.

0
source

if you want to wrap sscanf and you are using C ++ 11 you can do this:

 template<typename... Args> int mysscanf(const char* str, const char* fmt, Args... args) { //... return sscanf(str, fmt, args...); } 

To make this work on msvc, you need to download this update:

http://www.microsoft.com/en-us/download/details.aspx?id=35515

0
source

It's funny that it didnโ€™t reach me today. I could have sworn I used a function in the past. But anyway, here is a solution that works and is safe, like your arguments and format string:

template < size_t _NumArgs >

int VSSCANF_S (LPCTSTR strSrc, LPCTSTR ptcFmt, INT_PTR (& arr) [_ NumArgs]) {

 class vaArgs { vaArgs() {} INT_PTR* m_args[_NumArgs]; public: vaArgs(INT_PTR (&arr)[_NumArgs]) { for(size_t nIndex=0;nIndex<_NumArgs;++nIndex) m_args[nIndex] = &arr[nIndex]; } }; return sscanf_s(strSrc, ptcFmt, vaArgs(arr)); } 

//////////////////////////////////////////////////// ///////////////////////////

int _tmain (int, LPCTSTR argv [])

 { INT_PTR args[3]; int nScanned = VSSCANF_S(_T("-52 Hello 456 @"), _T("%d Hello %u %c"), args); return printf(_T("Arg1 = %d, arg2 = %u, arg3 = %c\n"), args[0], args[1], args[2]); } 

code>

Of:

Arg1 = -52, arg2 = 456, arg3 = @ Press any key to continue.,

Well, I canโ€™t format it correctly, but you get the point.

0
source

changed: http://www.gamedev.net/topic/310888-no-vfscanf-in-visual-studio/

 #if defined(_WIN32) && (_MSC_VER <= 1500) static int vsscanf( const char *buffer, const char *format, va_list argPtr ) { // Get an upper bound for the # of args size_t count = 0; const char* p = format; while(1) { char c = *(p++); if (c == 0) break; if (c == '%' && (p[0] != '*' && p[0] != '%')) ++count; } if (count <= 0) return 0; int result; // copy stack pointer _asm { mov esi, esp; } // push variable parameters pointers on stack for (int i = count - 1; i >= 0; --i) { _asm { mov eax, dword ptr[i]; mov ecx, dword ptr [argPtr]; mov edx, dword ptr [ecx+eax*4]; push edx; } } int stackAdvance = (2 + count) * 4; _asm { // now push on the fixed params mov eax, dword ptr [format]; push eax; mov eax, dword ptr [buffer]; push eax; // call sscanf, and more the result in to result call dword ptr [sscanf]; mov result, eax; // restore stack pointer mov eax, dword ptr[stackAdvance]; add esp, eax; } return result; } #endif // _WIN32 / _MSC_VER <= 1500 

tested only on Visual Studio 2008

0
source

Source: https://habr.com/ru/post/1304301/


All Articles