-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbatch.c
881 lines (860 loc) · 24.1 KB
/
batch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//
// This file is part of Alpertron Calculators.
//
// Copyright 2017-2021 Dario Alejandro Alpern
//
// Alpertron Calculators is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Alpertron Calculators is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
//
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bignbr.h"
#include "expression.h"
#include "factor.h"
#include "showtime.h"
#include "batch.h"
static char *ptrEndBatchFactor;
static char *ptrCurrBatchFactor;
static char *ptrNextBatchFactor;
static bool firstExprProcessed;
bool fromFile;
bool lineEndingCRLF;
int valuesProcessed;
char outputExpr[200000];
#ifdef __EMSCRIPTEN__
char *ptrInputText;
char emptyInputText;
#endif
static char* ptrExprToProcess;
static char* ptrOutput;
static char* ptrSrcString;
static const char* ptrConditionExpr;
static bool errorDisplayed;
static int endValuesProcessed;
static pBatchCallback callback;
static const char* ptrStartExprs[MAX_EXPRESSIONS];
static int nbrExpressions;
static const char* NextExpr;
static const char* EndExpr;
static const char* ptrStartExpr;
static const char* ptrStartQuote;
static const char* ptrEndQuote;
void beginLine(char** pptrOutput)
{
if (!fromFile)
{
copyStr(pptrOutput, "<p>");
}
}
void finishLine(char** pptrOutput)
{
if (fromFile)
{
copyStr(pptrOutput, (lineEndingCRLF ? "\r\n\r\n" : "\n\n"));
}
else
{
copyStr(pptrOutput, "</p>");
}
}
static void stringToHTML(char **pptrOutput, const char *ptrString)
{
const char* ptrStr = ptrString;
int character;
char *ptrOut = *pptrOutput;
for (;;)
{
char c = *ptrStr;
if (c == 0)
{
break; // End of string, so go out.
}
if (((int)c & 0x80) == 0)
{ // 1-byte UTF-8 character
character = c;
ptrStr++;
}
else if (((int)c & 0x60) == 0x40)
{ // 2-byte UTF-8 character
character = (((int)c & 0x1F) * 64) + (*(ptrStr + 1) & 0x3F);
ptrStr += 2;
}
else
{ // 3-byte UTF-8 character
character = (((int)c & 0x1F) * 0x1000) +
(((int)*(ptrStr + 1) & 0x3F) * 0x40) +
((int)*(ptrStr + 2) & 0x3F);
ptrStr += 3;
}
if ((character >= ' ') && (character < 127) && (character != '<') &&
(character != '>') && (character != '&'))
{ // Safe to copy character.
*ptrOut = (char)character;
ptrOut++;
}
else
{ // Insert HTML entity.
*ptrOut = '&';
ptrOut++;
*ptrOut = '#';
ptrOut++;
int2dec(&ptrOut, character);
*ptrOut = ';';
ptrOut++;
}
}
*pptrOutput = ptrOut;
}
static void showErrorInExpr(char** pptrOutput, int rcode)
{
copyStr(pptrOutput, lang ? "Error en la expresión " :
"Error in expression #");
int2dec(pptrOutput, expressionNbr);
copyStr(pptrOutput, ": ");
textError(pptrOutput, rcode);
counterC = 0;
ptrOutput += 4;
}
static enum eExprErr generateRPN(const char* expr, char **ptrRPN, bool varsExpected)
{
enum eExprErr retcode;
const char* ptrInputExpr = expr;
char* ptrOutputExpr = outputExpr;
while (*ptrInputExpr != 0)
{
char c = *ptrInputExpr;
if ((c == ';') || (c == ':'))
{ // End of expression characters for batch mode.
break;
}
// Copy character to output expression.
*ptrOutputExpr = c;
ptrOutputExpr++;
ptrInputExpr++;
}
*ptrOutputExpr = 0; // Append string terminator.
retcode = convertToRPN(expr, ptrRPN, varsExpected);
if (retcode != EXPR_OK)
{
showErrorInExpr(&ptrOutput, retcode);
}
return retcode;
}
static void SkipSpaces(char **pptrText)
{
char *ptrText = *pptrText;
while ((*ptrText == ' ') || (*ptrText == 9))
{ // Skip spaces or tabs.
ptrText++;
}
*pptrText = ptrText;
}
static void SkipSpacesConst(const char** pptrText)
{
const char* ptrText = *pptrText;
while ((*ptrText == ' ') || (*ptrText == 9))
{ // Skip spaces or tabs.
ptrText++;
}
*pptrText = ptrText;
}
static void BatchError(char **pptrOutput, const char *batchText, const char *errorText)
{
char *ptrOut = *pptrOutput;
stringToHTML(&ptrOut, batchText);
*ptrOut = ':';
ptrOut++;
*ptrOut = ' ';
ptrOut++;
copyStr(&ptrOut, errorText);
ptrOut += 4;
*pptrOutput = ptrOut;
counterC = 0;
}
static bool doCallback(const char * ptrExpr, BigInteger *valueFound, int type)
{
enum eExprErr rcode = ComputeExpression(ptrExpr,
valueFound);
if (rcode == EXPR_OK)
{
bool hexBak = hexadecimal;
if ((type & BATCH_MASK_HEX) != 0)
{
hexadecimal = true;
}
else
{
hexadecimal = false;
}
callback(&ptrOutput, type);
hexadecimal = hexBak;
}
else
{
showErrorInExpr(&ptrOutput, rcode);
return true;
}
return false;
}
static const char* missingVariableES[2] =
{
"falta variable x en la primera expresión",
"falta variable x en la segunda expresión",
};
static const char* missingVariableEN[2] =
{
"variable x missing in first expression",
"variable x missing in second expression",
};
static const char *missingSemicolonES[2] =
{
"se esperaban tres o cuatro puntos y comas pero no hay ninguno",
"se esperaban tres o cuatro puntos y comas pero solo hay uno",
};
static const char* missingSemicolonEN[2] =
{
"three or four semicolons expected but none found",
"three or four semicolons expected but there are only one",
};
static const char *missingEqualSignES[2] =
{
"falta signo igual en la primera expresión",
"falta signo igual en la segunda expresión",
};
static const char* missingEqualSignEN[2] =
{
"equal sign missing in first expression",
"equal sign missing in second expression",
};
static bool convertExpressionsToRPN(const char* batchText)
{
enum eExprErr retcode;
char* ptrCharFound;
const char* ptrBatchText = batchText;
char* ptrRPN;
#ifdef __EMSCRIPTEN__
ptrInputText = &emptyInputText;
#endif
// Process first and second expressions "x = expr"
// separated by semicolons.
for (int exprNbr = 0; exprNbr < 2; exprNbr++)
{
SkipSpacesConst(&ptrBatchText);
if ((*ptrBatchText != 'x') && (*ptrBatchText != 'X'))
{
BatchError(&ptrOutput, batchText,
lang ? missingVariableES[exprNbr] : missingVariableEN[exprNbr]);
return false;
}
ptrBatchText++; // Skip variable 'x'.
SkipSpacesConst(&ptrBatchText);
if (*ptrBatchText != '=')
{
BatchError(&ptrOutput, batchText,
lang ? missingEqualSignES[exprNbr] : missingEqualSignEN[exprNbr]);
return false;
}
ptrCharFound = findChar(ptrBatchText + 1, ';');
if (ptrCharFound == NULL)
{
BatchError(&ptrOutput, batchText,
lang ? missingSemicolonES[exprNbr] : missingSemicolonEN[exprNbr]);
return false;
}
ptrBatchText++; // Point after equal (assignment) sign.
expressionNbr = 1;
if (exprNbr == 0)
{ // Generate RPN for first expression "x = expr".
expressionNbr = 1;
retcode = generateRPN(ptrBatchText, &ptrRPN, false);
ptrStartExpr = (const char*)ptrRPN;
}
else
{ // Generate RPN for second expression "x = expr".
expressionNbr = 2;
retcode = generateRPN(ptrBatchText, &ptrRPN, true);
NextExpr = (const char*)ptrRPN;
}
if (retcode != EXPR_OK)
{
return false;
}
ptrBatchText = ptrCharFound + 1;
}
EndExpr = ptrCharFound + 1; // Point to end expression.
ptrCharFound = findChar(EndExpr, ';'); // Find third semicolon.
if (ptrCharFound == NULL)
{ // Third semicolon not found.
BatchError(&ptrOutput, batchText,
lang ? "se esperaban tres o cuatro puntos y comas pero solo hay dos" :
"three or four semicolons expected but there are only two");
return false;
}
// Generate RPN for third expression (end expression).
expressionNbr = 3;
retcode = generateRPN(EndExpr, &ptrRPN, true);
EndExpr = (const char*)ptrRPN;
if (retcode != EXPR_OK)
{
return false;
}
ptrCharFound++;
// Skip spaces.
while ((*ptrCharFound != 0) && (*ptrCharFound <= ' '))
{
ptrCharFound++;
}
ptrExprToProcess = ptrCharFound;
ptrStartQuote = NULL;
ptrEndQuote = NULL;
if (*ptrCharFound == '\"')
{
const char* ptrColon;
nbrExpressions = 0;
ptrStartQuote = ptrCharFound + 1;
ptrEndQuote = ptrStartQuote;
// Find closing quote.
while ((*ptrEndQuote != 0) && (*ptrEndQuote != '\"'))
{
if (*ptrEndQuote == '%')
{
ptrEndQuote++; // Discard character after percent sign.
if (*ptrEndQuote == 0)
{
break; // Go out of loop if sequence % NULL is found.
}
}
ptrEndQuote++;
}
if (*ptrEndQuote == 0)
{
BatchError(&ptrOutput, batchText,
lang ? "falta comilla de cierre" :
"missing closing quote");
return false;
}
// Find number of conversion clauses.
while (ptrCharFound < ptrEndQuote)
{
if (*ptrCharFound == '%')
{
char upper = *(ptrCharFound + 1) & 0xDF;
char upper2 = *(ptrCharFound + 2) & 0xDF;
if ((upper == 'D') || (upper == 'X') || (upper == 'L'))
{ // Decimal, hexadecimal or logic.
ptrCharFound += 2;
if (nbrExpressions == MAX_EXPRESSIONS)
{
BatchError(&ptrOutput, batchText,
lang ? "demasiadas cláusulas de conversion" :
"too many conversion clauses");
return false;
}
nbrExpressions++;
}
else if ((*(ptrCharFound + 1) == '\"') || (*(ptrCharFound + 1) == '%'))
{ // Quote or percent.
ptrCharFound += 2;
}
else if ((upper == 'F') && ((upper2 == 'D') || (upper2 == 'X')))
{ // Factoring with decimal or hexadecimal output.
ptrCharFound += 3;
if (nbrExpressions == MAX_EXPRESSIONS)
{
BatchError(&ptrOutput, batchText,
lang ? "demasiadas cláusulas de conversion" :
"too many conversion clauses");
return false;
}
nbrExpressions++;
}
else
{
BatchError(&ptrOutput, batchText,
lang ? "carácter extraño después de %" :
"strange character after %");
return false;
}
}
else
{
ptrCharFound++;
}
}
// Find start of expressions separated by colons.
ptrColon = ptrEndQuote + 1;
for (int nbrColons = 0; nbrColons < nbrExpressions; nbrColons++)
{
ptrColon = findChar(ptrColon, ':');
if (ptrColon == NULL)
{
BatchError(&ptrOutput, batchText,
lang ? "la cantidad de clásulas de conversión es mayor que la cantidad de dos puntos" :
"the number of conversion clauses is greater than the number of colons");
return false;
}
ptrColon++;
expressionNbr = 4;
retcode = generateRPN(ptrColon, &ptrRPN, true);
ptrStartExprs[nbrColons] = (const char*)ptrRPN;
if (retcode != EXPR_OK)
{
return false;
}
}
ptrConditionExpr = findChar(ptrColon, ';'); // Find optional fourth semicolon.
ptrColon = findChar(ptrColon, ':');
if (ptrColon != NULL)
{
BatchError(&ptrOutput, batchText,
lang ? "la cantidad de clásulas de conversión es menor que la cantidad de dos puntos" :
"the number of conversion clauses is less than the number of colons");
return false;
}
}
else
{ // No quotes.
ptrConditionExpr = findChar(ptrExprToProcess, ';'); // Find optional fourth semicolon.
expressionNbr = 4;
retcode = generateRPN(ptrExprToProcess, &ptrExprToProcess, true);
if (retcode != EXPR_OK)
{
return false;
}
}
if (ptrConditionExpr != NULL)
{
ptrConditionExpr++;
expressionNbr = 5;
retcode = generateRPN(ptrConditionExpr, &ptrRPN, true);
ptrConditionExpr = (const char*)ptrRPN;
if (retcode != EXPR_OK)
{
return false;
}
}
return true;
}
static bool InternalProcessLoop(bool* pIsBatch, const char* batchText,
BigInteger* valueFound)
{
bool retcode = convertExpressionsToRPN(batchText);
if (!retcode)
{
return false;
}
if (!firstExprProcessed)
{
enum eExprErr rc = ComputeExpression(ptrStartExpr,
valueFound);
if (rc != EXPR_OK)
{
expressionNbr = 1;
showErrorInExpr(&ptrOutput, rc);
return false;
}
CopyBigInt(&valueX, valueFound);
firstExprProcessed = true;
}
while (ptrOutput < &output[(int)sizeof(output) - 200000])
{ // Perform loop while there is space in output buffer.
bool processExpression = true;
static enum eExprErr rcode;
expressionNbr = 3;
rcode = ComputeExpression(EndExpr, valueFound);
if (rcode != EXPR_OK)
{
showErrorInExpr(&ptrOutput, rcode);
break; // Cannot compute end expression, so go out.
}
if (BigIntIsZero(valueFound))
{ // End expression result is zero: end of loop
firstExprProcessed = false;
break;
}
if (valuesProcessed >= endValuesProcessed)
{
output[0] = (fromFile ? 'A' : '6'); // Show Continue button.
break;
}
if (ptrConditionExpr != NULL)
{
expressionNbr = 5;
rcode = ComputeExpression(ptrConditionExpr,
valueFound);
if (rcode == EXPR_OK)
{
if (BigIntIsZero(valueFound))
{ // Do not compute factor expression if condition is false.
processExpression = false;
}
}
else
{
showErrorInExpr(&ptrOutput, rcode);
if ((rcode == EXPR_SYNTAX_ERROR) || (rcode == EXPR_VAR_OR_COUNTER_REQUIRED))
{ // Do not show multiple errors.
break;
}
processExpression = false;
}
}
if (processExpression)
{
expressionNbr = 4;
if (ptrStartQuote == NULL)
{
rcode = ComputeExpression(ptrExprToProcess, valueFound);
if (rcode == EXPR_OK)
{
callback(&ptrOutput, BATCH_NO_QUOTE);
}
else
{
showErrorInExpr(&ptrOutput, rcode);
break;
}
}
else
{
int colonNbr = 0;
const char* ptrInsideQuotes = ptrStartQuote;
while (ptrInsideQuotes < ptrEndQuote)
{
if (*ptrInsideQuotes == '%')
{
switch (*(ptrInsideQuotes + 1))
{
case '%':
case '\"':
*ptrOutput = *(ptrInsideQuotes + 1);
ptrOutput++;
ptrInsideQuotes += 2;
break;
case 'd':
case 'D':
if (doCallback(ptrStartExprs[colonNbr], valueFound, BATCH_NO_PROCESS_DEC))
{
return false;
}
colonNbr++;
ptrInsideQuotes += 2;
break;
case 'x':
case 'X':
if (doCallback(ptrStartExprs[colonNbr], valueFound, BATCH_NO_PROCESS_HEX))
{
return false;
}
colonNbr++;
ptrInsideQuotes += 2;
break;
case 'L':
case 'l':
rcode = ComputeExpression(ptrStartExprs[colonNbr], valueFound);
if (rcode == EXPR_OK)
{
if (BigIntIsZero(valueFound))
{
copyStr(&ptrOutput, "no");
}
else
{
copyStr(&ptrOutput, lang ? "sí": "yes");
}
colonNbr++;
ptrInsideQuotes += 2;
}
else
{
showErrorInExpr(&ptrOutput, rcode);
}
break;
default:
switch (*(ptrInsideQuotes + 2))
{
case 'd':
case 'D':
if (doCallback(ptrStartExprs[colonNbr], valueFound, BATCH_PROCESS_DEC))
{
return false;
}
colonNbr++;
ptrInsideQuotes += 3;
break;
default:
if (doCallback(ptrStartExprs[colonNbr], valueFound, BATCH_PROCESS_HEX))
{
return false;
}
colonNbr++;
ptrInsideQuotes += 3;
break;
}
}
}
else
{ // Not %. Copy character to output.
*ptrOutput = *ptrInsideQuotes;
ptrOutput++;
ptrInsideQuotes++;
}
}
}
if ((rcode == EXPR_SYNTAX_ERROR) || (rcode == EXPR_VAR_OR_COUNTER_REQUIRED) ||
(rcode == EXPR_VAR_IN_EXPRESSION))
{ // Do not show multiple errors.
errorDisplayed = true;
break;
}
}
expressionNbr = 2;
rcode = ComputeExpression(NextExpr, valueFound);
if (rcode != EXPR_OK)
{
showErrorInExpr(&ptrOutput, rcode);
break; // Cannot compute next expression, so go out.
}
CopyBigInt(&valueX, valueFound);
counterC++;
if ((counterC == 2) && (pIsBatch != NULL))
{ // Not processing first line: Indicate batch processing.
*pIsBatch = true;
}
if (processExpression)
{
if (fromFile)
{
copyStr(&ptrOutput, (lineEndingCRLF ? "\r\n" : "\n"));
}
else
{
copyStr(&ptrOutput, "</li><li>");
}
valuesProcessed++;
}
}
return true;
}
static bool ProcessLoop(bool* pIsBatch, const char* batchText, BigInteger* valueFound)
{
bool rc;
setInsideExpressionLoop(true);
rc = InternalProcessLoop(pIsBatch, batchText, valueFound);
setInsideExpressionLoop(false);
return rc;
}
enum eExprErr BatchProcessing(char *batchText, BigInteger *valueFound, char **pptrOutput,
bool *pIsBatch, pBatchCallback batchCallback)
{
enum eExprErr rc = EXPR_OK;
errorDisplayed = false;
ptrExprToProcess = NULL;
ptrOutput = output;
ptrConditionExpr = NULL;
callback = batchCallback;
if (fromFile)
{
copyStr(&ptrOutput, "B");
}
else
{
copyStr(&ptrOutput, "2<ul><li>");
}
endValuesProcessed = valuesProcessed + 1000;
if (pIsBatch != NULL)
{
*pIsBatch = false; // Indicate not batch processing in advance.
}
if (valuesProcessed == 0)
{ // Start batch factorization.
ptrCurrBatchFactor = batchText;
ptrEndBatchFactor = batchText + strlen(batchText);
firstExprProcessed = false;
}
for (; ptrCurrBatchFactor < ptrEndBatchFactor;
ptrCurrBatchFactor += (int)strlen(ptrCurrBatchFactor) + 1)
{ // Get next line. Line finishes with \r\n or \n only.
char c;
if ((ptrCurrBatchFactor != batchText) && (pIsBatch != NULL))
{ // Not processing first line: Indicate batch processing.
*pIsBatch = true;
}
expressionNbr = 0;
if (firstExprProcessed == false)
{
valueX.nbrLimbs = 0; // Invalidate variable x and counter c.
ptrNextBatchFactor = ptrCurrBatchFactor;
while ((*ptrNextBatchFactor != '\0') && (*ptrNextBatchFactor != '\r') &&
(*ptrNextBatchFactor != '\n'))
{
ptrNextBatchFactor++;
}
if (*ptrNextBatchFactor == '\n')
{ // One-byte end of line.
lineEndingCRLF = true;
}
else
{
if ((*ptrNextBatchFactor == '\r') && (*(ptrNextBatchFactor+1) == '\n'))
{ // Two-byte end of line.
lineEndingCRLF = true;
*ptrNextBatchFactor = ' '; // Dummy space to replace first byte of EOL.
ptrNextBatchFactor++;
}
}
*ptrNextBatchFactor = 0; // Indicate end of line.
counterC = 1;
}
// Skip leading spaces.
ptrSrcString = ptrCurrBatchFactor;
SkipSpaces(&ptrSrcString);
c = *ptrSrcString;
if (c == 0)
{ // Empty line.
copyStr(&ptrOutput, "<br>");
}
else if (c == '#')
{ // Copy comment to output, but convert non-safe characters to entities.
*ptrOutput = '#';
ptrOutput++;
stringToHTML(&ptrOutput, ptrCurrBatchFactor + 1);
if (fromFile)
{
copyStr(&ptrOutput, (lineEndingCRLF ? "\r\n" : "\n"));
}
else
{
copyStr(&ptrOutput, "<li>");
}
}
else if ((c == 'x') || (c == 'X'))
{ // Loop format: x=<orig expr>; x=<next expr>; <end expr>; <expr to factor>[; <factor cond>]
if (!ProcessLoop(pIsBatch, ptrCurrBatchFactor, valueFound))
{
continue;
}
if (valuesProcessed >= endValuesProcessed)
{
break;
}
}
else
{ // Factor expression (not loop).
#ifdef __EMSCRIPTEN__
ptrInputText = ptrSrcString;
#endif
if (valuesProcessed >= endValuesProcessed)
{
output[0] = (fromFile ? 'A' : '6'); // Show Continue button.
}
rc = ComputeExpression(ptrSrcString, valueFound);
if (rc == EXPR_OK)
{
callback(&ptrOutput, BATCH_NO_QUOTE);
}
else
{
if (rc == EXPR_VAR_IN_EXPRESSION)
{
copyStr(&ptrOutput, lang ? "Variable inesperada en la expresión." :
"Unexpected variable in expression.");
}
else
{
textError(&ptrOutput, rc);
}
}
counterC = -1;
if (fromFile)
{
copyStr(&ptrOutput, (lineEndingCRLF ? "\r\n" : "\n"));
}
else
{
copyStr(&ptrOutput, "</li><li>");
}
valuesProcessed++;
}
if (counterC == 1)
{
if (fromFile)
{
copyStr(&ptrOutput, (lineEndingCRLF ? "\r\n" : "\n"));
}
else
{
copyStr(&ptrOutput, "</li>");
}
}
if (ptrOutput >= &output[(int)sizeof(output) - 200000])
{
output[0] = (fromFile ? 'A' : '6'); // Show Continue button.
break;
}
if ((*ptrCurrBatchFactor & 0xDF) == 'X')
{ // Loop mode.
if (ptrConditionExpr != NULL)
{
ptrCurrBatchFactor += strlen(ptrConditionExpr);
ptrConditionExpr = NULL;
}
else
{
ptrCurrBatchFactor += strlen(ptrExprToProcess);
}
ptrCurrBatchFactor++;
}
valueX.nbrLimbs = 0; // Invalidate variable x and counter c.
}
if (!fromFile)
{
ptrOutput -= 4; // Erase start tag <li> without contents.
}
if (counterC == 1)
{
if (!fromFile)
{
ptrOutput--; // Erase extra character of </li>.
}
if (!errorDisplayed)
{
copyStr(&ptrOutput, lang ? "No hay valores para la expresión ingresada." :
"There are no values for the requested expression.");
}
}
if (fromFile)
{
copyStr(&ptrOutput, (lineEndingCRLF ? "\r\n" : "\n"));
}
else
{
copyStr(&ptrOutput, "</ul>");
}
*pptrOutput = ptrOutput;
return rc;
}
char *findChar(const char *str, char c)
{
const char* ptrStr = str;
while (*ptrStr != '\0')
{
if (*ptrStr == c)
{
return (char *)ptrStr; // Pointer to character found.
}
ptrStr++;
}
return NULL; // Character not found.
}