File: strings.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (1028 lines) | stat: -rw-r--r-- 33,100 bytes parent folder | download | duplicates (4)
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/****************************************************************************/
/*                                                                          */
/* Fenix - Videogame compiler/interpreter                                   */
/* Current release       : PROJECT 1.0 - 0.84                               */
/* Last stable release   :                                                                          */
/* Project documentation : http://fenix.divsite.net                         */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/* This program 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 2 of the License, or        */
/* (at your option) any later version.                                      */
/*                                                                          */
/* This program 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 this program; if not, write to the Free Software              */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */
/*                                                                          */
/****************************************************************************/
/* Copyright � 1999 Jos� Luis Cebri�n Pag�e                                 */
/* Copyright � 2002 FENIX PROJECT 1.0 TEAM                                  */
/****************************************************************************/

/****************************************************************************/
/* FILE        : strings.c                                                  */
/* DESCRIPTION : Strings management. Includes any function related to       */
/*               variable-length strings. Those strings are allocated       */
/*               in dynamic memory with reference counting.                 */
/****************************************************************************/
/* HISTORY     : 29/01/2001 (jlceb) the pointer/reference/dontfree arrays   */
/*                  are now of dynamic size. Some comments written.         */
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif

#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include "fxi.h"
#include "dcb.h"

/****************************************************************************/
/* GLOBAL VARIABLES :                                                       */
/****************************************************************************/

/* 1 to debug string operations to the console (lots of text) */
int report_string = 0 ;
/*
#undef gr_con_printf
#define gr_con_printf(...) printf(__VA_ARGS__);fflush(stdout);
*/
/****************************************************************************/
/* STATIC VARIABLES :                                                       */
/****************************************************************************/

/* Fixed string memory. The DCB fixed strings are stored here */
static char   * string_mem = 0 ;
static int      string_allocated = 0 ;
static int      string_used = 0 ;

/* Pointers to each string's text. Every string is allocated using strdup()
   or malloc(). A pointer of a unused slot is 0. Exception: "fixed" strings
   are stored in a separate memory block and should not be freed */
static char  ** string_ptr ;

/* Usage count for each string. An unused slot has a count of 0 */
static int    * string_uct ;

/* Fixed-memory flag: 1 for "fixed" strings */
static char   * string_dontfree ;

/* How many strings slots are used. This is only the bigger id in use + 1.
   There may be unused slots in this many positions */
static int      string_count = 1 ;

/* How many string slots are available in the ptr, uct and dontfree arrays */
static int      string_ptr_allocated = 0 ;

/****************************************************************************/
/* FUNCTION : string_init                                                   */
/****************************************************************************/
/* Allocate memory for the dynamic arrays. You should call this function    */
/* before anything else in this file. There is enough space for about       */
/* 1024 short strings, that should be enough for simple programs. More      */
/* space is allocated as needed.                                            */
/****************************************************************************/

void string_init ()
{
    string_mem = (char *) malloc (4096) ;
    string_allocated = 4096 ;
    string_used = 0 ;

    string_ptr_allocated = 1024 ;
    string_ptr = (char **) calloc (1024, sizeof(char *)) ;
    string_uct = (int *) calloc (1024, sizeof(int)) ;
    string_dontfree = (char *) calloc (1024, sizeof(char)) ;

    /* Create an empty string with ID 0 */
    string_mem[0] = 0 ;
    string_ptr[0] = string_mem ;

    string_count = 1 ;
}

/****************************************************************************/
/* FUNCTION : string_alloc                                                  */
/****************************************************************************/
/* int bytes: how many new strings we could need                            */
/****************************************************************************/
/* Increase the size of the internal string arrays. This limits how many    */
/* strings you can have in memory at the same time, and this should be      */
/* called when every identifier slot available is already used.             */
/****************************************************************************/

static void string_alloc (int count)
{
    int new_allocated = string_ptr_allocated + count ;

    string_ptr = (char **) realloc (string_ptr, new_allocated * sizeof(char *)) ;
    string_uct = (int *) realloc (string_uct, new_allocated * sizeof(int)) ;
    string_dontfree = (char *) realloc (string_dontfree, new_allocated * sizeof(char)) ;

    if (!string_ptr || !string_uct || !string_dontfree)
        gr_error (_("string_alloc: out of memory\n")) ;

    if (count > 0)
    {
        memset (string_ptr + string_ptr_allocated, 0, count * sizeof (char *));
        memset (string_uct + string_ptr_allocated, 0, count * sizeof (int));
        memset (string_dontfree + string_ptr_allocated, 0, count * sizeof (char));
    }

    string_ptr_allocated = new_allocated;
}

