source: trunk/dll/menu.c@ 1179

Last change on this file since 1179 was 1179, checked in by John Small, 17 years ago

Ticket 187: Draft 2: Move remaining function declarations

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