home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / wincvt / wincvt.c next >
C/C++ Source or Header  |  1992-06-22  |  13KB  |  518 lines

  1. /*    wincvt.c
  2. **
  3. ** by Wayne E. Wright, W5XD
  4. ** Round Rock, Tx
  5. ** June, 1992
  6. **
  7. ** This utility converts Atari ST resource files (.RSC) into 
  8. ** Windows resource files (.RC).
  9. **
  10. ** The conversion is not perfect, but its purpose is to aid in porting
  11. ** a GEM program to Windows. At least the geometry comes out the same
  12. ** and all the text comes out in the right place.
  13. **
  14. ** It does not convert bit maps at all, nor does it convert BOXCHARs.
  15. **
  16. **
  17. ** To convert a file (actually a .RSC/.DEF file pair), run the program with
  18. ** the file name as its argument--with no dot and no extension.
  19. **
  20. ** The program is designed to read .DEF files generated by Laser C. I
  21. ** don't know if it works with other resource editors. It does still
  22. ** make a conversion without a .DEF file, but with no symbols the .RC
  23. ** file is of limited usefulness.
  24. **
  25. ** The result of the conversion is a .RC file (which is text) and a
  26. ** .H file. Note that this header file has the same symbols in it as the
  27. ** the Atari resource editor's equivalent H file--but the values of the
  28. ** symbols are not at all the same.
  29. **
  30. ** The RC file then can be edited by Windows development tools to get the
  31. ** exact behavior you want under Windows.
  32. **
  33. ** Note that this program must itself run on the Atari because the byte
  34. ** it uses binary reads to get the int's out of the RSC file. This could
  35. ** be written in a portable way so that you could copy the RSC/DEF files
  36. ** to the PC world and run this program there, but I still have an
  37. ** Atari and I'm lazy.
  38. **
  39. */
  40. #include <stdio.h>
  41. #include <osbind.h>
  42. #include <obdefs.h>
  43. #include <gemdefs.h>
  44.  
  45. struct DefFileEntry
  46.     {
  47.     int    TreeNumber;
  48.     int    ObjIndex;
  49.     unsigned    Flag;
  50.     char    Label[8];
  51.     };
  52.  
  53. struct DefLabel
  54.     {
  55.     struct DefFileEntry        Entry;
  56.     int                        MenuFlag;
  57.     };
  58.  
  59. static    int    NumLabels;
  60. static    struct DefLabel *LabelArray;
  61. static    FILE *RcFile;
  62.  
  63. #define RBUTTON_ID_START 20        /* beginning ID for radio buttons */
  64. #define MAX_RBTN_BOX    20        /* maximum # of radio btns in a box */
  65. #define ID_START 256
  66.  
  67. static int RadioBtnIdx = RBUTTON_ID_START;
  68.  
  69.  
  70. main(argc,argv)
  71. char *argv[];
  72. {
  73.     RSHDR RscHeader;
  74.     OBJECT *ObjectPtr;
  75.     TEDINFO *TextPtr;
  76.     OBJECT    **TreeIndex;
  77.     FILE *RscFile;
  78.     FILE *DefFile;
  79.     char *ReadBuffer;
  80.     register int i,j;
  81.     int TreeCount;
  82.     int    *LabelReference;
  83.     char    InRscFile[80];
  84.     char    OutHfile[80];
  85.     char    OutRCfile[80];
  86.     char    InDefFile[80];
  87.  
  88.     puts("Convert to MS Windows 3.0 Resource\n");
  89.  
  90.     if (argc < 2)
  91.     {
  92.         puts("Usage: rscfile\n");
  93.         puts("    Will read rscfile.rsc and rscfile.def and create\n");
  94.         puts("    rscfile.h and rscfile.rc\n");
  95.         exit(0);
  96.     }
  97.     strcpy(InRscFile, argv[1]);    strcat(InRscFile, ".RSC");
  98.     strcpy(OutHfile, argv[1]);    strcat(OutHfile, ".H");
  99.     strcpy(OutRCfile, argv[1]); strcat(OutRCfile, ".RC");
  100.     strcpy(InDefFile, argv[1]); strcat(InDefFile, ".DEF");
  101.  
  102.     RscFile = fopen(InRscFile, "br");
  103.     if (RscFile == NULL)
  104.     {
  105.         printf("can't open %s\n", InRscFile);
  106.         exit(0);
  107.     }
  108.  
  109.     RcFile = fopen(OutRCfile, "w");
  110.     if (!RcFile)
  111.     {
  112.         printf("Can't open output file :%s\n", OutRCfile);
  113.         exit(0);
  114.     }
  115.     fprintf(RcFile, "#include \"%s\"\n", OutHfile);
  116.  
  117.     DefFile = fopen(InDefFile, "br");
  118.     if (!DefFile)
  119.     {
  120.         printf("Can't open %s\nContinuing...\n", InDefFile);
  121.     }
  122.  
  123.     
  124.     NumLabels = 0;
  125.     if (DefFile)
  126.     {
  127.         fseek(DefFile, 9l, 0);
  128.         fread(&NumLabels, 1, 2, DefFile);
  129.         LabelArray = (struct DefLabel *) malloc(
  130.                 NumLabels * sizeof(*LabelArray));
  131.         for (i = 0; i < NumLabels; i += 1)
  132.         {
  133.             fread(LabelArray + i, sizeof(struct DefFileEntry),
  134.                      1, DefFile);
  135.             LabelArray[i].MenuFlag = -1;
  136.         }
  137.         fclose(DefFile);
  138.     }
  139.  
  140.  
  141.     if (fread(&RscHeader, sizeof(RscHeader), 1, RscFile) <= 0)
  142.     {
  143.         printf("Can't read header\n");
  144.         exit(0);
  145.     }
  146.  
  147.     ReadBuffer = malloc(RscHeader.rsh_rssize);
  148.     rewind(RscFile);
  149.     if (fread(ReadBuffer, 1, RscHeader.rsh_rssize, RscFile) !=
  150.             RscHeader.rsh_rssize)
  151.     {
  152.         printf("File read failure\n");
  153.         exit(0);
  154.     }
  155.     fclose(RscFile);
  156.  
  157.     /* Get index of tedinfos */
  158.  
  159.     TextPtr = (TEDINFO *)   (((long)ReadBuffer) + RscHeader.rsh_tedinfo);
  160.     ObjectPtr = (OBJECT *)  (((long)ReadBuffer) + RscHeader.rsh_object);
  161.     TreeIndex = (OBJECT **) (((long)ReadBuffer) + RscHeader.rsh_trindex);
  162.  
  163.     i = 0;
  164.     while (i < RscHeader.rsh_nted)
  165.     {
  166.         TextPtr->te_ptext += (long) ReadBuffer;
  167.         TextPtr->te_ptmplt += (long) ReadBuffer;
  168.         TextPtr->te_pvalid += (long) ReadBuffer;
  169.         TextPtr++;
  170.         i += 1;
  171.     }
  172.  
  173.     i = 0;
  174.     while ((char *) &TreeIndex[i] < ReadBuffer + RscHeader.rsh_rssize)
  175.     {
  176.         TreeIndex[i] = (OBJECT *)((long) ReadBuffer + (char *) TreeIndex[i]);
  177.         i += 1;
  178.     }
  179.     TreeCount = i;
  180.     
  181.     
  182.     for (i = 0; i < RscHeader.rsh_nobs; i += 1)
  183.     {
  184.         switch(ObjectPtr->ob_type)
  185.         {
  186.         case G_STRING:
  187.         case G_BUTTON:
  188.         case G_TITLE:
  189.         case G_TEXT:
  190.         case G_BOXTEXT: 
  191.         case G_FTEXT:
  192.         case G_FBOXTEXT:
  193.                 if (ObjectPtr->ob_spec)
  194.                     ObjectPtr->ob_spec += (long) ReadBuffer;
  195.                 break;
  196.         default:
  197.                 break;
  198.         }
  199.         /* convert to Windows style coordinates */
  200.         j = (ObjectPtr->ob_x >> 8) & 0xFF;
  201.         ObjectPtr->ob_x &= 0xFF;        ObjectPtr->ob_x *= 4;
  202.         ObjectPtr->ob_x += j >> 1;
  203.  
  204.         j = (ObjectPtr->ob_y >> 8) & 0xFF;
  205.         ObjectPtr->ob_y &= 0xFF;        ObjectPtr->ob_y *= 8;
  206.         ObjectPtr->ob_y += j >> 1;
  207.  
  208.         j = (ObjectPtr->ob_width >> 8) & 0xFF;
  209.         ObjectPtr->ob_width &= 0xFF;        ObjectPtr->ob_width *= 4;
  210.         ObjectPtr->ob_width += j >> 1;
  211.  
  212.         j = (ObjectPtr->ob_height >> 8) & 0xFF;
  213.         ObjectPtr->ob_height &= 0xFF;        ObjectPtr->ob_height *= 8;
  214.         ObjectPtr->ob_height += j >> 1;
  215.  
  216.         ObjectPtr++;
  217.     }
  218.  
  219.     for (i = 0; i < TreeCount; i += 1)
  220.     {
  221.         ProcessObjTree(TreeIndex[i], i);
  222.     }
  223.  
  224.     if (DefFile) 
  225.         DefFile = fopen(OutHfile, "w");
  226.     if (DefFile)
  227.     {
  228.         for (i = 0; i < NumLabels; i += 1)
  229.         {
  230.             char    UserLabel[10];
  231.             bzero(UserLabel, sizeof(UserLabel));
  232.             strncpy(UserLabel,LabelArray[i].Entry.Label,
  233.                 sizeof(LabelArray[0].Entry.Label));
  234.             j = (LabelArray[i].MenuFlag == -1) ? i + ID_START : 
  235.                 LabelArray[i].MenuFlag;
  236.             fprintf(DefFile, "#define %s %d\n", UserLabel, j);
  237.         }
  238.         fclose(DefFile);
  239.     }
  240.  
  241.     fclose(RcFile);
  242.     return 0;
  243. }
  244.  
  245. static struct DefFileEntry *FindObjLabel(LabelArray, 
  246.             NumLabels, TreeIndex, ObIndex)
  247.     register struct DefLabel *LabelArray;
  248.     register int NumLabels;
  249.     register int TreeIndex;
  250.     register int ObIndex;
  251. {
  252.     while (NumLabels--)
  253.     {
  254.         if ((LabelArray->Entry.TreeNumber == TreeIndex) && 
  255.                 (LabelArray->Entry.ObjIndex == ObIndex) &&
  256.                 (LabelArray->Entry.Flag >= 256))
  257.             return &LabelArray->Entry;
  258.         LabelArray += 1;
  259.     }
  260.     return NULL;
  261. }
  262.  
  263. static struct DefFileEntry *FindTreeLabel(LabelArray, 
  264.             NumLabels, TreeIndex)
  265.     register struct DefLabel *LabelArray;
  266.     register int NumLabels;
  267.     register int TreeIndex;
  268. {
  269.     while (NumLabels--)
  270.     {
  271.         if ((LabelArray->Entry.Flag < 256) && 
  272.                 (LabelArray->Entry.ObjIndex == TreeIndex))
  273.             return &LabelArray->Entry;
  274.         LabelArray += 1;
  275.     }
  276.     return NULL;
  277. }
  278.  
  279. static int    AddGroupStop = 0;
  280.  
  281. PrChildren(Tree,TreeIndex, Parent, xParent, yParent)
  282.     register OBJECT *Tree;
  283.     int TreeIndex;
  284.     register int Parent;
  285.     int xParent;
  286.     int yParent;
  287. {
  288.     register int i;
  289.     struct DefLabel *Label;
  290.     char UserLabel[10];
  291.     TEDINFO *Ted;
  292.     int SavedRBtnIdx;
  293.     int    TmpRBtnIdx;
  294.     int FirstRButton;
  295.  
  296.     i = Tree[Parent].ob_head;
  297.  
  298.     SavedRBtnIdx = RadioBtnIdx;
  299.     FirstRButton = 1;
  300.     while (i != Parent)
  301.     {
  302.         bzero(UserLabel, sizeof(UserLabel));
  303.         Label = (struct DefLabel *)FindObjLabel(LabelArray,
  304.                 NumLabels, TreeIndex, i);
  305.         if (Label)
  306.         {
  307.             strncpy(UserLabel, Label->Entry.Label,
  308.                      sizeof(Label->Entry.Label));
  309.             UserLabel[sizeof(Label->Entry.Label)] = 0;
  310.         }
  311.         else
  312.             strcpy(UserLabel, "-1");
  313.  
  314.         switch(Tree[i].ob_type)
  315.         {
  316.             case    G_BOX:
  317.             case    G_IBOX:
  318.                 if (Tree[i].ob_head >= 0)
  319.                 {
  320.                     fprintf(RcFile,"    GROUPBOX \"\" %s, %d, %d, %d, %d",
  321.                             UserLabel, Tree[i].ob_x + xParent,
  322.                              Tree[i].ob_y + yParent,
  323.                             Tree[i].ob_width, Tree[i].ob_height);
  324.                     /* If we have started a radio button group */
  325.                     if (RadioBtnIdx != SavedRBtnIdx)
  326.                     {
  327.                         /* then "insert" any child group at a higher id */
  328.                         TmpRBtnIdx = RadioBtnIdx;
  329.                         RadioBtnIdx += MAX_RBTN_BOX;
  330.                     }
  331.                     else
  332.                         TmpRBtnIdx = 0;
  333.                     fprintf(RcFile, AddGroupStop ? ", WS_GROUP\n" : "\n");
  334.                     if (AddGroupStop)
  335.                     {
  336.                         AddGroupStop = 0;
  337.                         FirstRButton = 1;
  338.                     }
  339.                     PrChildren(Tree, TreeIndex, i,
  340.                             xParent + Tree[i].ob_x,
  341.                             yParent + Tree[i].ob_y);
  342.                     if (TmpRBtnIdx) RadioBtnIdx = TmpRBtnIdx;
  343.                 }    
  344.                 break;
  345.             case    G_BUTTON:
  346.                 fprintf(RcFile,
  347.                     "    %s    \"%s\" %s, %d, %d, %d, %d",
  348.                     Tree[i].ob_flags & RBUTTON ?
  349.                         (Label->MenuFlag = RadioBtnIdx++, "RADIOBUTTON") :
  350.                         (Tree[i].ob_flags & TOUCHEXIT ? 
  351.                             (Tree[i].ob_flags & DEFAULT ?
  352.                                 "DEFPUSHBUTTON" : "PUSHBUTTON") :
  353.                                 "CHECKBOX"),
  354.                     Tree[i].ob_spec, UserLabel, 
  355.                     Tree[i].ob_x + xParent,
  356.                     Tree[i].ob_y + yParent,
  357.                     Tree[i].ob_width, Tree[i].ob_height);
  358.                 if (Tree[i].ob_flags & RBUTTON)
  359.                 {
  360.                     if (FirstRButton)
  361.                     {
  362.                         fprintf(RcFile, ", WS_TABSTOP | WS_GROUP\n");
  363.                         FirstRButton = 0;
  364.                     }
  365.                     else
  366.                         fprintf(RcFile, "\n");
  367.                     AddGroupStop = 1;
  368.                 }
  369.                 else
  370.                 {
  371.                     fprintf(RcFile, ", WS_TABSTOP | WS_GROUP\n");
  372.                     AddGroupStop = 0;
  373.                 }
  374.                 break;
  375.             case    G_STRING:
  376.             case    G_TITLE:
  377.                 fprintf(RcFile,"    LTEXT    \"%s\" %s, %d, %d, %d, %d",
  378.                     Tree[i].ob_spec, UserLabel,
  379.                     Tree[i].ob_x + xParent, Tree[i].ob_y + yParent,
  380.                     Tree[i].ob_width, Tree[i].ob_height);
  381.                     fprintf(RcFile, AddGroupStop ? ", WS_GROUP\n" : "\n");
  382.                     if (AddGroupStop)
  383.                     {
  384.                         AddGroupStop = 0;
  385.                         FirstRButton = 1;
  386.                     }
  387.                 break;
  388.             case    G_FTEXT:
  389.             case    G_FBOXTEXT:
  390.             case    G_BOXTEXT:
  391.             case    G_TEXT:
  392.                 Ted = (TEDINFO *) Tree[i].ob_spec;
  393.                 if (Tree[i].ob_flags & EDITABLE)
  394.                     fprintf(RcFile,"    %s  %s, %d, %d, %d, %d",
  395.                          "EDITTEXT",
  396.                         UserLabel,
  397.                         Tree[i].ob_x + xParent, Tree[i].ob_y + yParent,
  398.                         Tree[i].ob_width, Tree[i].ob_height);
  399.                 else
  400.                     fprintf(RcFile,"    %s    \"%s\" %s, %d, %d, %d, %d",
  401.                         "LTEXT",
  402.                         Ted->te_ptext, UserLabel,
  403.                         Tree[i].ob_x + xParent, Tree[i].ob_y + yParent,
  404.                         Tree[i].ob_width, Tree[i].ob_height);
  405.                     fprintf(RcFile, AddGroupStop ? ", WS_GROUP\n" : "\n");
  406.                     if (AddGroupStop)
  407.                     {
  408.                         AddGroupStop = 0;
  409.                         FirstRButton = 1;
  410.                     }
  411.                 break;
  412.             case    G_BOXCHAR:
  413.                 break;
  414.         }
  415.         i = Tree[i].ob_next;
  416.     }
  417. }
  418.  
  419. PrMenu(Tree, TreeIndex)
  420. register OBJECT *Tree;
  421. int TreeIndex;
  422. {
  423.     int    TitleIndex;
  424.     int    EntryTrace;
  425.     int EntryBox;
  426.     int EntryQuit;
  427.     int    ColumnOne;
  428.     struct DefFileEntry *Label;
  429.     struct DefLabel *TitleLabel;
  430.     char    UserLabel[10];
  431.  
  432.     TitleIndex = Tree[0].ob_head;
  433.     EntryQuit = Tree[TitleIndex].ob_next;
  434.     TitleIndex = Tree[TitleIndex].ob_head;
  435.     TitleIndex = Tree[TitleIndex].ob_head;
  436.  
  437.     ColumnOne = 0;
  438.  
  439.     for (EntryBox = Tree[EntryQuit].ob_head;
  440.             EntryBox != EntryQuit;
  441.             (EntryBox = Tree[EntryBox].ob_next),
  442.                     (TitleIndex = Tree[TitleIndex].ob_next),
  443.                     (ColumnOne += 1))
  444.     /* go through the menu titles */
  445.     {
  446.         if (!ColumnOne)
  447.         {
  448.             continue;
  449.         }
  450.         TitleLabel = (struct DefLabel *)FindObjLabel(LabelArray,
  451.                         NumLabels, TreeIndex,
  452.                         TitleIndex);
  453.         if (TitleLabel) TitleLabel->MenuFlag = ColumnOne - 1;
  454.         fprintf(RcFile,
  455.             "    POPUP \"%s\"\n        {\n", Tree[TitleIndex].ob_spec);
  456.         for (EntryTrace = Tree[EntryBox].ob_head;
  457.                 EntryTrace != EntryBox;
  458.                 EntryTrace = Tree[EntryTrace].ob_next)
  459.         {
  460.  
  461.             bzero(UserLabel, sizeof(UserLabel));
  462.             Label = FindObjLabel(LabelArray, NumLabels, TreeIndex,
  463.                         EntryTrace);
  464.             if (Label)
  465.             {
  466.                 strncpy(UserLabel, Label->Label, sizeof(Label->Label));
  467.                 UserLabel[sizeof(Label->Label)] = 0;
  468.             }
  469.             else
  470.                 strcpy(UserLabel, "-1");
  471.  
  472.             fprintf(RcFile,"        MENUITEM \"%s\",    %s%s\n",
  473.                 Tree[EntryTrace].ob_spec,
  474.                 UserLabel,
  475.                 Tree[EntryTrace].ob_state & DISABLED ? " GRAYED" : "");
  476.         }
  477.         fprintf(RcFile,"        }\n");
  478.     }
  479. }
  480.  
  481. ProcessObjTree(Tree, TreeIndex)
  482.     OBJECT *Tree;
  483.     int TreeIndex;
  484. {
  485.     struct DefFileEntry *ObjectLabel;
  486.     char    UserLabel[10];
  487.  
  488.     AddGroupStop = 0;
  489.     ObjectLabel = FindTreeLabel(LabelArray, NumLabels, TreeIndex);
  490.     bzero(UserLabel, sizeof(UserLabel));
  491.     if (ObjectLabel)
  492.     {
  493.         strncpy(UserLabel, ObjectLabel->Label, sizeof(ObjectLabel->Label));
  494.         UserLabel[sizeof(ObjectLabel->Label)] = 0;
  495.     }
  496.     else
  497.         sprintf(UserLabel, "TREE%d", TreeIndex);
  498.     if (ObjectLabel && ObjectLabel->Flag == 2)
  499.     {
  500.         fprintf(RcFile,"%s MENU\n", UserLabel);
  501.         fprintf(RcFile,"    {\n");
  502.         PrMenu(Tree, TreeIndex, 0);
  503.         fprintf(RcFile,"    }\n\n");            
  504.     }
  505.     else
  506.     {
  507.         fprintf(RcFile,"%s DIALOG 10 10 %d %d\n",
  508.             UserLabel, Tree[0].ob_width,
  509.                     Tree[0].ob_height);
  510.         fprintf(RcFile,"    STYLE WS_POPUP | WS_DLGFRAME\n");
  511.         fprintf(RcFile,"    {\n");
  512.         PrChildren(Tree, TreeIndex, 0, Tree[0].ob_x, Tree[0].ob_y);
  513.         fprintf(RcFile, "    }\n\n");
  514.     }
  515.  
  516. }
  517.  
  518.