/****************************************************************************/
/* FUNCTION : string_dump                                                   */
/****************************************************************************/
/* Shows all the strings in memory in the console, including the reference  */
/* count (usage count) of each string.                                      */
/****************************************************************************/

void string_dump ()
{
    int i ;
    int used=0;

    gr_con_printf (_("[STRING] ---- Dumping MaxID=%d strings ----\n"), string_count) ;

    for (i = 0 ; i < string_count ; i++){
        if (string_ptr[i]){
            if (string_uct[i] == 0){
                if (!string_dontfree[i]){
                    free(string_ptr[i]) ;
                    string_ptr[i] = NULL ; // Splinter
                    continue ;
                }
            }
            used++;
        } else {
            continue ;
        }
        gr_con_printf (_("[STRING] %4d %1d [%4d]: {%s}\n"), i, string_uct[i], string_dontfree[i], string_ptr[i]) ;
    }
    gr_con_printf (_("[STRING] ---- Dumping Used=%d End ----\n"), used) ;
}

/****************************************************************************/
/* FUNCTION : string_get                                                    */
/****************************************************************************/
/* int code: identifier of the string you want                              */
/****************************************************************************/
/* Returns the contens of an string. Beware: this pointer with only be      */
/* valid while no other string function is called.                          */
/****************************************************************************/

const char * string_get (int code)
{
    assert (code < string_count && code >= 0) ;
    if (report_string){
        gr_con_printf (_("[STRING] string_get %d\n"), code) ;
    }
    return string_ptr[code] ;
}

/****************************************************************************/
/* FUNCTION : string_load                                                   */
/****************************************************************************/
/* file * fp: the DCB file (must be opened)                                 */
/*                                                                          */
/* This function uses the global "dcb" struct. It should be already filled. */
/****************************************************************************/
/* Loads the string portion of a DCB file. This includes an area with all   */
/* the text (that will be stored in the string_mem pointer) and an array of */
/* the offsets of every string. This function fills the internal arrayswith */
/* all this data and allocates memory if needed.                            */
/****************************************************************************/

void string_load (file * fp)
{
    int * string_offset, n;

    string_count = dcb.data.NStrings ;
    string_used  = dcb.data.SText ;
    file_seek (fp, dcb.data.OStrings, SEEK_SET) ;
    string_offset = (int *) malloc (4 * string_count) ;
    if (!string_offset) {
        gr_error (_("string_load: not enough memory\n")) ;
    }
    file_read (fp, string_offset, 4 * string_count) ;
    if (string_used > string_allocated)
    {
        string_allocated = string_used ;
        string_mem = (char *) realloc (string_mem, string_used) ;
    }
    if (string_count + 128 > string_ptr_allocated) {
        string_alloc (string_count + 512 - string_ptr_allocated) ;
    }

    file_seek (fp, dcb.data.OText, SEEK_SET) ;
    file_read (fp, string_mem, string_used) ;

    for (n = 0 ; n < string_count ; n++)
    {
        string_ptr[n]       = string_mem + string_offset[n] ;
        string_uct[n]       = 0 /*25*/ ; // -- Fix Splinter
        string_dontfree[n]  = 1 ;
    }

    string_ptr[n]       = NULL ;
    string_uct[n]       = 0 ; // -- Fix Splinter

    free (string_offset) ;
}

/****************************************************************************/
/* FUNCTION : string_use                                                    */
/****************************************************************************/
/* int code: identifier of the string you are using                         */
/****************************************************************************/
/* Increase the usage counter of an string. Use this when you store the     */
/* identifier of the string somewhere.                                      */
/****************************************************************************/

void string_use (int code)
{
    string_uct[code]++ ;
    if (report_string) {
        gr_con_printf (_("[STRING] String %d used (count: %d)\n"), code, string_uct[code]) ;
    }
}

