The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Farabi on February 01, 2010, 02:29:07 AM

Title: This macro did not works, why?
Post by: Farabi on February 01, 2010, 02:29:07 AM
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:
Title: Re: This macro did not works, why?
Post by: Farabi on February 01, 2010, 02:31:53 AM
#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 */
Title: Re: This macro did not works, why?
Post by: donkey on February 01, 2010, 03:22:28 AM
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
Title: Re: This macro did not works, why?
Post by: jj2007 on February 01, 2010, 08:54:47 AM
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.
Title: Re: This macro did not works, why?
Post by: drizz on February 01, 2010, 05:57:18 PM

@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

Title: Re: This macro did not works, why?
Post by: Farabi on February 02, 2010, 01:09:31 AM
Zheees, that was complicated. But thanks all  :U
Title: Re: This macro did not works, why?
Post by: jj2007 on February 02, 2010, 11:59:19 AM
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.
Title: Re: This macro did not works, why?
Post by: rags on July 25, 2010, 12:09:34 AM
JJ,
In your EnumAdd macro, what is the 'init' param used for?
I don't see where it is used in the macro.
Title: Re: This macro did not works, why?
Post by: jj2007 on July 25, 2010, 08:01:19 AM
   EnumCrt   CURLFORM, WM_USER+100