Basics-Constants.xml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <sect1 id="Constants">
  5. <title>Constants</title>
  6. <sect2 id="Const_String">
  7. <title>String</title>
  8. <para>All string literals must be contained within single quotation marks
  9. ( ' ' ). All ECL code is UTF-8 encoded, which means that all strings are
  10. also UTF-8 encoded, whether Unicode or non-Unicode strings. Therefore, you
  11. must use a UTF-8<indexterm>
  12. <primary>UTF-8</primary>
  13. </indexterm> editor (such as the ECL IDE <indexterm>
  14. <primary>ECL IDE</primary>
  15. </indexterm> program).</para>
  16. <para>To include the single quote character (apostrophe) in a constant
  17. string, prepend a backslash (\). To include the backslash character (\) in
  18. a constant string, use two backslashes (\\) together.</para>
  19. <programlisting>STRING20 MyString2 := 'Fred\'s Place';
  20. //evaluated as: "Fred's Place"
  21. STRING20 MyString3 := 'Fred\\Ginger\'s Place';
  22. //evaluated as: "Fred\Ginger's Place"</programlisting>
  23. <para>Other available escape characters are:</para>
  24. <para><informaltable colsep="1" frame="all" rowsep="1">
  25. <tgroup cols="2">
  26. <colspec colwidth="85.75pt" />
  27. <colspec />
  28. <tbody>
  29. <row>
  30. <entry><emphasis role="code">\t</emphasis></entry>
  31. <entry>tab</entry>
  32. </row>
  33. <row>
  34. <entry><emphasis role="code">\n</emphasis></entry>
  35. <entry>new line</entry>
  36. </row>
  37. <row>
  38. <entry><emphasis role="code">\r</emphasis></entry>
  39. <entry>carriage return</entry>
  40. </row>
  41. <row>
  42. <entry><emphasis role="code">\nnn</emphasis></entry>
  43. <entry>3 octal digits (for any other character)</entry>
  44. </row>
  45. <row>
  46. <entry><emphasis role="code">\uhhhh</emphasis></entry>
  47. <entry>lowercase "u" followed by 4 hexadecimal digits (for any
  48. other UNICODE-only character)</entry>
  49. </row>
  50. </tbody>
  51. </tgroup>
  52. </informaltable></para>
  53. <programlisting>MyString1 := 'abcd';
  54. MyString2 := U'abcd\353'; // becomes 'abcdë'
  55. </programlisting>
  56. <para><emphasis role="bold">Hexadecimal<indexterm>
  57. <primary>Hexadecimal</primary>
  58. </indexterm> string constants<indexterm>
  59. <primary>string constants</primary>
  60. </indexterm> </emphasis>must begin with a leading “x” character. Only
  61. valid hexadecimal values (0-9, A-F) may be in the character string and
  62. there must be an even number of characters.</para>
  63. <programlisting>DATA2 MyHexString := x'0D0A'; // a 2-byte hexadecimal string</programlisting>
  64. <para><emphasis role="bold">Data string<indexterm>
  65. <primary>Data string</primary>
  66. </indexterm> constants<indexterm>
  67. <primary>constants</primary>
  68. </indexterm> </emphasis>must begin with a leading “D” character. This is
  69. directly equivalent to casting the string constant to DATA.</para>
  70. <programlisting>MyDataString := D'abcd'; // same as: (DATA)'abcd'</programlisting>
  71. <para><emphasis role="bold">Unicode string<indexterm>
  72. <primary>Unicode string</primary>
  73. </indexterm> constants </emphasis>must begin with a leading “U”
  74. character. Characters between the quotes are utf8-encoded and the type of
  75. the constant is UNICODE.</para>
  76. <programlisting>MyUnicodeString1 := U'abcd'; // same as: (UNICODE)'abcd'
  77. MyUnicodeString2 := U'abcd\353'; // becomes 'abcdë'
  78. MyUnicodeString3 := U'abcd\u00EB'; // becomes 'abcdë'</programlisting>
  79. <para><emphasis role="bold">UTF8 string<indexterm>
  80. <primary>UTF8 string</primary>
  81. </indexterm> constants </emphasis>must begin with leading “U8”
  82. characters. Characters between the quotes are utf8-encoded and the type of
  83. the constant is UTF8.</para>
  84. <programlisting>MyUTF8String := U8'abcd\353';</programlisting>
  85. <para><emphasis role="bold">VARSTRING string constants<indexterm>
  86. <primary>VARSTRING string constants</primary>
  87. </indexterm> </emphasis>must begin with a leading “V” character. The
  88. terminating null byte is implied and type of the constant is
  89. VARSTRING.</para>
  90. <programlisting>MyVarString := V'abcd'; // same as: (VARSTRING)'abcd'</programlisting>
  91. <para><emphasis role="bold">QSTRING string constants<indexterm>
  92. <primary>QSTRING string constants</primary>
  93. </indexterm> </emphasis>must begin with a leading “Q” character. The
  94. terminating null byte is implied and type of the constant is
  95. VARSTRING.</para>
  96. <programlisting>MyQString := Q'ABCD'; // same as: (QSTRING)'ABCD'</programlisting>
  97. </sect2>
  98. <sect2 id="Numeric">
  99. <title>Numeric</title>
  100. <para>Numeric constants containing a decimal portion are treated as REAL
  101. values (scientific notation is allowed) and those without are treated as
  102. INTEGER<indexterm>
  103. <primary>INTEGER</primary>
  104. </indexterm> (see <emphasis role="bold">Value Types</emphasis>). Integer
  105. constants may be decimal, hexadecimal, or binary values.
  106. Hexadecimal<indexterm>
  107. <primary>Hexadecimal</primary>
  108. </indexterm> values are specified with either a leading “0x” or a
  109. trailing “x” character. Binary values<indexterm>
  110. <primary>Binary values</primary>
  111. </indexterm> are specified with either a leading “0b” or a trailing “b”
  112. character.</para>
  113. <programlisting>MyInt1 := 10; // value of MyInt1 is the INTEGER value 10
  114. MyInt2 := 0x0A; // value of MyInt2 is the INTEGER value 10
  115. MyInt3 := 0Ax; // value of MyInt3 is the INTEGER value 10
  116. MyInt4 := 0b1010; // value of MyInt4 is the INTEGER value 10
  117. MyInt5 := 1010b; // value of MyInt5 is the INTEGER value 10
  118. MyReal1 := 10.0; // value of MyReal1 is the REAL value 10.0
  119. MyReal2 := 1.0e1; // value of MyReal2 is the REAL value 10.0
  120. </programlisting>
  121. </sect2>
  122. <sect2 id="CompileTimeConstants" role="brk">
  123. <title>Compile Time Constants</title>
  124. <para>The following system constants <indexterm>
  125. <primary>constants</primary>
  126. </indexterm><indexterm>
  127. <primary>system constants</primary>
  128. </indexterm>are available at compile time. These can be useful in
  129. creating conditional code.</para>
  130. <para><informaltable colsep="1" frame="all" rowsep="1">
  131. <tgroup cols="2">
  132. <colspec colwidth="150pt" />
  133. <colspec />
  134. <tbody>
  135. <row>
  136. <entry><emphasis>__ECL_VERSION__<indexterm>
  137. <primary>__ECL_VERSION__</primary>
  138. </indexterm></emphasis></entry>
  139. <entry>A STRING containing the value of the platform version.
  140. For example, '6.4.0'</entry>
  141. </row>
  142. <row>
  143. <entry><emphasis>__ECL_VERSION_MAJOR__ <indexterm>
  144. <primary>__ECL_VERSION_MAJOR__</primary>
  145. </indexterm></emphasis></entry>
  146. <entry>An INTEGER containing the value of the major portion of
  147. the platform version. For example, '6'</entry>
  148. </row>
  149. <row>
  150. <entry><emphasis>__ECL_VERSION_MINOR__<indexterm>
  151. <primary>__ECL_VERSION_MINOR__</primary>
  152. </indexterm></emphasis></entry>
  153. <entry>An INTEGER containing the value of the minor portion of
  154. the platform version. For example, '4'</entry>
  155. </row>
  156. <row>
  157. <entry><emphasis>__ECL_LEGACY_MODE__<indexterm>
  158. <primary>__ECL_LEGACY_MODE__</primary>
  159. </indexterm></emphasis></entry>
  160. <entry>A BOOLEAN value indicating if it is being compiled with
  161. legacy IMPORT semantics.</entry>
  162. </row>
  163. <row>
  164. <entry><emphasis>__OS__<indexterm>
  165. <primary>__OS__</primary>
  166. </indexterm></emphasis></entry>
  167. <entry>A STRING indicating the operating system to which it is
  168. being compiled. (Available values are: 'windows' or
  169. 'linux')</entry>
  170. </row>
  171. <row>
  172. <entry><emphasis>__STAND_ALONE__<indexterm>
  173. <primary>__STAND_ALONE__</primary>
  174. </indexterm></emphasis></entry>
  175. <entry>A BOOLEAN value indicating if it is being compiled to a
  176. stand-alone executable.</entry>
  177. </row>
  178. </tbody>
  179. </tgroup>
  180. </informaltable>Example:</para>
  181. <para><programlisting>IMPORT STD;
  182. STRING14 fGetDateTimeString() :=
  183. #IF(__ECL_VERSION_MAJOR__ &gt; 5) or ((__ECL_VERSION_MAJOR__ = 5) AND (__ECL_VERSION_MINOR__ &gt;= 2))
  184. STD.Date.SecondsToString(STD.Date.CurrentSeconds(true), '%Y%m%d%H%M%S');
  185. #ELSE
  186. FUNCTION
  187. string14 fGetDimeTime():= // 14 characters returned
  188. BEGINC++
  189. #option action
  190. struct tm localt; // localtime in "tm" structure
  191. time_t timeinsecs; // variable to store time in secs
  192. time(&amp;timeinsecs);
  193. localtime_r(&amp;timeinsecs,&amp;localt);
  194. char temp[15];
  195. strftime(temp , 15, "%Y%m%d%H%M%S", &amp;amp;localt); // Formats the localtime to YYYYMMDDhhmmss
  196. strncpy(__result, temp, 14);
  197. ENDC++;
  198. RETURN fGetDimeTime();
  199. END;
  200. #END;
  201. </programlisting></para>
  202. </sect2>
  203. </sect1>