/****************************************************************************/
/* FUNCTION : string_discard                                                */
/****************************************************************************/
/* int code: identifier of the string you don't need anymore                */
/****************************************************************************/
/* Decrease the usage counter of an string. Use this when you retrieve the  */
/* identifier of the string and discard it, or some memory (like private    */
/* variables) containing the string identifier is destroyed. If the usage   */
/* count is decreased to zero, the string will be discarted, and the        */
/* identifier may be used in the future by other string.                    */
/****************************************************************************/

void string_discard (int code)
{
    if ( code < 0 || code > string_count || !string_ptr[code]) {
        return;
    }

    if (string_uct[code] < 1)
    {
        if (report_string) {
            gr_con_printf (_("[STRING] string_discard: String %d released but already discarded\n"), code) ;
        }
        return ;
    }

    string_uct[code]-- ;

    if (report_string) {
        gr_con_printf (_("[STRING] string_discard: String %d released (count: %d)\n"), code, string_uct[code]) ;
    }

    if ( string_uct[code] < 1 )
    {
        if (report_string) {
            gr_con_printf (_("[STRING] string_discard: String %d released and discarded\n"), code) ;
        }

        if (!string_dontfree[code]) {
            free(string_ptr[code]) ;
            string_ptr[code] = NULL ; // Splinter
            string_uct[code] = 0 ;
        }

        if (report_string ) {
            if ( string_dontfree[code]) {
                gr_con_printf (_("[STRING] string_discard: (Memory could not be freed - %d is special string, count: %d)\n"), code, string_uct[code]) ;
            } else {
                gr_con_printf (_("[STRING] string_discard: String %d released and discarded (count: %d)\n"), code, string_uct[code]) ;
            }
        }
    }
}

/****************************************************************************/
/* FUNCTION : string_coalesce                                               */
/****************************************************************************/
/* Does some garbage collection (frees any memory used by unreferenced      */
/* strings). This should not be necessary, because strings are now discarted*/
/* inmediately when its usage counts is zero.                               */
/****************************************************************************/

void string_coalesce()
{
    int n ;

    if (string_count < string_ptr_allocated/2)
        return ;

    for (n = 1 ; n < string_ptr_allocated ; n++)
    {
        if (!string_uct[n])
        {
            if (!string_dontfree[n]) {
                free (string_ptr[n]) ;
                string_ptr[n] = NULL ; // Splinter
            }
        }
    }
}

/****************************************************************************/
/* FUNCTION : string_getid                                                  */
/****************************************************************************/
/* Searchs for an available ID and returns it. If none available, more space*/
/* is allocated for the new string. This is used for new strings only.      */
/****************************************************************************/

static int string_getid ()
{
    int n ;

    // Si tengo suficientes alocados, retorno el siguiente segun string_count
    if (string_count < string_ptr_allocated) {
        return string_count++ ;
    }

    // Ya no tengo mas espacio, entonces busco alguno libre
    for (n = 1 ; n < string_ptr_allocated ; n++) {
        if (!string_ptr[n]) {
            return n ;
        }
    }
    // Incremento espacio
    string_alloc (1024) ;
    gr_con_printf (_("[STRING] �12*PANIC�7 Too many strings, allocating more space")) ;

    // Devuelvo el string_count + 1, ya que ahora tengo 1024 mas que antes
    return string_count++ ;
}

/****************************************************************************/
/* FUNCTION : string_new                                                    */
/****************************************************************************/
/* Create a new string. It returns its ID. Note that it uses strdup()       */
/* TODO: do something if no memory available                                */
/****************************************************************************/

int string_new (const char * ptr)
{
    char * str = strdup(ptr) ;
    int    id ;

    assert (str) ;
    id = string_getid() ;

    if (report_string) {
        gr_con_printf (_("[STRING] String %d created: \"%s\"\n"), id, str) ;
    }

    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;
    return id ;
}

/*
 *  FUNCTION : string_newa
 *
 *  Create a new string from a text buffer section
 *
 *  PARAMS:
 *              ptr         Pointer to the text buffer at start position
 *              count       Number of characters
 *
 *  RETURN VALUE:
 *      ID of the new string
 */

