News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

This macro did not works, why?

Started by Farabi, February 01, 2010, 02:29:07 AM

Previous topic - Next topic

Farabi

What is wrong with this?

Quote
ifdef CURL_ISOCPP
CFINIT macro name
   exitm <CURLFORM_ ## name >
   endm
else
CFINIT macro name
   exitm <CURLFORM_ name >
   endm
endif
CURLformoption typedef DWORD
CFINIT( NOTHING)
CFINIT( COPYNAME)
CFINIT( PTRNAME)
CFINIT( NAMELENGTH)
CFINIT( COPYCONTENTS)
CFINIT( PTRCONTENTS)
CFINIT( CONTENTSLENGTH)
CFINIT( FILECONTENT)
CFINIT( ARRAY)
CFINIT( OBSOLETE)
CFINIT( FILE)
CFINIT( BUFFER)
CFINIT( BUFFERPTR)
CFINIT( BUFFERLENGTH)
CFINIT( CONTENTTYPE)
CFINIT( CONTENTHEADER)
CFINIT( FILENAME)
CFINIT( END)
CFINIT( OBSOLETE2)
CFINIT( STREAM)

:dazzled:
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

#ifdef CFINIT
#undef CFINIT
#endif

#ifdef CURL_ISOCPP
#define CFINIT(name) CURLFORM_ ## name
#else
/* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
#define CFINIT(name) CURLFORM_/**/name
#endif

typedef enum {
  CFINIT(NOTHING),        /********* the first one is unused ************/

  /*  */
  CFINIT(COPYNAME),
  CFINIT(PTRNAME),
  CFINIT(NAMELENGTH),
  CFINIT(COPYCONTENTS),
  CFINIT(PTRCONTENTS),
  CFINIT(CONTENTSLENGTH),
  CFINIT(FILECONTENT),
  CFINIT(ARRAY),
  CFINIT(OBSOLETE),
  CFINIT(FILE),

  CFINIT(BUFFER),
  CFINIT(BUFFERPTR),
  CFINIT(BUFFERLENGTH),

  CFINIT(CONTENTTYPE),
  CFINIT(CONTENTHEADER),
  CFINIT(FILENAME),
  CFINIT(END),
  CFINIT(OBSOLETE2),

  CFINIT(STREAM),

  CURLFORM_LASTENTRY /* the last unused */
} CURLformoption;

#undef CFINIT /* done */
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

donkey

To start with I doubt that MASM supports auto-enumerated types so the macro and all of its invocations do pretty much nothing (though it would work fine in C). The enumeration is actually this:

CURLFORM_COPYNAME equ 0
CURLFORM_PTRNAME equ 1
CURLFORM_NAMELENGTH equ 2
CURLFORM_COPYCONTENTS equ 3
CURLFORM_PTRCONTENTS equ 4
CURLFORM_CONTENTSLENGTH equ 5
CURLFORM_FILECONTENT equ 6
CURLFORM_ARRAY equ 7
CURLFORM_OBSOLETE equ 8
CURLFORM_FILE equ 9
CURLFORM_BUFFER equ 10
CURLFORM_BUFFERPTR equ 11
CURLFORM_BUFFERLENGTH equ 12
CURLFORM_CONTENTTYPE equ 13
CURLFORM_CONTENTHEADER equ 14
CURLFORM_FILENAME equ 15
CURLFORM_END equ 16
CURLFORM_OBSOLETE2 equ 17
CURLFORM_STREAM equ 18
CURLFORM_LASTENTRY equ 19

With the constant beginning at 0 and increasing by 1 for each entry. The second thing is why bother with a macro for this, there are not that many entries so you might as well just expand them yourself. Since the macro is undefined after the enumeration is complete it is not intended to be used outside of that scope so it is a useless waste of time to try to mimic it.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

#3
Hi Farabi,
You probably wanted something like this; although I tend to agree with Edgar that this is a bit of an overkill for a simple enumeration...

include \masm32\include\masm32rt.inc

EnumCrt macro name, init
LOCAL hasPlus
  ifb <init>
