source: trunk/dll/menu.c@ 1039

Last change on this file since 1039 was 1039, checked in by Gregg Young, 17 years ago

Removed unnecessary xfrees and included fortify.h where needed; moved several misplaced (x)frees;

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1
2/***********************************************************************
3
4 $Id: menu.c 1039 2008-07-05 22:16:21Z 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 29 Feb 08 GKY Use xfree where appropriate
16
17***********************************************************************/
18
19#include <stdlib.h>
20#include <string.h>
21#include <share.h>
22
23#define INCL_WIN
24#define INCL_LONGLONG // dircnrs.h
25
26#include "menu.h"
27#include "errutil.h" // Dos_Error...
28#include "fm3dll.h"
29#include "fortify.h"
30
31#pragma data_seg(DATA2)
32
33static PSZ pszSrcFile = __FILE__;
34
35MENU *menuhead = NULL;
36
37INT tokenize(CHAR * str, INT max, CHAR ** tokens)
38{
39 INT x = 0;
40 CHAR *p;
41
42 if (str && max && tokens) {
43 p = str;
44 for (;;) {
45 p = skip_delim(p, " \t");
46 if (!p)
47 break;
48 tokens[x++] = p;
49 if (x == max)
50 break;
51 p = to_delim(p, " \t");
52 if (!p)
53 break;
54 *p = 0;
55 p++;
56 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
57 if (!*p)
58 break;
59 }
60 }
61 return x;
62}
63
64VOID FreeMenuList(MENU * head)
65{
66 MENU *info, *next;
67
68 info = head;
69 while (info) {
70 next = info->next;
71 xfree(info->text, pszSrcFile, __LINE__);
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 xfree(info->text, pszSrcFile, __LINE__);
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.