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
RevLine 
[2]1
[123]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
[356]9 Copyright (c) 2004, 2006 Steven H.Levine
[123]10
[356]11 01 Aug 04 SHL Rework lstrip/rstrip usage
12 22 Jul 06 SHL Check more run time errors
[406]13 29 Jul 06 SHL Use xfgets_bstripcr
[793]14 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[985]15 29 Feb 08 GKY Use xfree where appropriate
[123]16
17***********************************************************************/
18
[2]19#include <stdlib.h>
20#include <string.h>
21#include <share.h>
[356]22
[907]23#define INCL_WIN
24#define INCL_LONGLONG // dircnrs.h
25
26#include "menu.h"
27#include "errutil.h" // Dos_Error...
[2]28#include "fm3dll.h"
[1039]29#include "fortify.h"
[2]30
31#pragma data_seg(DATA2)
[356]32
33static PSZ pszSrcFile = __FILE__;
34
[2]35MENU *menuhead = NULL;
36
[551]37INT tokenize(CHAR * str, INT max, CHAR ** tokens)
[356]38{
[551]39 INT x = 0;
[2]40 CHAR *p;
41
[551]42 if (str && max && tokens) {
[2]43 p = str;
[551]44 for (;;) {
45 p = skip_delim(p, " \t");
46 if (!p)
47 break;
[2]48 tokens[x++] = p;
[551]49 if (x == max)
50 break;
51 p = to_delim(p, " \t");
52 if (!p)
53 break;
[2]54 *p = 0;
55 p++;
[356]56 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
[551]57 if (!*p)
58 break;
[2]59 }
60 }
61 return x;
62}
63
[551]64VOID FreeMenuList(MENU * head)
[356]65{
[551]66 MENU *info, *next;
[2]67
68 info = head;
[551]69 while (info) {
[2]70 next = info->next;
[1009]71 xfree(info->text, pszSrcFile, __LINE__);
[1039]72 free(info);
[2]73 info = next;
74 }
75}
76
[551]77BOOL AddToMenu(CHAR * filename, HWND hwndMenu)
[356]78{
[2]79 FILE *fp;
[551]80 CHAR s[256];
[2]81 CHAR *tokens[3];
[551]82 INT lines = 0;
83 MENU *info, *last = NULL;
84 BOOL ret = FALSE;
[2]85
[356]86 // fixme to complain?
87 if (!hwndMenu) {
88 Runtime_Error(pszSrcFile, __LINE__, "no data");
[2]89 return ret;
[356]90 }
91 if (!filename)
[2]92 filename = "FM3MENU.DAT";
[551]93 fp = _fsopen(filename, "r", SH_DENYWR);
[356]94 if (!fp) {
95 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
96 }
97 else {
98 while (!feof(fp)) {
[551]99 if (!xfgets_bstripcr(s, sizeof(s), fp, pszSrcFile, __LINE__))
100 break;
[2]101 lines++;
[551]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)
[1039]110 free(info);
[356]111 else {
[551]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 {
[356]117 /* error! */
[1009]118 xfree(info->text, pszSrcFile, __LINE__);
[1039]119 free(info);
[551]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 }
[2]132 }
[356]133 else {
134 // fixme to complain?
[551]135 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
[2]136 }
137 }
138 fclose(fp);
139
[356]140 if (menuhead) {
[2]141 MENUITEM mi;
142
[551]143 memset(&mi, 0, sizeof(mi));
[2]144 info = menuhead;
[551]145 WinEnableWindow(hwndMenu, FALSE);
[356]146 while (info) {
[551]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;
[2]155 }
[551]156 WinEnableWindow(hwndMenu, TRUE);
[2]157 FreeMenuList(menuhead);
158 menuhead = NULL;
159 }
160 }
161 return ret;
162}
[793]163
164#pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
Note: See TracBrowser for help on using the repository browser.