hasPlus = 0
  else
hasPlus INSTR <init>, <+>
  endif
  if hasPlus
ceOffset SUBSTR <init>, 1, hasPlus
  else
ceOffset equ <>
  endif
  ceNAME equ name ; we start a new enumeration
  ceCount = 0
ENDM

EnumAdd macro name, init
LOCAL tmp$
tmp$ CATSTR ceNAME, <_>, <name>, < = >, ceOffset, %ceCount
tmp$
ceCount = ceCount + 1
ENDM

.code
start:
EnumCrt CURLFORM, WM_USER + 0 ; create a new enumeration starting with WM_USER
; buggy: EnumCrt CURLFORM, 100 ; create a new enumeration starting with 100
EnumCrt CURLFORM ; create a new enumeration starting with zero

EnumAdd COPYNAME
EnumAdd PTRNAME
EnumAdd NAMELENGTH
EnumAdd COPYCONTENTS
EnumAdd PTRCONTENTS
EnumAdd CONTENTSLENGTH
EnumAdd FILECONTENT
EnumAdd ARRAY
EnumAdd OBSOLETE
EnumAdd FILE
EnumAdd BUFFER
EnumAdd BUFFERPTR
EnumAdd BUFFERLENGTH
EnumAdd CONTENTTYPE
EnumAdd CONTENTHEADER
EnumAdd FILENAME
EnumAdd END
EnumAdd OBSOLETE2
EnumAdd STREAM

print str$(CURLFORM_COPYNAME), 9, "CURLFORM_COPYNAME", 13, 10
print str$(CURLFORM_NAMELENGTH), 9, "CURLFORM_NAMELENGTH", 13, 10
print str$(CURLFORM_BUFFER), 9, "CURLFORM_BUFFER", 13, 10
print str$(CURLFORM_STREAM), 9, "CURLFORM_STREAM", 13, 10

getkey
exit

end start


EDIT: The second syntax won't work. More below.

drizz


@Enum macro __EnumStart:=<0>,__EnumArgs:vararg
LOCAL i,arg
i = __EnumStart
for arg,<__EnumArgs>
ifnb <arg>
&arg equ i
endif
i = i + 1
endm
endm

@PrefixedEnum macro __Prefix:=<>,__EnumStart:=<0>,__EnumArgs:vararg
LOCAL i,arg
i = __EnumStart
for arg,<__EnumArgs>
ifnb <arg>
@CatStr(__Prefix,arg,< equ i>)
endif
;;%echo @CatStr(__Prefix,arg) equ @CatStr(%i);; <-- do NOT expand them yourself :-)
i = i + 1
endm
endm
CURLformoption TYPEDEF DWORD

@PrefixedEnum CURLFORM_,0,COPYNAME,PTRNAME;;,     ...etc.
mov eax,CURLFORM_COPYNAME

The truth cannot be learned ... it can only be recognized.

Farabi

Zheees, that was complicated. But thanks all  :U
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

jj2007

Revised version, handles correctly all three syntax variants:

; create a new enumeration starting with...
EnumCrt CURLFORM, WM_USER ; WM_USER aka 1024
EnumCrt CURLFORM, 100 ; 100
EnumCrt CURLFORM ; zero


EnumCrt macro name, init
LOCAL hasPlus
  ceNAME equ name ; we start a new enumeration
  ceCount = 0
  ceOffset equ <>
  ifnb <init>
hasPlus INSTR <init>, <+>
if hasPlus
ceOffset  SubStr <init>, 1, hasPlus
ceCount = @SubStr(<init>, hasPlus + 1)
else
ceCount = init
endif
  endif
ENDM

EnumAdd macro name, init
% @CatStr(&ceNAME&, <_>, <name>, < = >, &ceOffset&, %ceCount)
ceCount = ceCount + 1
ENDM


Testbed attached.

rags

JJ,
In your EnumAdd macro, what is the 'init' param used for?
I don't see where it is used in the macro.
God made Man, but the monkey applied the glue -DEVO

jj2007

   EnumCrt   CURLFORM, WM_USER+100