source: trunk/dll/menu.c@ 433

Last change on this file since 433 was 406, checked in by root, 19 years ago

Use xfgets_bstripcr

  • 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
4 $Id: menu.c 406 2006-07-29 20:02:17Z root $
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
15***********************************************************************/
16
17#define INCL_DOS
18#define INCL_WIN
19#include <os2.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <share.h>
25
26#include "fm3dll.h"
27#include "menu.h"
28
29#pragma data_seg(DATA2)
30
31static PSZ pszSrcFile = __FILE__;
32
33#pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
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
64
65VOID FreeMenuList (MENU *head)
66{
67 MENU *info,*next;
68
69 info = head;
70 while(info) {
71 next = info->next;
72 if(info->text)
73 free(info->text);
74 free(info);
75 info = next;
76 }
77}
78
79
80BOOL AddToMenu (CHAR *filename,HWND hwndMenu)
81{
82 FILE *fp;
83 CHAR s[256];
84 CHAR *tokens[3];
85 INT lines = 0;
86 MENU *info,*last = NULL;
87 BOOL ret = FALSE;
88
89 // fixme to complain?
90 if (!hwndMenu) {
91 Runtime_Error(pszSrcFile, __LINE__, "no data");
92 return ret;
93 }
94 if (!filename)
95 filename = "FM3MENU.DAT";
96 fp = _fsopen(filename,"r",SH_DENYWR);
97 if (!fp) {
98 // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
99 }
100 else {
101 while (!feof(fp)) {
102 if (!xfgets_bstripcr(s,sizeof(s),fp,pszSrcFile,__LINE__))
103 break;
104 lines++;
105 if(!*s || *s == ';')
106 continue;
107 if (tokenize(s,3,tokens) == 3 && (USHORT)atoi(tokens[1])) {
108 info = xmallocz(sizeof(MENU),pszSrcFile,__LINE__);
109 if (info) {
110 info->size = sizeof(MENU);
111 info->text = xstrdup(tokens[2],pszSrcFile,__LINE__);
112 if (!info->text)
113 free(info);
114 else {
115 if (!stricmp(tokens[0],"MENUITEM"))
116 info->cmd = atoi(tokens[1]);
117 else if (!stricmp(tokens[0],"SEPARATOR"))
118 info->type = SEPARATOR;
119 else {
120 /* error! */
121 free(info->text);
122 free(info);
123 info = NULL;
124 }
125 if (info) {
126 if(!menuhead)
127 menuhead = info;
128 else
129 last->next = info;
130 info->next = NULL;
131 last = info;
132 }
133 }
134 }
135 }
136 else {
137 // fixme to complain?
138 // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
139 }
140 }
141 fclose(fp);
142
143 if (menuhead) {
144 MENUITEM mi;
145
146 memset(&mi,0,sizeof(mi));
147 info = menuhead;
148 WinEnableWindow(hwndMenu,FALSE);
149 while (info) {
150 mi.iPosition = MIT_END;
151 mi.id = info->cmd;
152 mi.afStyle = (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
153 if (WinSendMsg(hwndMenu, MM_INSERTITEM, MPFROMP(&mi),
154 MPFROMP(info->text)))
155 ret = TRUE;
156 info = info->next;
157 }
158 WinEnableWindow(hwndMenu,TRUE);
159 FreeMenuList(menuhead);
160 menuhead = NULL;
161 }
162 }
163 return ret;
164}
Note: See TracBrowser for help on using the repository browser.