Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

244 lines
4.4 KiB

  1. /* Copyright (C) 2011-2013 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. #include "conf.h"
  14. /* stackC.c */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include "nife.h"
  20. #include "mth.h"
  21. #include "err.h"
  22. #include "stackC.h"
  23. #include "stackF.h"
  24. /* #define DEBUG_M */
  25. void putString(char * S)
  26. {
  27. void * M;
  28. int i;
  29. i = i_StackC;
  30. if (i == LSTACKC) stopErr("putString",NULL);
  31. if ((M = malloc(strlen(S)+1)) == NULL) stopErr("putString","malloc");
  32. #ifdef DEBUG_M
  33. printf("New String address : %lu \n",(unsigned long)M);
  34. #endif
  35. strcpy((char*)M,S);
  36. if (fctEnCours) makeFct(T_CHA,M);
  37. else {
  38. stackC[i++] = (char*)M;
  39. _MODIF_i_StackC_(i);
  40. }
  41. }
  42. char * getString(void) /* NOT free() !!! */
  43. {
  44. int i;
  45. i = i_StackC;
  46. if (i) {
  47. i--;
  48. _MODIF_i_StackC_(i);
  49. return(stackC[i]);
  50. }
  51. messErr(6);
  52. return NULL;
  53. }
  54. int isNString(int n)
  55. {
  56. if (i_StackC >= n) return 1;
  57. return 0;
  58. }
  59. void IF_dropC(void)
  60. {
  61. char * S;
  62. S=getString();
  63. #ifdef DEBUG_M
  64. printf("Del String address : %lu \n",(unsigned long)S);
  65. #endif
  66. if (S != NULL) free((void*)S);
  67. }
  68. static void IF_dupC_i(int i)
  69. {
  70. char * S;
  71. int I;
  72. I = i_StackC;
  73. if (I>=i) {
  74. S = stackC[I-i];
  75. putString(S);
  76. }
  77. else messErr(19);
  78. }
  79. void IF_dupC(void)
  80. {
  81. IF_dupC_i(1);
  82. }
  83. void IF_overC(void)
  84. {
  85. IF_dupC_i(2);
  86. }
  87. void IF_swapC(void)
  88. {
  89. char * S;
  90. int I;
  91. I = i_StackC;
  92. if (I>1) {
  93. S = stackC[I-1];
  94. stackC[I-1] = stackC[I-2];
  95. stackC[I-2] = S;
  96. }
  97. else messErr(19);
  98. }
  99. void IF_stackC_clear(void)
  100. {
  101. while (i_StackC) IF_dropC();
  102. }
  103. static void IF_catC_i(int i)
  104. {
  105. char * S1, * S2, *S;
  106. int l, I;
  107. I = i_StackC;
  108. if (I>1) {
  109. S1 = stackC[I-2];
  110. S2 = stackC[I-1];
  111. l = strlen(S1) + strlen(S2) + i + 1;
  112. if ((S = (char*)malloc(l+1)) == NULL) stopErr("IF_catC_i","malloc");
  113. strcpy(S,S1);
  114. if (i) strcat(S, " ");
  115. strcat(S,S2);
  116. IF_dropC();
  117. IF_dropC();
  118. I = i_StackC;
  119. stackC[I++]=S;
  120. _MODIF_i_StackC_(I);
  121. }
  122. else messErr(19);
  123. }
  124. void IF_catC(void)
  125. {
  126. IF_catC_i(0);
  127. }
  128. void IF_catsC(void)
  129. {
  130. IF_catC_i(1);
  131. }
  132. void IF_crC(void)
  133. {
  134. printf("\n");
  135. }
  136. static void Get_Date_Time(int x)
  137. {
  138. struct tm * T;
  139. time_t t0;
  140. char b[12];
  141. t0 = time(NULL);
  142. T = localtime(&t0);
  143. if (x) sprintf(b,"%.2d/%.2d/%.4d",T->tm_mday,T->tm_mon+1,T->tm_year+1900);
  144. else sprintf(b,"%.2d:%.2d:%.2d",T->tm_hour,T->tm_min,T->tm_sec);
  145. putString(b);
  146. }
  147. void IF_dateC(void)
  148. {
  149. Get_Date_Time(1);
  150. }
  151. void IF_timeC(void)
  152. {
  153. Get_Date_Time(0);
  154. }
  155. void IF_typeC(void)
  156. {
  157. int i;
  158. i = i_StackC;
  159. if (i) {
  160. printf("%s",stackC[i-1]);
  161. IF_dropC();
  162. }
  163. else messErr(6);
  164. }
  165. void IF_show_stackC(void)
  166. {
  167. int i,j=0,I;
  168. char s;
  169. I=i_StackC;
  170. for(i=I-1;i>=0;i--) {
  171. #ifdef DEBUG_M
  172. printf(" %.5d : \"%s\" Add=%lu",strlen(stackC[i]),stackC[i],
  173. (unsigned long)(stackC[i]));
  174. #else
  175. printf(" %.5d : \"%s\"",(int)strlen(stackC[i]),stackC[i]);
  176. #endif
  177. if (j==0) printf(" <- top");
  178. printf("\n");
  179. j++;
  180. if (j==NBLIG) break;
  181. }
  182. if (i>0) {
  183. if (i==1) s=' ';
  184. else s='s';
  185. printf(" ... and %d other%c string%c !\n",I-NBLIG,s,s);
  186. } else printf("<end of character stack>\n");
  187. }
  188. void suiteString(char *S)
  189. {
  190. int end=0;
  191. if ((strlen(bufC)+strlen(S)+1) > MAXSTRING) {
  192. dropTrSuite();
  193. _MODIF_stringEnCours_(0);
  194. messErr(9);
  195. return;
  196. }
  197. if (S[strlen(S)-1] == '"') {
  198. S[strlen(S)-1] = '\0';
  199. end=1;
  200. }
  201. strcat(bufC,S);
  202. if (end) {
  203. dropTrSuite();
  204. _MODIF_stringEnCours_(0);
  205. putString(bufC);
  206. }
  207. }
  208. void IF_debString(void)
  209. {
  210. bufC[0]='\0';
  211. putTrSuite(suiteString);
  212. _MODIF_stringEnCours_(1);
  213. }