int string_newa (const char * ptr, unsigned count)
{
    char * str = malloc(count+1);
    int    id ;

    assert (str) ;
    id = string_getid() ;

    strncpy (str, ptr, count);
    str[count] = 0;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "newa", id, str) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_concat                                                 */
/****************************************************************************/
/* Add some text to an string and return the resulting string. This does not*/
/* modify the original string, but creates a new one.                       */
/****************************************************************************/

int string_concat (int code1, char * str2)
{
    char * str1 ;

    assert (code1 < string_count && code1 >= 0) ;

    str1 = string_ptr[code1] ;
    assert (str1) ;

    str1 = (char *) realloc(str1, strlen(str1) + strlen(str2) + 1) ;
    assert (str1) ;

    strcat (str1, str2) ;

    string_ptr[code1] = str1 ;
    return code1 ;
}

/****************************************************************************/
/* FUNCTION : string_add                                                    */
/****************************************************************************/
/* Add an string to another one and return the resulting string. This does  */
/* not modify the original string, but creates a new one.                   */
/****************************************************************************/

int string_add (int code1, int code2)
{
    const char * str1 = string_get(code1) ;
    const char * str2 = string_get(code2) ;
    char * str3 ;
    int id ;

    assert (str1) ;
    assert (str2) ;

    str3 = (char *) malloc(strlen(str1) + strlen(str2) + 1) ;
    assert (str3) ;

    strcpy (str3, str1) ;
    strcat (str3, str2) ;
    id = string_getid() ;

    string_ptr[id] = str3 ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "add", id, str3) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_ptoa                                                   */
/****************************************************************************/
/* Convert a pointer to a new created string and return its ID.             */
/****************************************************************************/

int string_ptoa (void * n)
{
    char * str ;
    int id ;

    str = (char *) malloc(64) ;
    assert (str)  ;

    sprintf (str, "%p", n) ;

    id = string_getid() ;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "ptoa", id, str) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_ftoa                                                   */
/****************************************************************************/
/* Convert a float to a new created string and return its ID.               */
/****************************************************************************/

int string_ftoa (float n)
{
    char * str, * ptr ;
    int id ;

    str = (char *) malloc(64) ;
    assert (str)  ;

    sprintf (str, "%f", n) ;
    ptr = str + strlen(str) - 1 ;
    while (ptr >= str)
    {
        if (*ptr != '0') break ;
        *ptr-- = 0 ;
    }
    if (ptr >= str && *ptr == '.') *ptr = 0 ;
    if (*str == 0) strcpy (str, "0") ;

    id = string_getid() ;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "ftoa", id, str) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_itoa                                                   */
/****************************************************************************/
/* Convert an integer to a new created string and return its ID.            */
/****************************************************************************/

int string_itoa (int n)
{
    char * str ;
    int id ;

    str = (char *) malloc(32) ;
    assert (str)  ;

    sprintf (str, "%d", n) ;

    id = string_getid() ;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "itoa", id, str) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_uitoa                                                  */
/****************************************************************************/
/* Convert an unsigned integer to a new created string and return its ID.   */
/****************************************************************************/

int string_uitoa (unsigned int n)
{
    char * str ;
    int id ;

    str = (char *) malloc(32) ;
    assert (str)  ;

    sprintf (str, "%u", n) ;

    id = string_getid() ;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "uitoa", id, str) ;
    }

    return id ;
}

/****************************************************************************/
/* FUNCTION : string_comp                                                   */
/****************************************************************************/
/* Compare two strings using strcmp and return the result                   */
/****************************************************************************/

int string_comp (int code1, int code2)
{
    const char * str1 = string_get(code1) ;
    const char * str2 = string_get(code2) ;

    return strcmp (str1, str2) ;
}

/****************************************************************************/
/* FUNCTION : string_char                                                   */
/****************************************************************************/
/* Extract a character from a string. The parameter nchar can be:           */
/* - 0 or positive: return this character from the left (0 = leftmost)      */
/* - negative: return this character from the right (-1 = rightmost)        */
/* The result is not the ASCII value, but the identifier of a new string    */
/* that is create in the process and contains only the extracted character  */
/****************************************************************************/

