PageRenderTime 75ms CodeModel.GetById 16ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/noah_palm/armlet/src/armlet-simple.c

https://github.com/kjk/noah-palm
C | 972 lines | 726 code | 109 blank | 137 comment | 101 complexity | d700af30e0813c10e6697faeb7b4b502 MD5 | raw file
  1/******************************************************************************
  2 *
  3 *  I M P O R T A N T
  4 *
  5 *  If you want to test program on simulator remember to
  6 *  copy armlet.dll to your simulator directory
  7 *
  8 *  You can create dll using noah_palm/armlet/palmsimarmlet.dsp
  9 *
 10 *****************************************************************************/
 11
 12/******************************************************************************
 13 *
 14 * File: armlet-simple.c
 15 *
 16 * Copyright (C) 2000-2003 Krzysztof Kowalczyk
 17 * Author: szymon knitter (szknitter@wp.pl)
 18 *
 19 * Description:
 20 *
 21 * This is changed armlet example.
 22 * We will use it to build armlet code for our aplications
 23 * NativeFunction must be first!!!
 24 * Read more in ../../src/armlet_structures.h
 25 *
 26 *****************************************************************************/
 27
 28/****************************************************
 29 *
 30 * Number - Function - return value when executed
 31 * 1  - Test function - retrun 101
 32 * 10 - Format2OnSortedBuffer
 33 * 11 - Format1OnSortedBuffer
 34 * 20 - WhileLoopFrom Get_defs_record
 35 *
 36 ****************************************************/
 37
 38#include "PceNativeCall.h"
 39#ifdef WIN32
 40	#include "SimNative.h"
 41//    #include <stdio.h>
 42#endif
 43
 44unsigned long NativeFunctionAtTheEnd(const void *emulStateP, void *userData68KP, Call68KFuncType *call68KFuncP);
 45
 46#ifndef WIN32
 47unsigned long __ARMlet_Startup__(const void *emulStateP, void *userData68KP, Call68KFuncType *call68KFuncP)
 48#else
 49unsigned long NativeFunction(const void *emulStateP, void *userData68KP, Call68KFuncType *call68KFuncP)
 50#endif
 51{
 52	return NativeFunctionAtTheEnd(emulStateP, userData68KP, call68KFuncP);
 53}
 54
 55#include "../../src/armlet_structures.h"
 56#include "utils68k.h"
 57
 58
 59/* FROM THIS POINT YOU CAN ADD YOUR OWN FUNCTIONS */
 60void Function1(void *funData)
 61{            
 62}
 63
 64/* Mem allocations */
 65#define sysTrapMemPtrNew	0xA013 // we need this in order to call into MemPtrNew
 66#define sysTrapMemPtrFree	0xA012 // we need this in order to call into MemPtrFree
 67
 68void new_free(void * ptr,const void *emulStateP,Call68KFuncType *call68KFuncP)
 69{
 70    void* ptrSwaped;  // array of Byte
 71    ptrSwaped = (void *)EndianSwap32(ptr);   
 72    ((call68KFuncP)(emulStateP, PceNativeTrapNo(sysTrapMemPtrFree), &ptrSwaped, 4 | kPceNativeWantA0));
 73}
 74
 75void * new_malloc(unsigned long setSize,const void *emulStateP,Call68KFuncType *call68KFuncP)
 76{
 77    unsigned long  size;     // UInt32, must be stack based
 78    unsigned char* bufferP;  // array of Byte
 79    // set the size to setSize bytes, byte swapped so it's in big-endian format
 80    // This will be the argument to MemPtrNew().
 81    size = EndianSwap32(setSize);   
 82    // call MemPtrNew, using the 4 bytes of size directly from the stack
 83    // The 4 specifies the size of the arguments, and we need to OR the size
 84    // with kPceNativeWantA0 because the return value is a pointer type.
 85    bufferP = (unsigned char *)((call68KFuncP)(emulStateP, PceNativeTrapNo(sysTrapMemPtrNew), &size, 4 | kPceNativeWantA0));
 86
 87    return (void *)bufferP;  // return a pointer to the buffer
 88}
 89
 90#ifndef WIN32
 91void memmove(char *dst, char *src, int len)
 92{
 93    int i;
 94    if(dst > src)
 95    {
 96        for(i=len-1;i>=0;i--)
 97            dst[i] = src[i];   
 98    }
 99    else
100    if(dst < src)
101    {
102        for(i=0;i<len;i++)
103            dst[i] = src[i];    
104    }
105}
106
107#define memcpy memmove
108
109unsigned long strlen(char *str)
110{
111    unsigned long i = 0;
112    while(str[i] != '\0')
113        i++;
114    return i;
115}
116#endif
117
118/*strprintstr function <=> sprintf(dst,"%s",src);*/
119void strprintstr(char *dst, char *src)
120{
121    int i = 0;
122    while(src[i] != 0)
123    {
124        dst[i] = (char) src[i];
125        i++;
126    }
127    dst[i] = 0;
128}
129
130/*straddstr function - add src to the end of dst*/
131/*void straddstr(char *dst, char *src)
132{
133    unsigned long i,j = 0;
134    i = strlen(dst);
135    while(src[j]!='\0')
136    {
137        dst[i] = src[j];
138        i++;
139        j++;
140    }
141    dst[i] = 0;
142}*/
143
144/*strprintshortint - printf int < 1000 as asci*/
145void strprintshortint(char *dst, int number)
146{
147    int i = 0;
148    int j = 13;
149    int number2;
150
151    if(number >= 100)
152    {
153        number2 = number;
154        j = 0;
155        while(number2 >= 100)
156        {
157            j++;
158            number2 -= 100;        
159        }
160        dst[i++] = j + '0';
161        number = number - j*100;
162    }
163
164    if(number >= 10)
165    {
166        number2 = number;
167        j = 0;
168        while(number2 >= 10)
169        {
170            j++;
171            number2 -= 10;        
172        }
173        dst[i++] = j + '0';
174        number = number - j*10;
175    }
176    
177        
178    dst[i++] = number + '0';
179    dst[i] = 0;
180}
181
182/* EBUF functions to handle buffer operations*/
183/* Extensible buffer for easier construction of string data
184   of unknown length */
185typedef struct
186{
187    int    allocated; // how many bytes allocated 
188    char * data;     // pointer to data
189    int    used;      // how many bytes used (out of all allocated)
190    //i dont use it!!! but it is needed
191    int    minSize;   // how much memory to pre-allocated upon initialization
192} ExtensibleBuffer;
193
194void ebufReplaceChar(ExtensibleBuffer *buf, char c, int pos)
195{
196    if (pos > buf->used)
197        return;
198    buf->data[pos] = c;
199}
200
201void ebufInsertChar(ExtensibleBuffer *buf, char c, int pos,const void *emulStateP,Call68KFuncType *call68KFuncP)
202{
203    int newAllocated;
204    char *newData;
205
206    // we can only insert up to the last pos in the buffer 
207    if (pos > buf->used)
208        return;
209
210    if ((buf->used + 1) >= buf->allocated)
211    {
212        newAllocated = buf->allocated + 256;
213        newData = (char *) new_malloc(newAllocated,emulStateP,call68KFuncP);   
214        if (!newData)
215            return;
216        if (buf->data)
217        {
218            memmove(newData, buf->data, buf->used);
219            new_free(buf->data,emulStateP,call68KFuncP);
220        }
221        buf->allocated = newAllocated;
222        buf->data = newData;
223    }
224    // make room if inserting 
225    if (pos < buf->used)
226    {
227        memmove(&(buf->data[pos + 1]), &(buf->data[pos]), buf->used - pos);
228    }
229    buf->data[pos] = c;
230    ++buf->used;
231}
232
233void ebufAddChar(ExtensibleBuffer *buf, char c,const void *emulStateP,Call68KFuncType *call68KFuncP)
234{
235    ebufInsertChar(buf, c, buf->used,emulStateP,call68KFuncP);
236}
237
238void ebufAddStrN(ExtensibleBuffer *buf, char *str, int strLen,const void *emulStateP,Call68KFuncType *call68KFuncP)
239{
240    int i;
241    for (i = 0; i < strLen; i++)
242        ebufInsertChar(buf, str[i], buf->used,emulStateP,call68KFuncP);
243}
244void ebufAddStr(ExtensibleBuffer * buf, char *str,const void *emulStateP,Call68KFuncType *call68KFuncP)
245{
246    ebufAddStrN(buf, str, strlen(str),emulStateP,call68KFuncP);
247}
248
249//delete one char from buffer
250//we will not free memory!!!
251void ebufDeleteChar(ExtensibleBuffer *buf, int pos)
252{
253    if (pos < buf->used - 1)
254        memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->used - pos - 1);
255    buf->used--;
256}
257
258//insert string into buf[pos]
259void ebufInsertStringOnPos(ExtensibleBuffer *buf, char *string, int pos,const void *emulStateP,Call68KFuncType *call68KFuncP)
260{
261    int i;
262    i = strlen(string) - 1;
263    
264    if(i+1 + buf->used < buf->allocated)
265    {
266        memmove(&buf->data[pos+i+1],&buf->data[pos],buf->used-pos);
267        buf->used += i+1;
268        for(;i >= 0; i--)
269            buf->data[pos+i] = string[i];
270    }
271    else
272    {    
273        for(; i >= 0; i--)
274            ebufInsertChar(buf, string[i], pos,emulStateP,call68KFuncP);
275    }    
276}
277
278//insert "Synonyms: " into buf[pos]
279void ebufInsertSynonymsOnPos(ExtensibleBuffer *buf, int pos,const void *emulStateP,Call68KFuncType *call68KFuncP)
280{
281    char syn[11] = {"Synonyms: "};
282    syn[0] = 'S';
283    syn[1] = 'y';
284    syn[2] = 'n';
285    syn[3] = 'o';
286    syn[4] = 'n';
287    syn[5] = 'y';
288    syn[6] = 'm';
289    syn[7] = 's';
290    syn[8] = ':';
291    syn[9] = ' ';
292    syn[10] = 0;
293    ebufInsertStringOnPos(buf, syn, pos,emulStateP,call68KFuncP); 
294
295//  This is not working - char* in code is invalid???
296//    ebufInsertStringOnPos(buf, "Synonyms: ", pos,emulStateP,call68KFuncP);
297
298}
299
300/*Called by Function10*/
301//formatting tag code (an unused in database asci code)
302//and DisplayPrefs structure
303#define FORMAT_TAG        152
304#define FORMAT_WORD       153
305#define FORMAT_DEFINITION 154
306#define FORMAT_EXAMPLE    155
307#define FORMAT_POS        156
308#define FORMAT_SYNONYM    157  
309#define FORMAT_LIST       158  
310#define FORMAT_BIG_LIST   159  
311#define FORMAT_PRONUNCIATION 160 
312
313// return true if a,b represents a tag
314// inline version to make it faster!
315static int IsTagInLine(char a, char b)
316{
317    if(a != (char)FORMAT_TAG)
318        return 0;
319    switch(b)
320    {
321        case (char)FORMAT_POS:
322        case (char)FORMAT_WORD:
323        case (char)FORMAT_DEFINITION: 
324        case (char)FORMAT_LIST:
325        case (char)FORMAT_BIG_LIST:
326        case (char)FORMAT_SYNONYM:
327        case (char)FORMAT_EXAMPLE: 
328        case (char)FORMAT_PRONUNCIATION: 
329            return 1;
330        default: 
331            return 0;
332    }
333}              
334
335/*return:   r<0 if pos1 < pos2
336            r=0 if pos1 = pos2
337            r>0 if pos1 > pos2   */
338int  CmpPos(char *pos1, char *pos2)
339{
340    int i,len1,len2;
341    
342    i = 1;
343    while( !IsTagInLine(pos1[i],pos1[i+1]) && pos1[i+1]!='\0')
344        i++;
345    len1 = i;
346    
347    i = 1;
348    while( !IsTagInLine(pos2[i],pos2[i+1]) && pos2[i+1]!='\0')
349        i++;
350    len2 = i;
351
352    i = 0;    
353    while(pos1[i] == pos2[i] && i < len1 && i < len2)
354        i++;
355    //end of pos1 or end of pos2?
356    if(i < len1 && i < len2)
357    {
358        if(pos1[i] < pos2[i])
359           return (-1);   
360        if(pos1[i] > pos2[i])
361           return (1);   
362    }
363    return(len1 - len2);
364}
365//delete all text until tag or EOB is reached
366static void ebufDeletePos(ExtensibleBuffer *buf, int pos)
367{
368    int i,j;
369    char *data = buf->data;
370    
371    i = pos;
372    j = buf->used;
373    while(!IsTagInLine(data[pos],data[pos+1]) && pos < j)
374        pos++;
375  
376    memmove(&(data[i]), &(data[pos]), j - pos);
377    buf->used -= (pos-i);
378}
379
380/*  Xchg AbcdEef -> EefAbcd   return updated offset2 */
381/*            |off1   |off2       |end2              */
382/*  before: abCdefghijKlmnopqrstuwXyz                */
383/*  after:  abKlmnopqrstuwCdefghijXyz                */
384/*                        |off2 - return             */
385//old version - in place
386int XchgInBuffer(char *txt, int offset1, int offset2, int end2)
387{
388    int  i;
389    char z;
390    char *txt1;
391    char *txt2;
392    int  div2;
393    //reverse all
394    txt1 = txt + offset1;
395    txt2 = txt + end2 - 1;
396    
397    div2 = (end2-offset1)>>1;
398    for(i = 0; i < div2 ; i++)
399    {
400        z       = txt1[0];
401        txt1[0] = txt2[0];
402        txt2[0] = z;
403        txt1++;
404        txt2--;
405    }
406    //mirror offset2
407    i = offset2 - offset1;
408    offset2 = end2 - i;
409    //reverse 1st
410    txt1 = txt + offset1;
411    txt2 = txt + offset2 - 1;
412    div2 = (offset2-offset1)>>1;
413    for(i = 0; i < div2 ; i++)
414    {
415        z       = txt1[0];
416        txt1[0] = txt2[0];
417        txt2[0] = z;
418        txt1++;
419        txt2--;
420    }
421    //reverse 2nd
422    txt1 = txt + offset2;
423    txt2 = txt + end2 - 1;
424    div2 = (end2-offset2)>>1;
425    for(i = 0; i < div2 ; i++)
426    {
427        z       = txt1[0];
428        txt1[0] = txt2[0];
429        txt2[0] = z;
430        txt1++;
431        txt2--;
432    }
433    return offset2;
434}
435//word is marked as synonym! and synonyms are marked as words!
436//we need to change it... and sort synonyms after definition and examples
437//also add "synonyms:" text (but not in thes.).
438static void XchgWordsWithSynonyms(ExtensibleBuffer *buf,const void *emulStateP,Call68KFuncType *call68KFuncP)
439{
440    int  i, j, k;
441    int  used;
442    char *data;
443    char *dataCurr;
444    char *dataEnd;
445
446    i = 0;
447    if(buf->data[1] == (char)FORMAT_SYNONYM /*|| buf->data[1] == (char)FORMAT_WORD*/)
448    {
449        buf->data[1] = (char)FORMAT_WORD;
450        i = 2;
451    }
452    else
453        return; //we dont have "word" at the begining. This is not Format1 or Format2...
454
455    used = buf->used;
456    data = buf->data;
457    while(i < used)
458    {
459        //set i on word tag
460        while(i < used && !(data[i]==(char)FORMAT_TAG && data[i+1]==(char)FORMAT_WORD))
461            i++;
462            
463        if(i < used)    
464        {
465            ebufReplaceChar(buf, FORMAT_SYNONYM, i + 1);
466#ifndef THESAURUS   //why? think :)
467            //ebufInsertStringOnPos(buf, "Synonyms: ", i + 2,emulStateP,call68KFuncP);
468            ebufInsertSynonymsOnPos(buf, i + 2,emulStateP,call68KFuncP);
469            used = buf->used;
470            data = buf->data;
471#endif
472        }
473        //set j on next tag
474        j = i+2;
475        dataCurr = data+j;
476        dataEnd = data+used;
477        while(dataCurr < dataEnd && !IsTagInLine(dataCurr[0],dataCurr[1]))
478            dataCurr++;
479        j = dataCurr-data;
480    
481        //some problems with unformated data
482        if(!(j+1 < used))
483            return; 
484        k = j;
485        //set k on pos //but not if its reached
486        if(data[j+1] != (char)FORMAT_POS)
487        {
488            k = j+2;
489            if(!(k+1 < used))
490                return; 
491        }    
492
493        dataCurr = data+k;
494        while(dataCurr < dataEnd && dataCurr[0]!='\0' && !(dataCurr[0]==(char)FORMAT_TAG && dataCurr[1]==(char)FORMAT_POS))
495            dataCurr++;
496            
497        k = dataCurr - data;    
498        //(i,j)(j,k) ---> (j,k)(i,j)
499        if(j < used && j!=k)
500            XchgInBuffer(data, i, j, k);
501        i = k+2;        
502    }
503}
504
505//format 1
506void Format1OnSortedBuffer(ExtensibleBuffer *buf,const void *emulStateP,Call68KFuncType *call68KFuncP)
507{
508    int  i, j;
509    int  first_pos;
510    int  number;    
511    char str_number[10];
512
513    XchgWordsWithSynonyms(buf,emulStateP,call68KFuncP);
514
515    i = 0;
516    while((buf->data[i] != (char)FORMAT_TAG || buf->data[i+1] != (char)FORMAT_POS) && i+1 < buf->used)
517        i++;
518    
519    first_pos = i + 2;
520    if(first_pos > buf->used)
521        return;   
522    i = first_pos;
523    number = 1;
524    while( i+1 < buf->used )
525    {
526        if(buf->data[i] == (char)FORMAT_TAG && buf->data[i+1] == (char)FORMAT_POS)
527        {
528            i += 2;
529            
530            if(CmpPos(&buf->data[i], &buf->data[first_pos])==0)
531            {
532                //add numbers to buff (2. 3. 4. etc)
533                ebufDeletePos(buf, i);
534                number++;                
535                strprintshortint(str_number,number);
536                j = strlen(str_number);
537                str_number[j++] = ':';
538                str_number[j++] = ' ';
539                str_number[j] = 0;
540
541                ebufReplaceChar(buf, FORMAT_LIST, i - 1);
542
543                ebufInsertStringOnPos(buf, str_number, i, emulStateP,call68KFuncP);
544                j = strlen(str_number);
545                
546                i += j-1;
547            }
548            else
549            {
550                //put 1. in first_pos (if its not a single pos type)
551                if(buf->data[first_pos] == (char)149)
552                {
553                    ebufDeleteChar(buf, first_pos);
554                    ebufDeleteChar(buf, first_pos);
555                    i-=2;
556                }
557
558                j = 0;
559                while( !IsTagInLine(buf->data[first_pos+j],buf->data[first_pos+j+1]) )
560                {
561                    if(buf->data[first_pos+j] == ')' || buf->data[first_pos+j]== '(')
562                    {
563                        ebufDeleteChar(buf, first_pos+j);
564                        j--;
565                        i--;
566                    }
567                    j++;                
568                }
569                j = first_pos + j;
570
571                str_number[0] = FORMAT_TAG;
572                str_number[1] = FORMAT_BIG_LIST;
573                if(number > 1)
574                {
575                    str_number[2] = '1';
576                    str_number[3] = ':';
577                    str_number[4] = ' ';
578                    str_number[5] = 0;
579                    i++;
580                }
581                else
582                {
583                    str_number[2] = ':';
584                    str_number[3] = ' ';
585                    str_number[4] = 0;
586                }
587                ebufInsertStringOnPos(buf, str_number, j, emulStateP,call68KFuncP);
588
589                i += 4;
590                number = 1;
591                first_pos = i;
592                i++;
593            }
594
595        }
596        else
597            i++;
598    } 
599    
600    //put 1. in first_pos
601    if(buf->data[first_pos] == (char)149)
602    {
603        ebufDeleteChar(buf, first_pos);
604        ebufDeleteChar(buf, first_pos);
605    }
606    j = 0;
607    while( !IsTagInLine(buf->data[first_pos+j],buf->data[first_pos+j+1]) )
608    {
609        if(buf->data[first_pos+j] == ')' || buf->data[first_pos+j]== '(')
610        {
611            ebufDeleteChar(buf, first_pos+j);
612            j--;
613        }
614        j++;                
615    }
616    j = first_pos + j;
617
618    i = 0;
619    str_number[i++] = FORMAT_TAG;
620    str_number[i++] = FORMAT_BIG_LIST;
621    if(number > 1)
622    {
623        str_number[i++] = '1';
624    }
625    str_number[i++] = ':';
626    str_number[i++] = ' ';
627    str_number[i] = 0;
628    ebufInsertStringOnPos(buf, str_number, j, emulStateP,call68KFuncP);
629}
630/* Print roman in dst */
631void strprintroman(char *dst, int roman)
632{
633    int i = 0;
634    dst[i++] = ' ';
635    switch(roman)
636    {
637        case 3: 
638                dst[i++] = 'I';
639        case 2: 
640                dst[i++] = 'I';
641        case 1: 
642                dst[i++] = 'I';
643            break;
644        case 4: 
645                dst[i++] = 'I';
646                dst[i++] = 'V';
647            break;
648        //we dont need it!
649        /*
650        case 5: 
651                dst[i++] = 'V';
652            break;
653        case 6: 
654                dst[i++] = 'V';
655                dst[i++] = 'I';
656            break;
657        case 7: 
658                dst[i++] = 'V';
659                dst[i++] = 'I';
660                dst[i++] = 'I';
661            break;
662        case 8: 
663                dst[i++] = 'V';
664                dst[i++] = 'I';
665                dst[i++] = 'I';
666                dst[i++] = 'I';
667            break;
668        case 9: 
669                dst[i++] = 'I';
670                dst[i++] = 'X';
671            break;*/
672        default: break;
673    }
674    dst[i++] = ' ';
675    dst[i++] = 0;
676}
677
678/* Format 2 on sorted buffer*/
679void Format2onSortedBuffer(ExtensibleBuffer *buf,const void *emulStateP,Call68KFuncType *call68KFuncP)
680{       
681    int  i, j, p;
682    int  first_pos;
683    int  number;    
684    int  bignumber;    
685    char str_number[16];
686    char *data;
687    char *dataTest;
688
689    XchgWordsWithSynonyms(buf,emulStateP,call68KFuncP);
690    
691    i = 0;
692    while((buf->data[i] != (char)FORMAT_TAG || buf->data[i+1] != (char)FORMAT_POS) && i+1 < buf->used)
693        i++;
694
695    first_pos = i + 2;
696    if(first_pos > buf->used)
697        return;   
698    i = first_pos;
699    number = 1;
700    bignumber = 1;
701    
702    data = buf->data;
703    data += i;
704    dataTest = (buf->data) + (buf->used) - 1;
705    
706    while(data < dataTest)
707    {
708        if(data[0] == (char)FORMAT_TAG)
709        {
710            if(data[1] == (char)FORMAT_POS)
711            {
712                i = buf->used - (dataTest+1-data);
713                
714                i += 2;
715            
716                if(CmpPos(&buf->data[i], &buf->data[first_pos])==0)
717                {
718                    //add numbers to buff (2. 3. 4. etc)
719                    ebufDeletePos(buf, i);
720                    number++;
721                    strprintshortint(str_number,number);
722                    p = strlen(str_number);
723                    str_number[p++] = ')';
724                    str_number[p++] = ' ';
725                    str_number[p++] = 0;
726                    
727                    ebufInsertStringOnPos(buf, str_number, i,emulStateP,call68KFuncP);
728                    j = strlen(str_number);
729
730                    ebufReplaceChar(buf, FORMAT_LIST, i - 1);
731                    i += j-1;
732                }
733                else
734                {
735                    //put 1) in first_pos (if its not a single pos type)
736                    if(buf->data[first_pos] == (char)149)
737                    {
738                        ebufDeleteChar(buf, first_pos);
739                        ebufDeleteChar(buf, first_pos);
740                        i-=2;
741                    }
742    
743                    j = first_pos;
744
745                    while( !IsTagInLine(buf->data[j],buf->data[j+1]) )
746                    {
747                        if(buf->data[j] == ')' || buf->data[j]== '(')
748                        {
749                            ebufDeleteChar(buf, j);
750                            j--;
751                            i--;
752                        }
753                        j++;
754                    }
755    
756                    ebufInsertChar(buf, '\n', j,emulStateP,call68KFuncP);
757                    j++;
758                    i++;
759                                    
760                    if(number > 1)
761                    {
762                        str_number[0] = FORMAT_TAG;
763                        str_number[1] = FORMAT_LIST;
764                        str_number[2] = '1';
765                        str_number[3] = ')';
766                        str_number[4] = ' ';
767                        str_number[5] = 0;
768                        ebufInsertStringOnPos(buf, str_number, j, emulStateP,call68KFuncP);
769                        i += 5;
770                    }
771                    
772                    str_number[0] = (char) FORMAT_TAG;
773                    str_number[1] = (char) FORMAT_BIG_LIST;
774                    str_number[2] = 0;
775                    strprintroman(&str_number[strlen(str_number)],bignumber);
776                    
777                    ebufInsertStringOnPos(buf, str_number, first_pos - 2, emulStateP,call68KFuncP);
778                    j = strlen(str_number);
779                    
780                    i += j-1;
781                    
782                    i++;
783                    number = 1;
784                    first_pos = i;
785                    i++;
786                    bignumber++;
787                }
788        
789                data = buf->data + i;
790                dataTest = (buf->data) + (buf->used) - 1;
791            }
792            else
793                data++;            
794        }
795        else
796            data++;
797    } 
798
799    //put 1) in first_pos (if its not a single pos type)
800    if(buf->data[first_pos] == (char)149)
801    {
802        ebufDeleteChar(buf, first_pos);
803        ebufDeleteChar(buf, first_pos);
804    }
805
806    j = first_pos;
807    while( !IsTagInLine(buf->data[j],buf->data[j+1]) )
808    {
809        if(buf->data[j] == ')' || buf->data[j]== '(')
810        {
811            ebufDeleteChar(buf, j);
812            j--;
813        }
814        j++;                
815    }
816
817    //goto end of pos
818    j = first_pos + 1;
819    while( !IsTagInLine(buf->data[j], buf->data[j+1]) )
820        j++;
821
822    ebufInsertChar(buf, '\n', j,emulStateP,call68KFuncP);
823    j++;
824                                
825    if(number > 1)
826    {
827        str_number[0] = FORMAT_TAG;
828        str_number[1] = FORMAT_LIST;
829        str_number[2] = '1';
830        str_number[3] = ')';
831        str_number[4] = ' ';
832        str_number[5] = 0;
833        ebufInsertStringOnPos(buf, str_number, j, emulStateP,call68KFuncP);
834    }
835    
836    if(bignumber > 1)
837    {    
838        str_number[0] = (char) FORMAT_TAG;
839        str_number[1] = (char) FORMAT_BIG_LIST;
840        str_number[2] = 0;
841        strprintroman(&str_number[strlen(str_number)],bignumber);
842
843        ebufInsertStringOnPos(buf, str_number, first_pos - 2, emulStateP,call68KFuncP);
844    }
845}
846/*Fasade on Format2OnSortedBuffer*/
847void Function10(void *funData,const void *emulStateP,Call68KFuncType *call68KFuncP)
848{
849    armFunction10Input *input;
850    ExtensibleBuffer buf;
851    unsigned long temp;
852    input = (armFunction10Input *) funData;
853    
854    buf.allocated = Read68KUnaligned32(&input->allocated);
855    buf.data = (char *) Read68KUnaligned32(&input->data);
856    buf.used = Read68KUnaligned32(&input->used);
857    buf.minSize = 0;
858
859    
860    Format2onSortedBuffer(&buf,emulStateP,call68KFuncP);
861        
862
863    Write68KUnaligned32(&input->data,buf.data);
864    temp = buf.allocated;
865    Write68KUnaligned32(&input->allocated,temp);
866    temp = buf.used;
867    Write68KUnaligned32(&input->used,temp);
868}
869/*Fasade on Format1OnSortedBuffer*/
870void Function11(void *funData,const void *emulStateP,Call68KFuncType *call68KFuncP)
871{
872    armFunction10Input *input;
873    ExtensibleBuffer buf;
874    unsigned long temp;
875    input = (armFunction10Input *) funData;
876    
877    buf.allocated = Read68KUnaligned32(&input->allocated);
878    buf.data = (char *) Read68KUnaligned32(&input->data);
879    buf.used = Read68KUnaligned32(&input->used);
880    buf.minSize = 0;
881
882    
883    Format1OnSortedBuffer(&buf,emulStateP,call68KFuncP);
884        
885
886    Write68KUnaligned32(&input->data,buf.data);
887    temp = buf.allocated;
888    Write68KUnaligned32(&input->allocated,temp);
889    temp = buf.used;
890    Write68KUnaligned32(&input->used,temp);
891}
892/*while from get_defs_record*/
893unsigned long CalculateOffsetGDR(unsigned long *current_entry,unsigned long *offset, unsigned long *curr_len,
894                unsigned char **def_lens_fast, unsigned char *def_lens_fast_end,unsigned long synsetNoFast)
895{
896    do
897    {
898        (*curr_len) = (unsigned long) ((unsigned char)(def_lens_fast[0])[0]);
899        (def_lens_fast[0])++;
900        if ((unsigned long)255 == (*curr_len))
901        {
902            (*curr_len) = (unsigned long)((unsigned char)((def_lens_fast[0])[0])) * 256;
903            (def_lens_fast[0])++;
904            (*curr_len) += (unsigned long)(unsigned char)((def_lens_fast[0])[0]);
905            (def_lens_fast[0])++;
906        }
907
908        if ((*current_entry) == synsetNoFast)
909            return ((unsigned long) 1);
910
911        (*offset) += (*curr_len);
912        
913        if(def_lens_fast_end == (def_lens_fast[0]))
914            return ((unsigned long) 2);
915        (*current_entry)++;
916    }
917    while (1==1);//true
918}
919
920/*runner for CalculateOffsetGDR*/
921void FunctionGetDefsRecord(void *funData)
922{
923    armFunctionGetDefsRecordInput *input = (armFunctionGetDefsRecordInput *) funData;
924    unsigned long current_entry = Read68KUnaligned32(&input->current_entry);
925    unsigned long offset = Read68KUnaligned32(&input->offset);
926    unsigned long curr_len = Read68KUnaligned32(&input->curr_len);
927    unsigned char *def_lens_fast;
928    unsigned char *def_lens_fast_end;
929    unsigned long synsetNoFast = Read68KUnaligned32(&input->synsetNoFast);
930    unsigned long returnValue = 0;
931
932    def_lens_fast = (unsigned char *) Read68KUnaligned32(&input->def_lens_fast);
933    def_lens_fast_end = (unsigned char *) Read68KUnaligned32(&input->def_lens_fast_end);    
934
935    returnValue = CalculateOffsetGDR(&current_entry, &offset, &curr_len, &def_lens_fast, def_lens_fast_end, synsetNoFast);
936
937    Write68KUnaligned32(&input->current_entry, current_entry);
938    Write68KUnaligned32(&input->offset, offset);
939    Write68KUnaligned32(&input->curr_len, curr_len);
940    Write68KUnaligned32(&input->def_lens_fast, def_lens_fast);
941    Write68KUnaligned32(&input->returnValue, returnValue);
942}
943/*THIS SHOULD BE LAST FUNCTION IN FILE*/
944unsigned long NativeFunctionAtTheEnd(const void *emulStateP, void *userData68KP, Call68KFuncType *call68KFuncP)
945{
946    unsigned long funID;
947    armMainInput *inptr;
948    inptr = (armMainInput*) userData68KP;
949
950    funID = Read68KUnaligned32(&inptr->functionID);
951    switch(funID)
952    {
953        case ARM_FUN_TESTIFPRESENT: //test function
954                Function1((void*)Read68KUnaligned32(&inptr->functionData));
955                Write68KUnaligned32(&inptr->functionID, funID+ARM_FUN_RETURN_OFFSET);
956            break;
957        case ARM_FUN_FORMAT2ONBUFF: //format 2 on sorted buffer
958                Function10((void*)Read68KUnaligned32(&inptr->functionData),emulStateP,call68KFuncP);
959                Write68KUnaligned32(&inptr->functionID, funID+ARM_FUN_RETURN_OFFSET);
960            break;
961        case ARM_FUN_FORMAT1ONBUFF: //format 1 on sorted buffer
962                Function11((void*)Read68KUnaligned32(&inptr->functionData),emulStateP,call68KFuncP);
963                Write68KUnaligned32(&inptr->functionID, funID+ARM_FUN_RETURN_OFFSET);
964            break;
965        case ARM_FUN_GETDEFSRECORD: //get defs record (while loop)
966                FunctionGetDefsRecord((void*)Read68KUnaligned32(&inptr->functionData));
967                Write68KUnaligned32(&inptr->functionID, funID+ARM_FUN_RETURN_OFFSET);
968            break;    
969        default: break;
970    }                  
971	return (unsigned long)userData68KP;
972}