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.
 
 
 
 

320 lines
6.0 KiB

  1. /* Copyright (C) 2011-2022 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 "debug.h"
  23. #include "stackC.h"
  24. #include "stackF.h"
  25. /* #define DEBUG_M */
  26. void putString(char * S)
  27. {
  28. void * M;
  29. int i;
  30. i = i_StackC;
  31. if (i == LSTACKC) stopErr("putString",NULL);
  32. if ((M = malloc(strlen(S)+1)) == NULL) stopErr("putString","malloc");
  33. #ifdef DEBUG_M
  34. printf("New String address : %lu \n",(unsigned long)M);
  35. #endif
  36. strcpy((char*)M,S);
  37. if (fctEnCours) makeFct(T_CHA,M);
  38. else {
  39. stackC[i++] = (char*)M;
  40. _MODIF_i_StackC_(i);
  41. }
  42. }
  43. char * getString(void) /* NOT free() !!! */
  44. {
  45. int i;
  46. i = i_StackC;
  47. if (i) {
  48. i--;
  49. _MODIF_i_StackC_(i);
  50. return(stackC[i]);
  51. }
  52. messErr(6);
  53. return NULL;
  54. }
  55. int isNString(int n)
  56. {
  57. if (i_StackC >= n) return 1;
  58. return 0;
  59. }
  60. void IF_dropC(void)
  61. {
  62. char * S;
  63. S=getString();
  64. #ifdef DEBUG_M
  65. printf("Del String address : %lu \n",(unsigned long)S);
  66. #endif
  67. if (S != NULL) free((void*)S);
  68. }
  69. static void IF_dupC_i(int i)
  70. {
  71. char * S;
  72. int I;
  73. I = i_StackC;
  74. if (I>=i) {
  75. S = stackC[I-i];
  76. putString(S);
  77. }
  78. else messErr(19);
  79. }
  80. void IF_dupC(void)
  81. {
  82. IF_dupC_i(1);
  83. }
  84. void IF_overC(void)
  85. {
  86. IF_dupC_i(2);
  87. }
  88. void IF_swapC(void)
  89. {
  90. char * S;
  91. int I;
  92. I = i_StackC;
  93. if (I>1) {
  94. S = stackC[I-1];
  95. stackC[I-1] = stackC[I-2];
  96. stackC[I-2] = S;
  97. }
  98. else messErr(19);
  99. }
  100. void IF_stackC_clear(void)
  101. {
  102. while (i_StackC) IF_dropC();
  103. }
  104. static void IF_catC_i(int i)
  105. {
  106. char * S1, * S2, *S;
  107. int l, I;
  108. I = i_StackC;
  109. if (I>1) {
  110. S1 = stackC[I-2];
  111. S2 = stackC[I-1];
  112. l = strlen(S1) + strlen(S2) + i + 1;
  113. if ((S = (char*)malloc(l+1)) == NULL) stopErr("IF_catC_i","malloc");
  114. strcpy(S,S1);
  115. if (i) strcat(S, " ");
  116. strcat(S,S2);
  117. IF_dropC();
  118. IF_dropC();
  119. I = i_StackC;
  120. stackC[I++]=S;
  121. _MODIF_i_StackC_(I);
  122. }
  123. else messErr(19);
  124. }
  125. void IF_catC(void)
  126. {
  127. IF_catC_i(0);
  128. }
  129. void IF_catsC(void)
  130. {
  131. IF_catC_i(1);
  132. }
  133. void IF_crC(void)
  134. {
  135. printf("\n");
  136. }
  137. static void Get_Date_Time(int x)
  138. {
  139. struct tm * T;
  140. time_t t0;
  141. char b[12];
  142. t0 = time(NULL);
  143. T = localtime(&t0);
  144. if (x) sprintf(b,"%.2d/%.2d/%.4d",T->tm_mday,T->tm_mon+1,T->tm_year+1900);
  145. else sprintf(b,"%.2d:%.2d:%.2d",T->tm_hour,T->tm_min,T->tm_sec);
  146. putString(b);
  147. }
  148. void IF_dateC(void)
  149. {
  150. Get_Date_Time(1);
  151. }
  152. void IF_timeC(void)
  153. {
  154. Get_Date_Time(0);
  155. }
  156. void IF_typeC(void)
  157. {
  158. int i;
  159. i = i_StackC;
  160. if (i) {
  161. printf("%s",stackC[i-1]);
  162. IF_dropC();
  163. }
  164. else messErr(6);
  165. }
  166. void IF_show_stackC(void)
  167. {
  168. int i,j=0,I;
  169. char s;
  170. I=i_StackC;
  171. for(i=I-1;i>=0;i--) {
  172. #ifdef DEBUG_M
  173. printf(" %.5d : \"%s\" Add=%lu",strlen(stackC[i]),stackC[i],
  174. (unsigned long)(stackC[i]));
  175. #else
  176. printf(" %.5d : \"%s\"",(int)strlen(stackC[i]),stackC[i]);
  177. #endif
  178. if (j==0) printf(" <- top");
  179. printf("\n");
  180. j++;
  181. if (j==NBLIG) break;
  182. }
  183. if (i>0) {
  184. if (i==1) s=' ';
  185. else s='s';
  186. printf(" ... and %d other%c string%c !\n",I-NBLIG,s,s);
  187. } else printf("<end of character stack>\n");
  188. }
  189. void IFD_show_stackC(void)
  190. {
  191. _IFD_BEGIN_
  192. IF_show_stackC();
  193. _IFD_END_
  194. }
  195. void suiteString(char *S)
  196. {
  197. int end=0;
  198. if ((strlen(bufC)+strlen(S)+1) > MAXSTRING) {
  199. dropTrSuite();
  200. _MODIF_stringEnCours_(0);
  201. messErr(9);
  202. return;
  203. }
  204. if (S[strlen(S)-1] == '"') {
  205. S[strlen(S)-1] = '\0';
  206. end=1;
  207. }
  208. strcat(bufC,S);
  209. if (end) {
  210. dropTrSuite();
  211. _MODIF_stringEnCours_(0);
  212. putString(bufC);
  213. }
  214. }
  215. void IF_debString(void)
  216. {
  217. bufC[0]='\0';
  218. putTrSuite(suiteString);
  219. _MODIF_stringEnCours_(1);
  220. }
  221. void dump_eltC(int fd, char *A)
  222. {
  223. uint32_t l;
  224. l=strlen(A);
  225. write(fd, (void*)&l, sizeof(l));
  226. write(fd, A, l+1);
  227. }
  228. void dump_stackC(int fd)
  229. {
  230. uint32_t n, i, l;
  231. n = i_StackC;
  232. write(fd, (void*)&n, sizeof(n));
  233. for (i=0; i<n; i++) dump_eltC(fd, stackC[i]);
  234. dump_rest_pr(0,n,"character");
  235. }
  236. char * restore_eltC(int fd)
  237. {
  238. uint32_t l;
  239. void * M;
  240. read(fd, (void*)&l, sizeof(l));
  241. if ((M = malloc(l+1)) == NULL) stopErr("restore_eltC","malloc");
  242. read(fd, M, l+1);
  243. return (char*)M;
  244. }
  245. void restore_stackC(int fd)
  246. {
  247. uint32_t n=0, i, j;
  248. if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return;
  249. IF_stackC_clear();
  250. for (i=0; i<n; i++) {
  251. j = i_StackC;
  252. stackC[j++] = restore_eltC(fd);
  253. _MODIF_i_StackC_(j);
  254. }
  255. dump_rest_pr(1,n,"character");
  256. }
  257. /* gestion des meta-stacks */
  258. void IF_new_stackC(void)
  259. {
  260. if (G_i_TStackC == LSTACKS) {
  261. messErr(60); return;
  262. }
  263. G_TiStackC[G_i_TStackC] = i_StackC;
  264. G_TStackC[G_i_TStackC++] = stackC;
  265. stackC = G_TStackC[G_i_TStackC];
  266. i_StackC = G_TiStackC[G_i_TStackC];
  267. if (stackC == (char **)0) {
  268. if ((stackC = (char**)malloc(sizeof(char**)*LSTACKC)) == NULL)
  269. stopErr("IF_new_stackC","malloc");
  270. i_StackC=0;
  271. }
  272. }
  273. void IF_old_stackC(void)
  274. {
  275. if (G_i_TStackC == 0) {
  276. messErr(61); return;
  277. }
  278. G_TiStackC[G_i_TStackC] = i_StackC;
  279. G_TStackC[G_i_TStackC--] = stackC;
  280. stackC = G_TStackC[G_i_TStackC];
  281. i_StackC = G_TiStackC[G_i_TStackC];
  282. }