int string_char (int n, int nchar)
{
    const char * str = string_get(n) ;
    int          len ;
    char buffer[2] ;

    assert (str) ;
    len = strlen(str) ;

    if (nchar >= len)
        nchar = len ;

    if (nchar < 0)
        nchar = len + nchar ;

    if (nchar < 0)
        nchar = len ;

    buffer[0] = str[nchar] ;
    buffer[1] = 0 ;

    return string_new (buffer) ;
}

/****************************************************************************/
/* FUNCTION : string_substr                                                 */
/****************************************************************************/
/* Extract a substring from a string. The parameters can be:                */
/* - 0 or positive: count this character from the left (0 = leftmost)       */
/* - negative: count this character from the right (-1 = rightmost)         */
/*                                                                          */
/* NO MORE: If first > last, the two values are swapped before returning the result  */
/****************************************************************************/

int string_substr (int code, int first, int len)
{
    const char * str = string_get(code) ;
    char       * ptr ;
    int          rlen, n ;

    assert (str) ;
    rlen = strlen(str) ;

    if (first < 0) first = rlen + first ;
    if (first < 0) first = 0 ;
    if (first > (rlen - 1)) return string_new("") ;

    if (len < 0) {
        len = rlen + (len + 2) - first - 1 ;
        if (len < 1) return string_new("") ;
    }

    if ((first + len) > rlen) len = (rlen - first) ;

    ptr = (char *)malloc(len+1) ;
    memcpy (ptr, str + first, len) ;
    ptr[len]            = 0 ;

    n                   = string_getid() ;
    string_ptr[n]       = ptr ;
    string_uct[n]       = 0 ;
    string_dontfree[n]  = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "substr", n, ptr) ;
    }

    return n ;
}


/*
 *  FUNCTION : string_find
 *
 *  Find a substring. Returns the position of the leftmost character (0
 *  for the leftmost position) or -1 if the string was not found.
 *
 *  PARAMS:
 *              code1                   Code of the string
 *              code2                   Code of the substring
 *              first                   Character to start the search
 *                                      (negative to search backwards)
 *
 *  RETURN VALUE:
 *      Result of the comparison
 */

int string_find (int code1, int code2, int first)
{
    const char * str1 = string_get(code1) ;
    const char * str2 = string_get(code2) ;
    int pos, len1, len ;

    assert (str1 && str2) ;
    len1 = strlen(str1);
    len = strlen(str2) ;

    pos = first;
    if (pos < 0)
    {
        pos = len1 + pos;
        if (pos < 0)
            pos = 0;
    }
    if (pos > len1)
        pos = len1;

    for ( ; str1[pos] && pos >= 0 ; first >= 0 ? pos++ : pos--)
    {
        if (memcmp(str1+pos, str2, len) == 0)
            return pos ;
    }
    return -1 ;
}

/*
 *  FUNCTION : string_ucase
 *
 *  Convert an string to upper case. It does not alter the given string, but
 *  creates a new string in the correct case and returns its id.
 *
 *  PARAMS:
 *              code                    Internal code of original string
 *
 *  RETURN VALUE:
 *      Code of the resulting string
 */

int string_ucase (int code)
{
    const char * str = string_get(code) ;
    char       * bptr, * ptr ;
    int          id ;

    assert (str) ;
    bptr = (char *)malloc(strlen(str)+1) ;
    assert (bptr) ;

    for (ptr = bptr; *str ; ptr++, str++)
        *ptr = TOUPPER(*str) ;

    *ptr = 0 ;
    id = string_getid() ;
    string_ptr[id] = bptr ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "ucase", id, bptr) ;
    }

    return id ;
}

/*
 *  FUNCTION : string_lcase
 *
 *  Convert an string to lower case. It does not alter the given string, but
 *  creates a new string in the correct case and returns its id.
 *
 *  PARAMS:
 *              code                    Internal code of original string
 *
 *  RETURN VALUE:
 *      Code of the resulting string
 */

int string_lcase (int code)
{
    const char * str = string_get(code) ;
    char       * bptr, * ptr ;
    int          id ;

    assert (str) ;
    bptr = (char *)malloc(strlen(str)+1) ;
    assert (bptr) ;

    for (ptr = bptr; *str ; ptr++, str++)
        *ptr = TOLOWER(*str) ;

    *ptr = 0 ;
    id = string_getid() ;
    string_ptr[id] = bptr ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "lcase", id, bptr) ;
    }

    return id ;
}

