c8a0deccd657a79e2f922fbc63bb6a3c43256d9f
[scilab.git] / scilab / modules / fileio / src / cpp / scilab_sscanf.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2011 - DIGITEO - Cedric DELAMARRE
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include "types.hxx"
17 #include "double.hxx"
18 #include "string.hxx"
19 #include "scilab_sscanf.hxx"
20 #include "int.hxx"
21
22 extern "C"
23 {
24 #include <stdio.h>
25 #include "Scierror.h"
26 #include "sci_malloc.h"
27 #include "localization.h"
28 #include "charEncoding.h"
29 #include "BOOL.h"
30 #include "os_wcstok.h"
31 }
32
33 static wchar_t* findChars(wchar_t*, BOOL*); // Use to find what is inside the [] when the format is %[...].
34
35 int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _iNiter, std::vector<types::InternalType*> *_pITOut)
36 {
37     int i                       = 0;
38     int j                       = 0;
39     int nbrOfDigit              = 0;
40     int dims                    = 2;
41     int dimsArray[2]            = {_iNiter, 1};
42     BOOL bStar                  = FALSE;
43     BOOL bUnsigned              = FALSE;
44     BOOL bIgnoredChars          = TRUE;
45     int base                    = 0;
46     wchar_t wcsLLH              = L' ';
47     wchar_t* wcsData            = NULL;
48     int sizeOfData              = (int)wcslen(_wcsData);
49     int iCountDataRead          = 0;
50
51     wcsData = (wchar_t*)MALLOC((sizeOfData + 1) * sizeof(wchar_t));
52     memcpy(wcsData, _wcsData, sizeOfData * sizeof(wchar_t));
53     wcsData[sizeOfData] = '\0';
54
55     while (i < (int)wcslen(_wcsFormat))
56     {
57         while (bIgnoredChars && i < (int)wcslen(_wcsFormat)) // %da%s => 'a' is an ignored char.
58         {
59             if (wcsData != NULL && wcsData[0] != L'\0' && _wcsFormat[i] == wcsData[0])
60             {
61                 if (_wcsFormat[i] != L' ')
62                 {
63                     i++;
64                     wcsData++;
65                 }
66                 else
67                 {
68                     while (wcsData[0] == L' ')
69                     {
70                         wcsData++;
71                     }
72                     while (_wcsFormat[i] == L' ')
73                     {
74                         i++;
75                     }
76                 }
77             }
78             else if ((wcsData == NULL || wcsData[0] == L'\0') && i < (int)wcslen(_wcsFormat) && iCountDataRead == 0 && _pITOut->size() == 0)
79             {
80                 iCountDataRead = -1;
81             }
82             else
83             {
84                 if (_wcsFormat[i] == L' ')
85                 {
86                     do
87                     {
88                         i++;
89                     }
90                     while (i < (int)wcslen(_wcsFormat) && _wcsFormat[i] == L' ');
91                 }
92
93                 if (_wcsFormat[i] != L'%')
94                 {
95                     wcsData = NULL;
96                     while (i < (int)wcslen(_wcsFormat) && _wcsFormat[i] != L'%')
97                     {
98                         i++;
99                     }
100                 }
101                 break;
102             }
103         }
104         if (i == (int)wcslen(_wcsFormat))
105         {
106             break;
107         }
108
109         if (iswdigit(_wcsFormat[i]))
110         {
111             nbrOfDigit = wcstol(&_wcsFormat[i], NULL, 10);
112             while (iswdigit(_wcsFormat[i]))
113             {
114                 i++;
115             }
116         }
117         else switch (_wcsFormat[i])
118             {
119                 case L' ' :
120                 case L'\n':
121                 case L'\t':
122                     i++;
123                     break;
124                 case L'%' :
125                     i++;
126                     bIgnoredChars = FALSE;
127                     break;
128                 case L'*' :
129                     bStar = TRUE;
130                     i++;
131                     break;
132                 case L'h' :
133                 case L'l' :
134                 case L'L' :
135                     wcsLLH = _wcsFormat[i];
136                     i++;
137                     break;
138                 case L'c' :
139                 {
140                     if (wcsData != NULL && wcsData[0] != L'\0') // If the end of data has not been reached we can get datas.
141                     {
142                         wchar_t wcSingleData[2];
143                         wcSingleData[0] = wcsData[0];
144                         wcSingleData[1] = 0;
145
146                         if (!bStar) // If this format is not ignored put the datas found.
147                         {
148                             if (_iIterrator == 0) // Create and initialize the container only the first time.
149                             {
150                                 types::String* pS = new types::String(dims, dimsArray);
151                                 for (int k = 0 ; k < pS->getSize(); k++)
152                                 {
153                                     pS->set(k, L"");
154                                 }
155                                 _pITOut->push_back(pS);
156                             }
157                             (*_pITOut)[j]->getAs<types::String>()->set(_iIterrator, wcSingleData);
158                             iCountDataRead++;
159                         }
160                         wcsData++;
161                     }
162                     else
163                     {
164                         if (_iIterrator == 0 && !bStar && _iNiter == 1)
165                         {
166                             _pITOut->push_back(types::Double::Empty());
167                         }
168                         else
169                         {
170                             FREE(wcsData);
171                             return -10;
172                         }
173                     }
174                     if (!bStar)
175                     {
176                         j++;
177                     }
178                     i++;
179                     bIgnoredChars = TRUE;
180                     bStar = FALSE;
181                 }
182                 break;
183                 case L's' :
184                 {
185                     if (wcsData != NULL && wcsData[0] != L'\0')
186                     {
187                         wchar_t* wcsSingleData  = NULL;
188                         wchar_t* wcsRes         = NULL;
189                         wchar_t seps[]          = L" \t\n";
190                         int sizeOfCurrentData   = (int)wcslen(wcsData);
191                         wchar_t* wcsTemp        = (wchar_t*)MALLOC((sizeOfCurrentData + 1) * sizeof(wchar_t));
192
193                         memcpy(wcsTemp, wcsData, sizeOfCurrentData * sizeof(wchar_t));
194                         wcsTemp[sizeOfCurrentData] = L'\0';
195                         wcsRes = os_wcstok(wcsTemp, seps, &wcsTemp); // the seps[] found is replaced by the '\0' char.
196
197                         if (wcsTemp == NULL || wcsTemp[0] == L'\0')
198                         {
199                             wcsData = NULL;
200                         }
201                         else
202                         {
203                             wcsData += (wcslen(wcsData) - wcslen(wcsTemp) - 1); // set the pointer on the seps[] and not on the next char.
204                         }
205
206                         if (nbrOfDigit) // Get only the numbers of digit indicated in the format. (ex: %2d)
207                         {
208                             wcsSingleData = (wchar_t*)MALLOC(sizeof(wchar_t) * (nbrOfDigit + 1));
209                             memcpy(wcsSingleData, wcsRes, sizeof(wchar_t) * nbrOfDigit);
210                             wcsSingleData[nbrOfDigit] = L'\0';
211                             nbrOfDigit = 0;
212                         }
213                         else // Get all data find.
214                         {
215                             wcsSingleData = wcsRes;
216                         }
217
218                         if (!bStar)
219                         {
220                             if (_iIterrator == 0)
221                             {
222                                 types::String* pS = new types::String(dims, dimsArray);
223                                 for (int k = 0 ; k < pS->getSize(); k++)
224                                 {
225                                     pS->set(k, L"");
226                                 }
227                                 _pITOut->push_back(pS);
228                             }
229                             (*_pITOut)[j]->getAs<types::String>()->set(_iIterrator, wcsSingleData);
230                             iCountDataRead++;
231                         }
232
233                         if (nbrOfDigit)
234                         {
235                             FREE(wcsSingleData);
236                         }
237                     }
238                     else
239                     {
240                         if (_iIterrator == 0 && !bStar && _iNiter == 1)
241                         {
242                             _pITOut->push_back(types::Double::Empty());
243                         }
244                         else
245                         {
246                             FREE(wcsData);
247                             return -10;
248                         }
249                     }
250                     if (!bStar)
251                     {
252                         j++;
253                     }
254                     i++;
255                     bIgnoredChars = TRUE;
256                     bStar = FALSE;
257                 }
258                 break;
259                 case L'[' :
260                 {
261                     if (wcsData != NULL && wcsData[0] != L'\0')
262                     {
263                         wchar_t* wcsInside          = NULL;
264                         wchar_t* wcsCpyFormat       = NULL;
265                         unsigned int iPos           = 0;
266                         wchar_t* wcsSingleData      = NULL;
267                         wchar_t* wcsRes             = NULL;
268                         wchar_t* wscToFind          = NULL;
269                         BOOL bInv                   = FALSE;
270
271                         i++;
272                         wcsCpyFormat = (wchar_t*)MALLOC((wcslen(_wcsFormat) - i + 1) * sizeof(wchar_t));
273                         memcpy(wcsCpyFormat, &_wcsFormat[i], (wcslen(_wcsFormat) - i) * sizeof(wchar_t));
274                         wcsCpyFormat[wcslen(_wcsFormat) - i] = L'\0';
275
276                         wcsInside = os_wcstok(wcsCpyFormat, L"]", &wcsCpyFormat);
277                         i += (int)wcslen(wcsInside) + 1; // +1 => ]
278
279                         wscToFind = findChars(wcsInside, &bInv);
280                         if (wscToFind == NULL)
281                         {
282                             // MALLOC error
283                             FREE(wcsData);
284                             return -10;
285                         }
286
287                         if (bInv)
288                         {
289                             iPos = (int)wcscspn(wcsData, wscToFind);
290                         }
291                         else
292                         {
293                             iPos = (int)wcsspn(wcsData, wscToFind);
294                         }
295
296                         if (iPos == 0)
297                         {
298                             // The string begins with a character which is not in wscToFind
299                             if (_iIterrator == 0 && !bStar && _iNiter == 1)
300                             {
301                                 _pITOut->push_back(types::Double::Empty());
302                             }
303                             else
304                             {
305                                 FREE(wcsData);
306                                 return -10;
307                             }
308                         }
309                         else
310                         {
311                             wcsRes = (wchar_t*)MALLOC((iPos + 1) * sizeof(wchar_t));
312                             memcpy(wcsRes, wcsData, iPos * sizeof(wchar_t));
313                             wcsRes[iPos] = '\0';
314
315                             FREE(wcsInside);
316
317                             if (nbrOfDigit)
318                             {
319                                 wcsSingleData = (wchar_t*)MALLOC(sizeof(wchar_t) * (nbrOfDigit + 1));
320                                 memcpy(wcsSingleData, wcsRes, sizeof(wchar_t) * nbrOfDigit);
321                                 wcsSingleData[nbrOfDigit] = L'\0';
322                                 wcsData += nbrOfDigit;
323                             }
324                             else
325                             {
326                                 wcsSingleData = wcsRes;
327                                 wcsData += iPos;
328                             }
329                         }
330
331                         if (!bStar)
332                         {
333                             if (_iIterrator == 0)
334                             {
335                                 types::String* pS = new types::String(dims, dimsArray);
336                                 for (int k = 0 ; k < pS->getSize(); k++)
337                                 {
338                                     pS->set(k, L"");
339                                 }
340                                 _pITOut->push_back(pS);
341                             }
342                             if (wcsSingleData != NULL)
343                             {
344                                 (*_pITOut)[j]->getAs<types::String>()->set(_iIterrator, wcsSingleData);
345                                 iCountDataRead++;
346                             }
347                         }
348
349                         if (nbrOfDigit)
350                         {
351                             FREE(wcsSingleData);
352                         }
353                     }
354                     else
355                     {
356                         if (_iIterrator == 0 && !bStar && _iNiter == 1)
357                         {
358                             _pITOut->push_back(types::Double::Empty());
359                         }
360                         else
361                         {
362                             FREE(wcsData);
363                             return -10;
364                         }
365                     }
366                     if (!bStar)
367                     {
368                         j++;
369                     }
370                     bIgnoredChars   = TRUE;
371                     nbrOfDigit      = 0;
372                     bStar           = FALSE;
373                 }
374                 break;
375                 case L'x' :
376                 case L'X' :
377                     base += 6; // 6 + 2 + 8 = 16 // Compute the base of data to get.
378                 case L'u' :
379                     if (base == 0)
380                     {
381                         bUnsigned = TRUE;    // unsigned int
382                     }
383                 case L'i' :
384                 case L'd' :
385                     base += 2; // 2 + 8 = 10
386                 case L'o' :
387                     base += 8; // 8 = 8 :D
388                     {
389                         if (wcsData != NULL && wcsData[0] != L'\0')
390                         {
391                             long int iSingleData = 0;
392                             while (wcsData[0] == L' ')
393                             {
394                                 wcsData++;
395                             }
396                             if (nbrOfDigit)
397                             {
398                                 wchar_t* number = NULL;
399                                 if (wcslen(wcsData) < nbrOfDigit)
400                                 {
401                                     nbrOfDigit = (int)wcslen(wcsData);
402                                 }
403
404                                 number = (wchar_t*)MALLOC((nbrOfDigit + 1) * sizeof(wchar_t));
405                                 memcpy(number, wcsData, nbrOfDigit * sizeof(wchar_t));
406                                 number[nbrOfDigit] = L'\0';
407                                 iSingleData = wcstoul(number, &number, base);
408                                 if ((iSingleData == 0) && (number[0] == wcsData[0]))
409                                 {
410                                     if (_iIterrator == 0 && !bStar && _iNiter == 1)
411                                     {
412                                         wcsData = NULL;
413                                         _pITOut->push_back(types::Double::Empty());
414                                         bStar = TRUE;
415                                     }
416                                     else
417                                     {
418                                         FREE(wcsData);
419                                         return -10;
420                                     }
421                                 }
422                                 if (number == NULL)
423                                 {
424                                     wcsData += nbrOfDigit;
425                                 }
426                                 else
427                                 {
428                                     wcsData += (nbrOfDigit - wcslen(number));
429                                 }
430                                 nbrOfDigit = 0;
431                             }
432                             else
433                             {
434                                 wchar_t temp = wcsData[0];
435                                 iSingleData = wcstoul(wcsData, &wcsData, base);
436                                 if ((iSingleData == 0) && (temp == wcsData[0]))
437                                 {
438                                     if (_iIterrator == 0 && !bStar && _iNiter == 1)
439                                     {
440                                         wcsData = NULL;
441                                         _pITOut->push_back(types::Double::Empty());
442                                         bStar = TRUE;
443                                     }
444                                     else
445                                     {
446                                         FREE(wcsData);
447                                         return -10;
448                                     }
449                                 }
450                             }
451
452                             if (!bStar)
453                             {
454                                 if (_iIterrator == 0)
455                                 {
456                                     switch (wcsLLH)
457                                     {
458                                         case L'h' :
459                                         {
460                                             if (bUnsigned)
461                                             {
462                                                 types::UInt16* pUInt16 = new types::UInt16(dims, dimsArray);
463                                                 for (int k = 0; k < pUInt16->getSize(); k++)
464                                                 {
465                                                     pUInt16->set(k, 0);
466                                                 }
467                                                 _pITOut->push_back(pUInt16);
468                                             }
469                                             else
470                                             {
471                                                 types::Int16* pInt16 = new types::Int16(dims, dimsArray);
472                                                 for (int k = 0; k < pInt16->getSize(); k++)
473                                                 {
474                                                     pInt16->set(k, 0);
475                                                 }
476                                                 _pITOut->push_back(pInt16);
477                                             }
478                                         }
479                                         break;
480                                         case L'l' :
481                                         case L'L' :
482                                         {
483                                             if (bUnsigned)
484                                             {
485                                                 types::UInt64* pUInt64 = new types::UInt64(dims, dimsArray);
486                                                 for (int k = 0; k < pUInt64->getSize(); k++)
487                                                 {
488                                                     pUInt64->set(k, 0);
489                                                 }
490                                                 _pITOut->push_back(pUInt64);
491                                             }
492                                             else
493                                             {
494                                                 types::Int64* pInt64 = new types::Int64(dims, dimsArray);
495                                                 for (int k = 0; k < pInt64->getSize(); k++)
496                                                 {
497                                                     pInt64->set(k, 0);
498                                                 }
499                                                 _pITOut->push_back(pInt64);
500                                             }
501                                         }
502                                         break;
503                                         default :
504                                         {
505                                             if (bUnsigned)
506                                             {
507                                                 types::UInt32* pUInt32 = new types::UInt32(dims, dimsArray);
508                                                 for (int k = 0; k < pUInt32->getSize(); k++)
509                                                 {
510                                                     pUInt32->set(k, 0);
511                                                 }
512                                                 _pITOut->push_back(pUInt32);
513                                             }
514                                             else
515                                             {
516                                                 types::Int32* pInt32 = new types::Int32(dims, dimsArray);
517                                                 for (int k = 0; k < pInt32->getSize(); k++)
518                                                 {
519                                                     pInt32->set(k, 0);
520                                                 }
521                                                 _pITOut->push_back(pInt32);
522                                             }
523                                         }
524                                     }
525                                 }
526                                 switch (wcsLLH)
527                                 {
528                                     case L'h' :
529                                         if (bUnsigned)
530                                         {
531                                             (*_pITOut)[j]->getAs<types::UInt16>()->set(_iIterrator, static_cast<unsigned short int>(iSingleData));
532                                             iCountDataRead++;
533                                         }
534                                         else
535                                         {
536                                             (*_pITOut)[j]->getAs<types::Int16>()->set(_iIterrator, static_cast<short int>(iSingleData));
537                                             iCountDataRead++;
538                                         }
539                                         break;
540                                     case L'l' :
541                                     case L'L' :
542                                         if (bUnsigned)
543                                         {
544                                             (*_pITOut)[j]->getAs<types::UInt64>()->set(_iIterrator, iSingleData);
545                                             iCountDataRead++;
546                                         }
547                                         else
548                                         {
549                                             (*_pITOut)[j]->getAs<types::Int64>()->set(_iIterrator, static_cast<long int>(iSingleData));
550                                             iCountDataRead++;
551                                         }
552                                         break;
553                                     default :
554                                         if (bUnsigned)
555                                         {
556                                             (*_pITOut)[j]->getAs<types::UInt32>()->set(_iIterrator, static_cast<unsigned int>(iSingleData));
557                                             iCountDataRead++;
558                                         }
559                                         else
560                                         {
561                                             (*_pITOut)[j]->getAs<types::Int32>()->set(_iIterrator, static_cast<int>(iSingleData));
562                                             iCountDataRead++;
563                                         }
564                                 }
565                             }
566                         }
567                         else
568                         {
569                             if (_iIterrator == 0 && !bStar && _iNiter == 1)
570                             {
571                                 _pITOut->push_back(types::Double::Empty());
572                             }
573                             else
574                             {
575                                 FREE(wcsData);
576                                 return -10;
577                             }
578                         }
579                         if (!bStar)
580                         {
581                             j++;
582                         }
583                         wcsLLH          = L' ';
584                         bIgnoredChars   = TRUE;
585                         bUnsigned       = FALSE;
586                         bStar           = FALSE;
587                         base            = 0;
588                         i++;
589                     }
590                     break;
591                 case L'e' :
592                 case L'E' :
593                 case L'g' :
594                 case L'G' :
595                 case L'f' :
596                 {
597                     if (wcsData != NULL && wcsData[0] != L'\0')
598                     {
599                         double dSingleData  = 0;
600                         BOOL bSigne         = FALSE;
601                         while (wcsData[0] == L' ')
602                         {
603                             wcsData++;
604                         }
605                         if (nbrOfDigit)
606                         {
607                             int iSizeRead   = 0;
608                             wchar_t* number = NULL;
609                             wchar_t* next   = NULL;
610                             if (wcslen(wcsData) < nbrOfDigit)
611                             {
612                                 nbrOfDigit = (int)wcslen(wcsData);
613                             }
614                             number = (wchar_t*)MALLOC((nbrOfDigit + 1) * sizeof(wchar_t));
615                             memcpy(number, wcsData, nbrOfDigit * sizeof(wchar_t));
616                             number[nbrOfDigit] = L'\0';
617                             dSingleData = wcstod(number, &next);
618                             if (next)
619                             {
620                                 iSizeRead = nbrOfDigit - (int)wcslen(next);
621                             }
622                             else
623                             {
624                                 iSizeRead = nbrOfDigit;
625                             }
626                             if ((dSingleData == 0) && (number[0] == next[0]))
627                             {
628                                 if (_iIterrator == 0 && !bStar && _iNiter == 1)
629                                 {
630                                     wcsData = NULL;
631                                     _pITOut->push_back(types::Double::Empty());
632                                     bStar = TRUE;
633                                 }
634                                 else
635                                 {
636                                     FREE(wcsData);
637                                     return -10;
638                                 }
639                             }
640                             wcsData += iSizeRead;
641                             FREE(number);
642                             nbrOfDigit = 0;
643                         }
644                         else
645                         {
646                             int iLenData = (int)wcslen(wcsData);
647                             dSingleData = wcstod(wcsData, &wcsData);
648                             if ((dSingleData == 0) && (iLenData == wcslen(wcsData)))
649                             {
650                                 if (_iIterrator == 0 && !bStar && _iNiter == 1)
651                                 {
652                                     wcsData = NULL;
653                                     _pITOut->push_back(types::Double::Empty());
654                                     bStar = TRUE;
655                                 }
656                                 else
657                                 {
658                                     FREE(wcsData);
659                                     return -10;
660                                 }
661                             }
662                         }
663
664                         if (!bStar)
665                         {
666                             if (_iIterrator == 0)
667                             {
668                                 types::Double* pD = new types::Double(dims, dimsArray);
669                                 for (int k = 0 ; k < pD->getSize(); k++)
670                                 {
671                                     pD->set(k, 0);
672                                 }
673                                 _pITOut->push_back(pD);
674                             }
675                             (*_pITOut)[j]->getAs<types::Double>()->set(_iIterrator, dSingleData);
676                             iCountDataRead++;
677                         }
678                     }
679                     else
680                     {
681                         if (_iIterrator == 0 && !bStar && _iNiter == 1)
682                         {
683                             _pITOut->push_back(types::Double::Empty());
684                         }
685                         else
686                         {
687                             FREE(wcsData);
688                             return -10;
689                         }
690                     }
691                     if (!bStar)
692                     {
693                         j++;
694                     }
695                     i++;
696                     bIgnoredChars = TRUE;
697                     bStar = FALSE;
698                 }
699                 break;
700                 case L'n' :
701                 {
702                     double dSingleData = -1;
703                     if (_iIterrator == 0 && !bStar)
704                     {
705                         types::Double* pD = new types::Double(dims, dimsArray);
706                         for (int k = 0 ; k < pD->getSize(); k++)
707                         {
708                             pD->set(k, 0);
709                         }
710                         _pITOut->push_back(pD);
711                     }
712
713                     if (wcsData == NULL || wcsData[0] == L'\0')
714                     {
715                         dSingleData = (double)sizeOfData;
716                     }
717                     else
718                     {
719                         dSingleData = (double)sizeOfData - (double)wcslen(wcsData);
720                     }
721
722                     if (!bStar)
723                     {
724                         (*_pITOut)[j]->getAs<types::Double>()->set(_iIterrator, dSingleData);
725                         j++;
726                     }
727
728                     i++;
729                     bIgnoredChars = TRUE;
730                     bStar = FALSE;
731                 }
732                 break;
733                 default :
734                     printf("format read : %c\n", _wcsFormat[i]);
735                     FREE(wcsData);
736                     return -10;
737             }
738     }
739     FREE(wcsData);
740     return iCountDataRead;
741 }
742
743 wchar_t* findChars(wchar_t* chars, BOOL* _bInv) // ex: "123456789abc" = findChars("1-9abc")
744 {
745     unsigned int iIterChars = 0;
746     unsigned int iIterRes   = 0;
747     int iNbr                = 0;
748     int iLen                = 0;
749     wchar_t* wcsRes         = NULL;
750     int* piPos              = NULL;
751
752     piPos = (int*)MALLOC(wcslen(chars) * sizeof(int));
753
754     if (chars[0] == L'^')
755     {
756         *_bInv = TRUE;
757     }
758     else
759     {
760         *_bInv = FALSE;
761     }
762
763     for (iIterChars = 0; iIterChars < wcslen(chars); iIterChars++)
764     {
765         if (chars[iIterChars] == L'-' && iIterChars != 0 && iIterChars != wcslen(chars) - 1)
766         {
767             int iSize = chars[iIterChars + 1] - chars[iIterChars - 1] - 1;
768             if (iSize >= 0)
769             {
770                 iLen += iSize;
771                 iNbr++;
772                 piPos[iIterChars] = 1;
773             }
774             else
775             {
776                 piPos[iIterChars] = 0;
777                 iLen++;
778             }
779         }
780         else
781         {
782             piPos[iIterChars] = 0;
783             iLen++;
784         }
785     }
786
787     wcsRes = (wchar_t*)MALLOC((iLen + 1) * sizeof(wchar_t));
788
789     for (iIterChars = 0; iIterChars < wcslen(chars); iIterChars++)
790     {
791         if (piPos[iIterChars])
792         {
793             for (int i = 1; i < (chars[iIterChars + 1] - chars[iIterChars - 1]); i++, iIterRes++)
794             {
795                 wcsRes[iIterRes] = chars[iIterChars - 1] + i;
796             }
797         }
798         else
799         {
800             wcsRes[iIterRes] = chars[iIterChars];
801             iIterRes++;
802         }
803     }
804     wcsRes[iLen] = L'\0';
805
806     FREE(piPos);
807     return wcsRes;
808 }