source: trunk/dll/menu.c@ 793

Last change on this file since 793 was 793, checked in by Gregg Young, 18 years ago

Move #pragma alloc_text to end for OpenWatcom compat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1
2/***********************************************************************
3
4 $Id: menu.c 793 2007-08-21 02:53:38Z gyoung $
5
6 Custom menu support routines for FM/2
7
8 Copyright (c) 1996-98 M. Kimes
9 Copyright (c) 2004, 2006 Steven H.Levine
10
11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 22 Jul 06 SHL Check more run time errors
13 29 Jul 06 SHL Use xfgets_bstripcr
14 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
15
16***********************************************************************/
17
18#define INCL_DOS
19#define INCL_WIN
20#include <os2.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <share.h>
26
27#include "fm3dll.h"
28#include "menu.h"
29
30#pragma data_seg(DATA2)
31
32static PSZ pszSrcFile = __FILE__;
33
34MENU *menuhead = NULL;
35
36INT tokenize(CHAR * str, INT max, CHAR ** tokens)
37{
38 INT x = 0;
39 CHAR *p;
40
41 if (str && max && tokens) {
42 p = str;
43 for (;;) {
44 p = skip_delim(p, " \t");
45 if (!p)
46 break;
47 tokens[x++] = p;
48 if (x == max)
49 break;
50 p = to_delim(p, " \t");
51 if (!p)
52 break;
53 *p = 0;
54 p++;
55 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
56 if (!*p)
57 break;
58 }
59 }
60 return x;
61}
62
63VOID FreeMenuList(MENU * head)
64{
65 MENU *info, *next;
66
67 info = head;
68 while (info) {
69 next = info->next;
70 if (info->text)
71 free(info->text);
72 free(info);
73 info = next;
74 }
75}
76
77BOOL AddToMenu(CHAR * filename, HWND hwndMenu)
78{
79 FILE *fp;
80 CHAR s[256];
81 CHAR *tokens[3];
82 INT lines = 0;
83 MENU *info, *last = NULL;
84 BOOL ret = FALSE;
85
86 // fixme to complain?
87 if (!hwndMenu) {
88 Runtime_Error(pszSrcFile, __LINE__, "no data");
89 return ret;
90 }
91 if (!filename)
92 filename = "FM3MENU.DAT";
93 fp = _fsopen(filename, "r", SH_DENYWR);
94 if (!fp) {
95 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
96 }
97 else {
98 while (!feof(fp)) {
99 if (!xfgets_bstripcr(s, sizeof(s), fp, pszSrcFile, __LINE__))
100 break;
101 lines++;
102 if (!*s || *s == ';')
103 continue;
104 if (tokenize(s, 3, tokens) == 3 && (USHORT) atoi(tokens[1])) {
105 info = xmallocz(sizeof(MENU), pszSrcFile, __LINE__);
106 if (info) {
107 info->size = sizeof(MENU);
108 info->text = xstrdup(tokens[2], pszSrcFile, __LINE__);
109 if (!info->text)
110 free(info);
111 else {
112 if (!stricmp(tokens[0], "MENUITEM"))
113 info->cmd = atoi(tokens[1]);
114 else if (!stricmp(tokens[0], "SEPARATOR"))
115 info->type = SEPARATOR;
116 else {
117 /* error! */
118 free(info->text);
119 free(info);
120 info = NULL;
121 }
122 if (info) {
123 if (!menuhead)
124 menuhead = info;
125 else
126 last->next = info;
127 info->next = NULL;
128 last = info;
129 }
130 }
131 }
132 }
133 else {
134 // fixme to complain?
135 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
136 }
137 }
138 fclose(fp);
139
140 if (menuhead) {
141 MENUITEM mi;
142
143 memset(&mi, 0, sizeof(mi));
144 info = menuhead;
145 WinEnableWindow(hwndMenu, FALSE);
146 while (info) {
147 mi.iPosition = MIT_END;
148 mi.id = info->cmd;
149 mi.afStyle =
150 (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
151 if (WinSendMsg
152 (hwndMenu, MM_INSERTITEM, MPFROMP(&mi), MPFROMP(info->text)))
153 ret = TRUE;
154 info = info->next;
155 }
156 WinEnableWindow(hwndMenu, TRUE);
157 FreeMenuList(menuhead);
158 menuhead = NULL;
159 }
160 }
161 return ret;
162}
163
164#pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
Note: See TracBrowser for help on using the repository browser.