/*
 *  FUNCTION : string_strip
 *
 *  Create a copy of a string, without any leading or ending blanks
 *
 *  PARAMS:
 *              code                    Internal code of original string
 *
 *  RETURN VALUE:
 *      Code of the resulting string
 */

int string_strip (int code)
{
    const char * ptr = string_get(code) ;
    char *       ostr;
    char *       bptr;
    int          id = string_new(ptr);

    ostr = (char *)string_get(id) ;
    bptr = ostr;
    assert (bptr);

    while (*ptr == ' ' || *ptr == '\n' || *ptr == '\r' || *ptr == '\t')
        ptr++;

    while (*ptr)
        *bptr++ = *ptr++;

    while (bptr > ostr && (bptr[-1] == ' ' || bptr[-1] == '\n' || bptr[-1] == '\r' || bptr[-1] == '\t'))
        bptr--;

    *bptr = 0;

    return id ;
}

/*
 *  FUNCTION : string_format
 *
 *  Format a number using the given characters
 *
 *  PARAMS:
 *              number                  Number to format
 *              spec                    Format specification
 *
 *  RETURN VALUE:
 *      Code of the resulting string
 */

int string_format (double number, int dec, char point, char thousands)
{
    char buffer[128];
    char buffer2[256];
    char * s, * t;
    int c;

    int negative = 0;

    if (number < 0)
    {
        negative = 1;
//        number = -number;
    }

    sprintf (buffer, "%.*f", dec, number);
    s = buffer + strlen(buffer)-1;
    t = buffer2 + 127;
    *t-- = 0;
    if (strchr (buffer, '.'))
    {
        if (dec < 0)
        {
            while (*s == '0' && s >= buffer)
                s--;
            if (*s == '.')
                *s-- = 0;
        }
        if (strchr (buffer, '.'))
        {
            while (s >= buffer)
            {
                if (*s == '.')
                {
                    *t-- = point;
                    s--;
                    break;
                }
                *t-- = *s--;
            }
        }
    }
    c = 0;
    while (s >= buffer)
    {
        *t-- = *s-- ;
        if (c == 2 && s >= buffer)
        {
            *t-- = thousands ;
            c = 0;
        }
        else c++;
    }
    return string_new (t+1);
}

/*
 *  FUNCTION : string_casecmp
 *
 *  Compare two strings (case-insensitive version)
 *
 *  PARAMS:
 *              code1                   Code of the first string
 *              code2                   Code of the second string
 *
 *  RETURN VALUE:
 *      Result of the comparison
 */

int string_casecmp (int code1, int code2)
{
    const unsigned char * str1 = string_get(code1) ;
    const unsigned char * str2 = string_get(code2) ;

    while (*str1 || *str2)
    {
        if (TOUPPER(*str1) != TOUPPER(*str2))
            return TOUPPER(*str1) - TOUPPER(*str2);

        str1++;
        str2++;
    }

    return 0 ;
}

/*
 *  FUNCTION : string_pad
 *
 *  Add spaces to the left or right of a string
 *
 *  PARAMS:
 *              code                    Code of the string
 *              total                   Total length of the final string
 *              align                   0 = align to the right; 1 = align to the left
 *
 *  RETURN VALUE:
 *      Result of the comparison
 */

int string_pad (int code, int total, int align)
{
    const char * ptr = string_get(code);

    int    len;
    int    spaces = 0;
    int    id;
    char * str;

    assert(ptr);
    len = strlen(ptr);
    if (len < total)
        spaces = total - len;

    if (!spaces) return string_new(ptr) ;

    str = malloc(total+1);
    assert (str);

    if (!align)
    {
        memset (str, ' ', spaces);
        strcpy (str + spaces, ptr) ;
    }
    else
    {
        strcpy (str, ptr) ;
        memset (str + len, ' ', spaces) ;
        str[total] = 0;
    }

    id = string_getid() ;
    string_ptr[id] = str ;
    string_uct[id] = 0 ;
    string_dontfree[id] = 0 ;

    if (report_string){
        gr_con_printf (_("[STRING] (%s) String %d created: \"%s\"\n"), "pad", id, str) ;
    }

    return id ;
}