Re: typedef union
"Mark Tolonen" <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
Nils, Please reply to the mailing list instead of me directly. Answers below. "Mark Tolonen" <[email protected]> wrote in message news:[email protected]... > > "Nils Wagner" <[email protected]> wrote in message > news:[email protected]... >> Hi all, >> >> I am newbie to ctypes. >> >> How can I "translate" the following C-code into Python ? >> I don't know how to handle HEAD and memcpy ? >> Any pointer would be appreciated. >> >> Thanks in advance. >> >> Nils >> >> >> typedef union { >> int i; >> float f; >> char c[4]; >> } Word; >> >> int >> I,J,K,N,IDE,IA,IE,IERR,JUNIT,JUNCAT,NDATA,NREC,LREADY,ONE=1; >> Word BUF[100],HEAD[30]; >> >> for (I=5;I<LHEAD;I++) >> HEAD[I].i = 0; >> HEAD[ 0].i = 1; >> HEAD[ 1].i = LHEAD + NDATA*7; >> HEAD[ 2].i = LHEAD; >> HEAD[ 3].i = NDATA; >> HEAD[ 4].i = 7; >> memcpy (HEAD[ 7].c,"DSIO",4); >> memcpy (HEAD[ 8].c,"TEST",4); >> memcpy (HEAD[ 9].c,"NPCO",4); >> memcpy (HEAD[10].c," ",4); >> memcpy (HEAD[11].c,"DSIO",4); >> HEAD[20].i = 1; >> HEAD[21].i = NDATA; >> STRING = "MM RAD"; >> DSEINH(STRING,&HEAD[24].i,&ONE, >> strlen(STRING)); > > Here's a rough translation. Note how the arrays are declared. "c_char*4" > is a 4-character array *type*. (Word*100)() is a 100-Word array > *instance*. > To assign to the char array, just use a maximum 4-character string. > > import ctypes > > class Word(ctypes.Union): > _fields_ = [ > ('i',ctypes.c_int), > ('f',ctypes.c_float), > ('c',ctypes.c_char*4)] > > I=J=K=N=IDE=IA=IE=IERR=JUNIT=JUNCAT=NDATA=NREC=LREADY=ONE=1 > BUF = (Word*100)() > HEAD = (Word*30)() > > # not defined in C code > LHEAD = 30 > > for I in range(5,LHEAD): > HEAD[I].i = 0 > > HEAD[ 0].i = 1; > HEAD[ 1].i = LHEAD + NDATA*7 > HEAD[ 2].i = LHEAD > HEAD[ 3].i = NDATA > HEAD[ 4].i = 7 > HEAD[ 7].c = "DSIO" > HEAD[ 8].c = "TEST" > HEAD[ 9].c = "NPCO" > HEAD[10].c = " " > HEAD[11].c = "DSIO" > HEAD[20].i = 1 > HEAD[21].i = NDATA > STRING = "MM RAD" > # Not enough info > # DSEINH(STRING,&HEAD[24].i,&ONE,strlen(STRING)); >From your private replies: > Thank you very much for your help. > Here comes information concerning DSEINH. > > extern void dseinh (char* const,int* const, > const int* const,const size_t); > > How do I call dseinh from python (byref etc.) ? First load the dynamic library (.dll or .so) using CDLL. See (untested) example: dll = ctypes.CDLL('library.dll') value1 = ctypes.c_uint() value2 = ctypes.c_uint() s = "string" dll.dseinh(s,ctypes.byref(value1),ctypes.byref(value2),len(s)+1) Python strings are passed as null-terminated C strings, so len(s) + 1 accounts for the null. There is also a ctypes.sizeof() function that can be used with ctypes types and classes derived from ctypes.Structure or ctypes.Union. You can (and sometimes have to) declare the parameters and return types of a function. See the Python ctypes documentation (http://docs.python.org/library/ctypes.html). In regards to your other email: > Is the following loop in C > > do { > IDE = IDE + 1; > } while (ID != KYNPCO) > > equivalent to > > while ID != KYNPCO: > IDE += 1 > > in python ? No, if ID == KYNPCO, the first version would still increment IDE; the 2nd would not. Python doesn't have do/while. Either: IDE += 1 while ID != KYNPCO: IDE += 1 or: while True: IDE += 1 if ID == KYNPCO: break Would give the correct result. -Mark ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev