@@ -64,19 +64,149 @@ void DoFileOpen(void) {
6464 }
6565}
6666
67+ /*============================================================================
68+ ** Recent Files
69+ **============================================================================*/
70+
71+ void AddRecentFile (const wchar_t * path ) {
72+ int i , j ;
73+ /* Don't add :memory: */
74+ if (lstrcmpW (path , L":memory:" ) == 0 ) return ;
75+ /* Check if already in list, move to top if so */
76+ for (i = 0 ; i < g_recentCount ; i ++ ) {
77+ if (lstrcmpiW (g_recentFiles [i ], path ) == 0 ) {
78+ /* Move to top */
79+ for (j = i ; j > 0 ; j -- )
80+ lstrcpyW (g_recentFiles [j ], g_recentFiles [j - 1 ]);
81+ lstrcpyW (g_recentFiles [0 ], path );
82+ UpdateRecentMenu ();
83+ return ;
84+ }
85+ }
86+ /* Shift down and add at top */
87+ for (i = MAX_RECENT_FILES - 1 ; i > 0 ; i -- )
88+ lstrcpyW (g_recentFiles [i ], g_recentFiles [i - 1 ]);
89+ lstrcpyW (g_recentFiles [0 ], path );
90+ if (g_recentCount < MAX_RECENT_FILES ) g_recentCount ++ ;
91+ UpdateRecentMenu ();
92+ }
93+
94+ void UpdateRecentMenu (void ) {
95+ int i , j ;
96+
97+ if (!g_hRecentDbMenu ) return ;
98+
99+ /* Clear and rebuild */
100+ while (RemoveMenu (g_hRecentDbMenu , 0 , MF_BYPOSITION ));
101+
102+ if (g_recentCount == 0 ) {
103+ AppendMenuW (g_hRecentDbMenu , MF_STRING | MF_GRAYED , 0 , L"(none)" );
104+ } else {
105+ for (i = 0 ; i < g_recentCount ; i ++ ) {
106+ wchar_t item [MAX_PATH + 8 ];
107+ const wchar_t * name = g_recentFiles [i ];
108+ int len = lstrlenW (name );
109+ for (j = len - 1 ; j >= 0 ; j -- )
110+ if (name [j ] == '\\' ) { name = & g_recentFiles [i ][j + 1 ]; break ; }
111+ wsprintfW (item , L"&%d %s" , i + 1 , name );
112+ AppendMenuW (g_hRecentDbMenu , MF_STRING , IDM_RECENT_BASE + i , item );
113+ }
114+ }
115+ }
116+
117+ void AddRecentQuery (const wchar_t * path ) {
118+ int i , j ;
119+ /* Check if already in list, move to top if so */
120+ for (i = 0 ; i < g_recentQueryCount ; i ++ ) {
121+ if (lstrcmpiW (g_recentQueries [i ], path ) == 0 ) {
122+ for (j = i ; j > 0 ; j -- )
123+ lstrcpyW (g_recentQueries [j ], g_recentQueries [j - 1 ]);
124+ lstrcpyW (g_recentQueries [0 ], path );
125+ UpdateRecentQueryMenu ();
126+ return ;
127+ }
128+ }
129+ /* Shift down and add at top */
130+ for (i = MAX_RECENT_FILES - 1 ; i > 0 ; i -- )
131+ lstrcpyW (g_recentQueries [i ], g_recentQueries [i - 1 ]);
132+ lstrcpyW (g_recentQueries [0 ], path );
133+ if (g_recentQueryCount < MAX_RECENT_FILES ) g_recentQueryCount ++ ;
134+ UpdateRecentQueryMenu ();
135+ }
136+
137+ void UpdateRecentQueryMenu (void ) {
138+ int i , j ;
139+
140+ if (!g_hRecentQueryMenu ) return ;
141+
142+ while (RemoveMenu (g_hRecentQueryMenu , 0 , MF_BYPOSITION ));
143+
144+ if (g_recentQueryCount == 0 ) {
145+ AppendMenuW (g_hRecentQueryMenu , MF_STRING | MF_GRAYED , 0 , L"(none)" );
146+ } else {
147+ for (i = 0 ; i < g_recentQueryCount ; i ++ ) {
148+ wchar_t item [MAX_PATH + 8 ];
149+ const wchar_t * name = g_recentQueries [i ];
150+ int len = lstrlenW (name );
151+ for (j = len - 1 ; j >= 0 ; j -- )
152+ if (name [j ] == '\\' ) { name = & g_recentQueries [i ][j + 1 ]; break ; }
153+ wsprintfW (item , L"&%d %s" , i + 1 , name );
154+ AppendMenuW (g_hRecentQueryMenu , MF_STRING , IDM_RECENT_QUERY_BASE + i , item );
155+ }
156+ }
157+ }
158+
67159/*============================================================================
68160** Query File Operations
69161**============================================================================*/
70162
71- void DoOpenQuery (void ) {
72- CE_OPENFILENAME ofn ;
73- wchar_t szFile [MAX_PATH ] = L"" ;
163+ void OpenQueryFile (const wchar_t * path ) {
74164 HANDLE hFile ;
75165 DWORD dwSize , dwRead ;
76166 char * buf ;
77167 wchar_t * wbuf ;
78168 int i , j , extraCR ;
79169
170+ hFile = CreateFileW (path , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , 0 , NULL );
171+ if (hFile != INVALID_HANDLE_VALUE ) {
172+ dwSize = GetFileSize (hFile , NULL );
173+ if (dwSize < 65536 ) {
174+ buf = (char * )LocalAlloc (LMEM_FIXED , dwSize + 1 );
175+ if (buf && ReadFile (hFile , buf , dwSize , & dwRead , NULL )) {
176+ buf [dwRead ] = '\0' ;
177+ extraCR = 0 ;
178+ for (i = 0 ; i < (int )dwRead ; i ++ )
179+ if (buf [i ] == '\n' && (i == 0 || buf [i - 1 ] != '\r' )) extraCR ++ ;
180+ wbuf = (wchar_t * )LocalAlloc (LMEM_FIXED , (dwRead + extraCR + 1 ) * sizeof (wchar_t ));
181+ if (wbuf ) {
182+ for (i = 0 , j = 0 ; i < (int )dwRead ; i ++ ) {
183+ if (buf [i ] == '\n' && (i == 0 || buf [i - 1 ] != '\r' ))
184+ wbuf [j ++ ] = '\r' ;
185+ wbuf [j ++ ] = (wchar_t )(unsigned char )buf [i ];
186+ }
187+ wbuf [j ] = 0 ;
188+ g_showingHint = 0 ;
189+ SetWindowTextW (g_hwndQuery , wbuf );
190+ UpdateWindow (g_hwndQuery );
191+ lstrcpyW (g_szQueryPath , path );
192+ AddRecentQuery (path );
193+ g_queryDirty = 0 ;
194+ UpdateTitle ();
195+ UpdateLineNumbers ();
196+ LocalFree (wbuf );
197+ }
198+ }
199+ if (buf ) LocalFree (buf );
200+ }
201+ CloseHandle (hFile );
202+ }
203+ }
204+
205+ void DoOpenQuery (void ) {
206+ CE_OPENFILENAME ofn ;
207+ wchar_t szFile [MAX_PATH ] = L"" ;
208+ int i ;
209+
80210 memset (& ofn , 0 , sizeof (ofn ));
81211 ofn .lStructSize = sizeof (ofn );
82212 ofn .hwndOwner = g_hwndMain ;
@@ -94,40 +224,7 @@ void DoOpenQuery(void) {
94224 if (g_szLastQueryDir [i ] == '\\' ) { g_szLastQueryDir [i ] = 0 ; break ; }
95225 }
96226 if (g_szLastQueryDir [0 ] == 0 ) lstrcpyW (g_szLastQueryDir , L"\\" );
97-
98- hFile = CreateFileW (szFile , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , 0 , NULL );
99- if (hFile != INVALID_HANDLE_VALUE ) {
100- dwSize = GetFileSize (hFile , NULL );
101- if (dwSize < 65536 ) {
102- buf = (char * )LocalAlloc (LMEM_FIXED , dwSize + 1 );
103- if (buf && ReadFile (hFile , buf , dwSize , & dwRead , NULL )) {
104- buf [dwRead ] = '\0' ;
105- /* Count LFs not preceded by CR */
106- extraCR = 0 ;
107- for (i = 0 ; i < (int )dwRead ; i ++ )
108- if (buf [i ] == '\n' && (i == 0 || buf [i - 1 ] != '\r' )) extraCR ++ ;
109- wbuf = (wchar_t * )LocalAlloc (LMEM_FIXED , (dwRead + extraCR + 1 ) * sizeof (wchar_t ));
110- if (wbuf ) {
111- for (i = 0 , j = 0 ; i < (int )dwRead ; i ++ ) {
112- if (buf [i ] == '\n' && (i == 0 || buf [i - 1 ] != '\r' ))
113- wbuf [j ++ ] = '\r' ;
114- wbuf [j ++ ] = (wchar_t )(unsigned char )buf [i ];
115- }
116- wbuf [j ] = 0 ;
117- g_showingHint = 0 ;
118- SetWindowTextW (g_hwndQuery , wbuf );
119- UpdateWindow (g_hwndQuery );
120- lstrcpyW (g_szQueryPath , szFile );
121- g_queryDirty = 0 ;
122- UpdateTitle ();
123- UpdateLineNumbers ();
124- LocalFree (wbuf );
125- }
126- }
127- if (buf ) LocalFree (buf );
128- }
129- CloseHandle (hFile );
130- }
227+ OpenQueryFile (szFile );
131228 }
132229}
133230
0 commit comments