source: trunk/dll/menu.c@ 907

Last change on this file since 907 was 907, checked in by Steven Levine, 18 years ago

Avoid out of memory traps in Compare Directories
Rework Compare Directories progress display for 2 second update rate
Start refactoring to reduce dependence on fm3dll.h
Add timer services (IsITimerExpired etc.)

  • 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 907 2008-01-06 07:26:17Z stevenhl $
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#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 "menu.h"
26#include "errutil.h" // Dos_Error...
27#include "fm3dll.h"
28
29#pragma data_seg(DATA2)
30
31static PSZ pszSrcFile = __FILE__;
32
33MENU *menuhead = NULL;
34
35INT tokenize(CHAR * str, INT max, CHAR ** tokens)
36{
37 INT x = 0;
38 CHAR *p;
39
40 if (str && max && tokens) {
41 p = str;
42 for (;;) {
43 p = skip_delim(p, " \t");
44 if (!p)
45 break;
46 tokens[x++] = p;
47 if (x == max)
48 break;
49 p = to_delim(p, " \t");
50 if (!p)
51 break;
52 *p = 0;
53 p++;
54 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
55 if (!*p)
56 break;
57 }
58 }
59 return x;
60}
61
62VOID FreeMenuList(MENU * head)
63{
64 MENU *info, *next;
65
66 info = head;
67 while (info) {
68 next = info->next;
69 if (info->text)
70 free(info->text);
71 free(info);
72 info = next;
73 }
74}
75
76BOOL AddToMenu(CHAR * filename, HWND hwndMenu)
77{
78 FILE *fp;
79 CHAR s[256];
80 CHAR *tokens[3];
81 INT lines = 0;
82 MENU *info, *last = NULL;
83 BOOL ret = FALSE;
84
85 // fixme to complain?
86 if (!hwndMenu) {
87 Runtime_Error(pszSrcFile, __LINE__, "no data");
88 return ret;
89 }
90 if (!filename)
91 filename = "FM3MENU.DAT";
92 fp = _fsopen(filename, "r", SH_DENYWR);
93 if (!fp) {
94 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
95 }
96 else {
97 while (!feof(fp)) {
98 if (!xfgets_bstripcr(s, sizeof(s), fp, pszSrcFile, __LINE__))
99 break;
100 lines++;
101 if (!*s || *s == ';')
102 continue;
103 if (tokenize(s, 3, tokens) == 3 && (USHORT) atoi(tokens[1])) {
104 info = xmallocz(sizeof(MENU), pszSrcFile, __LINE__);
105 if (info) {
106 info->size = sizeof(MENU);
107 info->text = xstrdup(tokens[2], pszSrcFile, __LINE__);
108 if (!info->text)
109 free(info);
110 else {
111 if (!stricmp(tokens[0], "MENUITEM"))
112 info->cmd = atoi(tokens[1]);
113 else if (!stricmp(tokens[0], "SEPARATOR"))
114 info->type = SEPARATOR;
115 else {
116 /* error! */
117 free(info->text);
118 free(info);
119 info = NULL;
120 }
121 if (info) {
122 if (!menuhead)
123 menuhead = info;
124 else
125 last->next = info;
126 info->next = NULL;
127 last = info;
128 }
129 }
130 }
131 }
132 else {
133 // fixme to complain?
134 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
135 }
136 }
137 fclose(fp);
138
139 if (menuhead) {
140 MENUITEM mi;
141
142 memset(&mi, 0, sizeof(mi));
143 info = menuhead;
144 WinEnableWindow(hwndMenu, FALSE);
145 while (info) {
146 mi.iPosition = MIT_END;
147 mi.id = info->cmd;
148 mi.afStyle =
149 (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
150 if (WinSendMsg
151 (hwndMenu, MM_INSERTITEM, MPFROMP(&mi), MPFROMP(info->text)))
152 ret = TRUE;
153 info = info->next;
154 }
155 WinEnableWindow(hwndMenu, TRUE);
156 FreeMenuList(menuhead);
157 menuhead = NULL;
158 }
159 }
160 return ret;
161}
162
163#pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
Note: See TracBrowser for help on using the repository browser.