|
|
|
|
Elisa |
C |
Storage |
| character | char | 1 byte |
| boolean | not defined | 1 byte |
| integer | long | 4 bytes |
| real | double | 8 bytes |
As will be clear from this table: there are no major obstacles to map a basic data type from Elisa to C and visa versa. However, the memory layouts of strings, arrays and structures are quite different.
In the C language a string is an array of characters closed by a null character. In Elisa, text, which is an array of characters, can be converted to a C-string by means of the foreign function:
foreign(c) _Cstring(text) -> CstringPtr;
The _Cstring function converts an Elisa text into a pointer to a Cstring. The CstringPtr is defined by:
type CstringPtr = subtype(integer);
It is also possible to convert a Cstring into an Elisa text by means of the foreign function:
foreign(c) _text(CstringPtr) -> text;
The complete set of string conversion declarations is:
type CstringPtr = subtype(integer);
foreign(c) _Cstring(text) -> CstringPtr;
foreign(c) _text(CstringPtr) -> text;
The memory layouts of Elisa arrays and C arrays are incompatible. In some foreign C functions a reference to a C array is required. This can be done by converting an array written in Elisa to an array processable by C. For example, an array of reals in Elisa can be converted to a pointer to the array of reals as is used in C, by means of the foreign function:
foreign(c) _ArrayPtr(array(real)) -> CarrayPtr;
The _ArrayPtr function converts an Elisa array of reals into a pointer to a Carray. The CarrayPtr is defined by:
type CarrayPtr = subtype(integer);
Array conversion declarations are:
foreign(c) _ArrayPtr(array(character)) -> CarrayPtr;
foreign(c) _ArrayPtr(array(boolean)) -> CarrayPtr;
foreign(c) _ArrayPtr(array(integer)) -> CarrayPtr;
foreign(c) _ArrayPtr(array(real)) -> CarrayPtr;
The use of foreign C declarations to operate on C files are:
type CstringPtr = subtype(integer); foreign(c) _Cstring(text) -> CstringPtr; foreign(c) _text(CstringPtr) -> text; type CarrayPtr = subtype(integer); foreign(c) _ArrayPtr(text) -> CarrayPtr; type TextFile = subtype(integer);type StreamFile = subtype(integer);foreign(c) fopen(FileName = CstringPtr, FileMode = CstringPtr) -> TextFile; foreign(c) fclose(TextFile) -> integer; foreign(c) remove(FileName = CstringPtr) -> integer; foreign(c) fgets(CstringPtr, MaxStringSize = integer, TextFile) -> integer; foreign(c) strlen(CstringPtr) -> integer; foreign(c) ObtainErrorFile( ) -> TextFile; foreign(c) fprintf(outtextfile = TextFile, format = CstringPtr, character) -> nothing; foreign(c) fseek(StreamFile, FileOffset = integer, Origin = integer) -> integer; foreign(c) fwrite( CarrayPtr, Size = integer, Number = integer, StreamFile) -> integer;
| Part 1: Language Description | Chapter 18: External Interfaces |
|
<!-- Start of StatCounter Code for Default Guide --> <script type="text/javascript"> var sc_project=11338540; var sc_invisible=0; var sc_security="f2d7a14f"; var sc_https=1; var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www."); document.write("<sc"+"ript type='text/javascript' src='" + scJsHost+ "statcounter.com/counter/counter.js'></"+"script>"); </script> <noscript><div class="statcounter"><a title="Web Analytics Made Easy - StatCounter" href="http://statcounter.com/" target="_blank"><img class="statcounter" src="//c.statcounter.com/11338540/0/f2d7a14f/0/" alt="Web Analytics Made Easy - StatCounter"></a></div></noscript> <!-- End of StatCounter Code for Default Guide --> |