home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet 2.7b5 / source / parse / translate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-08  |  7.6 KB  |  296 lines  |  [TEXT/CWIE]

  1. /*
  2. *    translate.c
  3. *    written by Roland MÃ¥nsson, Lund University Computing Center, Sweden
  4. *    roland_m@ldc.lu.se
  5. *    July 1992
  6. *
  7. *    Modified by Pascal Maes
  8. *    UCL/ELEC
  9. *    Place du Levant, 3
  10. *    B-1348 Louvain-la-Neuve
  11. *****************************************************************
  12. *    Part of:                                                    *
  13. *    NCSA Telnet for the Macintosh                                *
  14. *                                                                *
  15. *    National Center for Supercomputing Applications                *
  16. *    Software Development Group                                    *
  17. *    152 Computing Applications Building                            *
  18. *    605 E. Springfield Ave.                                        *
  19. *    Champaign, IL  61820                                        *
  20. *                                                                *
  21. *    Copyright (c) 1993,                                            *
  22. *    Board of Trustees of the University of Illinois                *
  23. *****************************************************************
  24. *    Modified 7/93 by Jim Browne for NCSA.
  25. */
  26.  
  27. #ifdef MPW
  28. #pragma segment 4
  29. #endif
  30.  
  31.  
  32.  
  33. #include "wind.h"
  34. #include "vsdata.h"
  35. #include "translate.proto.h"
  36. #include "vsinterf.proto.h"
  37. #include "LinkedList.proto.h"
  38.  
  39. //#define    DEBUG_TRANSLATION
  40. /*************** external variables ***************/
  41.  
  42.  
  43. extern     WindRec *screens;        /* The screen array from maclook.c */
  44. extern    short scrn;                /* The current screen from maclook.c */
  45.  
  46. /*************** global variables ***************/
  47.  
  48. BytePtr    DefaultTable,
  49.         FTPinTable,
  50.         FTPoutTable;
  51.         
  52. Handle    transTablesHdl;
  53. short    nNational;
  54.  
  55. #if 0
  56. Boolean get_trsl (short id, Byte **table)
  57. {
  58.     Handle h;
  59.     long size;
  60.  
  61.     h = GetResource (TRSL,id);
  62.  
  63.     if ((h==NULL) || (ResError()!=noErr)) 
  64.         {
  65.         DoError(106 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  66.         return (FALSE);
  67.         }
  68.         
  69.     size = GetHandleSize(h);
  70.     if (size != 256) 
  71.         {
  72.         DoError(107 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  73.         return (FALSE);
  74.         }
  75.  
  76.     HLockHi(h);
  77.     *table = (Byte *) *h;
  78.     return (TRUE);
  79. }
  80. #endif
  81.  
  82. short    transBuffer(short oldtable, short newtable)        /* translate entire buffer */
  83. {
  84.     VSscrn    *vsscreen;
  85.     VSline    *vslin,*p;
  86.     short        lineNo,maxLineNo;
  87.     short        width;                /* allocated witdth of window (80/132) */
  88.     char        tmp[80];                /* only for debugging */
  89.     
  90.     vsscreen = VSwhereis (screens[scrn].vs);
  91.     vslin = vsscreen->buftop;
  92.  
  93. #ifdef DEBUG_TRANSLATION
  94.     putln ("in transBuffer, well and alive");
  95.     sprintf (tmp,"VSgetlines(screens[scrn].vs):%d", VSgetlines(screens[scrn].vs)); putln (tmp);
  96.     sprintf (tmp,"VSmaxwidth(screens[scrn].vs):%d", VSmaxwidth(screens[scrn].vs)); putln (tmp);
  97.     sprintf (tmp,"vsscreen->lines:%d\n",vsscreen->lines); putln (tmp);
  98.     sprintf (tmp,"vsscreen->maxlines:%d\n",vsscreen->maxlines); putln (tmp);
  99.     sprintf (tmp,"vsscreen->numlines:%d\n",vsscreen->numlines); putln (tmp);
  100.     sprintf (tmp,"vsscreen->allwidth:%d\n",vsscreen->allwidth); putln (tmp);
  101.     sprintf (tmp,"vsscreen->maxwidth:%d\n",vsscreen->maxwidth); putln (tmp);
  102.     sprintf (tmp,"vsscreen:%08x\n",vsscreen);  putln (tmp);
  103.     sprintf (tmp,"vslin:%08x\n",vslin);  putln (tmp);
  104.     sprintf (tmp,"next:%08x\n",vslin->next);  putln (tmp);
  105.     sprintf (tmp,"prev:%08x\n",vslin->prev);  putln (tmp);
  106.     sprintf (tmp,"text:%08x\n\n",vslin->text);  putln (tmp);
  107. #endif
  108.  
  109.     width = VSmaxwidth(screens[scrn].vs)+1;    /* VSmaxwidth returns 79 or 131 */
  110.     p = vslin;
  111.     maxLineNo = vsscreen->numlines+VSgetlines(screens[scrn].vs); /* VSgetlines returns 24 or whatever */
  112.     for (lineNo=1; lineNo<=maxLineNo; lineNo++) {
  113.         /*sprintf(tmp,"lineNo:%d, p:%08x, starts with:%c%c%c%c",lineNo,p,*(p->text),*(p->text+1),*(p->text+2),*(p->text+3)); putln(tmp);*/
  114.         if (p==NULL) { putln ("p is NULL"); return (-1); }
  115.         if (p->text==NULL) { putln ("p->text is NULL"); return (-1); }
  116.         
  117.         // First convert the line back to Mac US format, and then to the new format.
  118.         trbuf_nat_mac((unsigned char *)p->text,width, oldtable);
  119.         trbuf_mac_nat((unsigned char *)p->text,width, newtable);
  120.         
  121.         p = p->next;
  122.     }
  123.     sprintf (tmp, "transBuffer:did convert %d lines", lineNo-1); putln (tmp);
  124.     return (0);
  125. }
  126.  
  127. BytePtr        GetTranslationResource(short id)
  128. {
  129.     Handle    h;
  130.     
  131.     h = GetResource(MY_TRSL, id);
  132.     
  133.     if ((h == NULL) || (ResError() != noErr)) {
  134.         // Do nasty mean error here. BUGG
  135.         }
  136.     
  137.     DetachResource(h);
  138.     HLockHi(h);
  139.     return((BytePtr) *h);
  140. }
  141.  
  142. //    table #'s 1...n correspond to tables in our master array, table #0 is the default table
  143. BytePtr        ReturnTablePtr(short table, Boolean out)
  144. {
  145.     if (table > nNational || table < 1) return(DefaultTable + ((out == TRUE) * 256));
  146.     return((BytePtr)(*transTablesHdl + ((table - 1) * 512) + ((out == TRUE) * 256)));
  147. }
  148.  
  149. //    The Default table (i.e. no translation) and the two FTP tables are stored in the 
  150. //     Application's resource fork as resources of type TRSL.  The
  151. //     tables added and removed by the user are stored in the prefs file as resources of
  152. //     type taBL.  This routine loads the default table and the two FTP tables into memory.
  153. //     Failures can only be caused by an incorrect application resource fork.
  154. void    Setup_Default_Tables(void)
  155. {
  156.     DefaultTable = GetTranslationResource(TRSL_DEFAULT_TABLE);
  157.     FTPinTable = GetTranslationResource(TRSL_FTP_TABLE);
  158.     FTPoutTable = FTPinTable + 256;
  159. }
  160.  
  161. /* 
  162. *    Be very careful with calling putln from this procedure, since
  163. *    putln uses the translation tables. If the tables are not setup
  164. *    garbage output will appear. This is not harmful, but very
  165. *    annoying.
  166. */
  167.  
  168. void trInit (MenuHandle    whichMenu)
  169. {
  170.     short    i, numTables;        
  171.     Handle    h;
  172.     LinkedListNode *theHead;
  173.         
  174.      nNational = 0;
  175.     Setup_Default_Tables();
  176.     transTablesHdl = myNewHandle(0);
  177.     UseResFile(TelInfo->ApplicationFile);
  178.     numTables = CountResources(USER_TRSL);
  179.     if (numTables)
  180.     {
  181.         Str255 NoneString;
  182.         theHead = createSortedList(USER_TRSL,numTables,NULL); //now we have a sorted linked list of the names
  183.         GetIndString(NoneString,MISC_STRINGS,NONE_STRING);
  184.         AppendMenu(whichMenu,NoneString);
  185.         addListToMenu(whichMenu,theHead);
  186.         deleteList(&theHead);
  187.         
  188.         for (i = 2; i <= numTables + 1; i++) //start adding things from the second menu item (first is none)
  189.         {
  190.             Str255 menuItemName;
  191.             GetItem(whichMenu, i,menuItemName);
  192.             h = Get1NamedResource(USER_TRSL,menuItemName);
  193.             if (ResError() == noErr && (GetHandleSize(h) == 512)) {
  194.                 nNational++;
  195.                 
  196.                 // Append the table's data to the master array of table data
  197.                 HUnlock(transTablesHdl);
  198.                 mySetHandleSize(transTablesHdl, (nNational * 512));//we're at init time; we have the mem
  199.                 HLockHi(transTablesHdl);
  200.                 HLock(h);
  201.                 BlockMove(*h, (*transTablesHdl) + ((nNational - 1) * 512), 512);
  202.             }
  203.             // Release the resource
  204.             ReleaseResource(h);
  205.         }
  206.  
  207.     }
  208.     UseResFile(TelInfo->SettingsFile);
  209. }
  210.  
  211. /*    Converts a char from 8-bit National to 8-bit Macintosh */
  212. void    trbuf_nat_mac(unsigned char *buf, short len, short table)
  213. {
  214.     short            i;
  215.     unsigned char    *p;
  216.     BytePtr            table_data;
  217.  
  218.     table_data = ReturnTablePtr(table, FALSE);
  219.     
  220.     for (i=0,p=buf; i<len; i++,p++)
  221.     {
  222.         *p = table_data[(short)*p];
  223.     }
  224.             
  225. }
  226.  
  227. unsigned char    ftp_iso_mac(unsigned char *ascii)
  228. {
  229.     short    b;
  230.     
  231.     b = (short) *ascii;
  232.     *ascii = FTPinTable[b];
  233.     return (*ascii);
  234. }
  235.  
  236.  
  237. void    trbuf_ftp_mac(unsigned char *buf, short len)
  238. {
  239.     short            i;
  240.     unsigned char    ascii;
  241.     unsigned char    *p;
  242.  
  243.     for (i=0,p=buf; i<len; i++,p++)
  244.     {
  245.         ascii = *p;
  246.         *p = ftp_iso_mac(&ascii);
  247.     }
  248.             
  249. }
  250.  
  251.  
  252. /*    Converts a char from 8-bit Macintosh to 8-bit National */
  253. unsigned char    mac_nat(unsigned char *ascii, short table)
  254. {
  255.     short    b;
  256.     BytePtr    table_data = ReturnTablePtr(table, TRUE);
  257.     
  258.     b = (short) *ascii;
  259.     *ascii = table_data[b];
  260.     return (*ascii);
  261. }
  262.  
  263. unsigned char    ftp_mac_iso(unsigned char *ascii)
  264. {
  265.     short    b;
  266.     
  267.     b = (short) *ascii;
  268.     *ascii = FTPoutTable[b];
  269.     return (*ascii);
  270. }
  271.  
  272. void    trbuf_mac_nat(unsigned char *buf, short len, short table)
  273. {
  274.     short            i;
  275.     unsigned char    ascii;
  276.     unsigned char    *p;
  277.     
  278.     for (i=0,p=buf; i<len; i++,p++)
  279.     {
  280.         ascii = *p;
  281.         *p = mac_nat(&ascii, table);
  282.     }
  283. }
  284.  
  285. void    trbuf_mac_ftp(unsigned char *buf, short len)
  286. {
  287.     short            i;
  288.     unsigned char    ascii;
  289.     unsigned char    *p;
  290.     
  291.     for (i=0,p=buf; i<len; i++,p++)
  292.     {
  293.         ascii = *p;
  294.         *p = ftp_mac_iso(&ascii);
  295.     }
  296. }