root/trunk/sam6/sam6.c

Revision 14, 12.4 kB (checked in by jvw, 2 years ago)

HEX/ASCII (sector) editor control added

Line 
1#define SAM_D "Text-viewer sample for JvW-Fsys TXW OpenWatcom build environment."
2
3#define SAM_C "(c) 2005: Jan van Wijk"
4
5#define SAM_V "1.00 26-09-2005" // Initial version
6
7#include <txlib.h>                              // TX library interface
8#if   defined (WIN32)
9   #define SAM_N "SAM6 winNT"
10#elif defined (DOS32)
11   #define SAM_N "SAM6 Dos32"
12#elif defined (LINUX)
13   #define SAM_N "SAM6 Linux"
14#else
15   #define SAM_N "SAM6  OS/2"
16#endif
17
18
19char *usagetext[] =
20{
21   "  filename",
22   "",
23   "  Switch character for EXE switches is '-' or '/'. All single letter",
24   "  switches are case-sensitive, long switchnames like 'query' are not.",
25   "",
26#if defined (DUMP)
27   " -123[t][s][l][r][dxx] = trace level 123, [t]stamp; [s]creen; No [l]ogging;"
28   "                                          [r]e-open [d]elay xx ms per line",
29#endif
30   "",
31   " -?            = help on executable commandline switches (this text)",
32   " -7            = Use 7-bit ASCII only (no 'graphic' characters)",
33   " -a            = switch off usage of ANSI escape characters for color",
34   " -l:logfile    = start logging immediately to 'logfile.log'",
35   "",
36   NULL
37};
38
39static char        sam_footer[]    = " F1=Help F3=Quit F8=OpenFile ";
40static TXLN        sam_title       = {0};
41static TXLN        sam_filename    = {0};
42static TXWHANDLE   view;                    // the text-view window
43static char      **viewtext = NULL;         // attached text, or NULL
44
45// SAM6 application messages
46#define SAM_FILEDIALOG    TXWM_USER             // prompt for new file
47#define SAM_ATTACHFILE    TXWM_USER +1          // attach named file to window
48
49
50// SAMPLE standard window procedure, for any window-class
51static ULONG samStdWindowProc                   // RET   result
52(
53   TXWHANDLE           hwnd,                    // IN    current window
54   ULONG               msg,                     // IN    message id
55   ULONG               mp1,                     // IN    msg param 1
56   ULONG               mp2                      // IN    msg param 2
57);
58
59// read specified file, and attach to global anchor; update title
60static BOOL samLoadNewFile
61(
62   char               *name                     // IN    name of the file to attach
63);
64
65// SAMView read-file procedure
66static char **samvReadText                      // RET   ptr to text-array
67(
68   FILE               *file,                    // IN    file opened for read
69   ULONG              *size                     // OUT   size in lines
70);
71
72// SAMView free memory for view-text
73static void  samvDiscardText
74(
75   char              **text                     // IN    ptr to text-array
76);
77
78
79int main (int argc, char *argv[]);
80
81/*****************************************************************************/
82/* Main function of the program, handle commandline-arguments                */
83/*****************************************************************************/
84int main (int argc, char *argv[])
85{
86   ULONG               rc = NO_ERROR;           // function return
87   TXWQMSG             qmsg;
88   TXWINDOW            setup;                   // setup data
89   TXWINDOW           *win;
90
91   TxINITmain( "SAMTRACE", "SAM", FALSE, 0);
92
93   if (TxaExeSwitch('l'))                       // start logfile now ?
94   {
95      TxAppendToLogFile( TxaExeSwitchStr( 'l', NULL, "sam"), TRUE);
96   }
97   if (TxaExeSwitch('?'))                       // switch help requested
98   {
99      TxPrint( "\n%s %s %s\n%s\n\nUsage: %s",
100                  SAM_N, SAM_V, SAM_C, SAM_D, TxaExeArgv(0));
101      TxShowTxt( usagetext);
102   }
103   else
104   {
105      if (txwInitializeDesktop( NULL, NULL) != 0)
106      {
107         strcpy( sam_title, SAM_N " " SAM_V "; " SAM_C); // initial title
108         txwSetupWindowData(
109            0, 0,                               // upper left corner
110            TxScreenRows(),                     // vertical size   full-screen
111            TxScreenCols(),                     // horizontal size full-screen
112            TXWS_FRAMED        |                // borders (scroll indicator)
113            TXWS_SAVEBITS      |                // save underlying screen
114            TXWS_MOVEABLE      |                // allow window movement
115            TXCS_CLOSE_BUTTON,                  // include a close button [X]
116            0,                                  // no help for now
117            ' ', ' ', TXWSCHEME_COLORS,         // default colors
118            sam_title,                          // variable title line
119            sam_footer,                         // footer, function-key help
120            &setup);                            // resulting window data structure
121
122         view = txwCreateWindow(
123                   0,                           // parent window
124                   TXW_TEXTVIEW,                // class of this window
125                   0,                           // owner window
126                   0,                           // insert after ...
127                   &setup,                      // window setup data
128                   samStdWindowProc);
129
130         win = txwWindowData( view);
131         win->tv.topline = 0;                   // set to start of text
132         win->tv.leftcol = 0;
133         win->tv.maxtop  = TXW_INVALID;
134         win->tv.maxcol  = TXW_INVALID;
135         win->tv.buf     = NULL;                // no text to start with
136
137         txwShowWindow( view, TRUE);            // make sure it is visible
138         txwSetFocus(   view);                  // and gets input focus
139
140         if (TxaExeArgc() > 1)                  // filename specified
141         {
142            strcpy( sam_filename, TxaExeArgv(1));
143            txwPostMsg( view, SAM_ATTACHFILE, 0, 0);
144         }
145         else                                   // start with file-open dialog
146         {
147            txwPostMsg( view, SAM_FILEDIALOG, 0, 0);
148         }
149
150         while (txwGetMsg(  &qmsg))
151         {
152            txwDispatchMsg( &qmsg);
153         }
154         txwTerminateDesktop();
155      }
156      else
157      {
158         TxPrint("Failed to intialize desktop\n");
159      }
160      samvDiscardText( viewtext);
161   }
162   TxEXITmain(rc);                              // TX Exit code, incl tracing
163}                                               // end 'main'
164/*---------------------------------------------------------------------------*/
165
166
167/*****************************************************************************/
168// SAMPLE standard window procedure, for any window-class
169/*****************************************************************************/
170static ULONG samStdWindowProc                   // RET   result
171(
172   TXWHANDLE           hwnd,                    // IN    current window
173   ULONG               msg,                     // IN    message id
174   ULONG               mp1,                     // IN    msg param 1
175   ULONG               mp2                      // IN    msg param 2
176)
177{
178   ULONG               rc   = NO_ERROR;
179   TXWINDOW           *win;
180   TXLN                text;
181
182   ENTER();
183   TRCMSG( hwnd, msg, mp1, mp2);
184   if (hwnd != 0)
185   {
186      TRCLAS( "SAM std ",  hwnd);
187      win = txwWindowData( hwnd);
188      switch (msg)
189      {
190         case TXWM_CHAR:
191            switch (mp2)
192            {
193               case TXc_O:
194               case TXk_F8:                     // open new file
195                  txwPostMsg( hwnd, SAM_FILEDIALOG, 0, 0);
196                  break;
197
198               case TXk_ESCAPE:
199                  txwPostMsg( hwnd, TXWM_CLOSE, 0, 0);
200                  break;
201
202               default:
203                  rc = txwDefWindowProc( hwnd, msg, mp1, mp2);
204                  break;
205            }
206            break;
207
208         case SAM_FILEDIALOG:
209            strcpy( text, "*.?;*.log;*.txt");     // default extensions
210            if (txwOpenFileDialog( text, NULL, NULL, 0, NULL,
211                " Select a (text) file for viewing ", text))
212            {
213               strcpy( sam_filename, text);
214               txwPostMsg( view, SAM_ATTACHFILE, 0, 0);
215            }
216            break;
217
218         case SAM_ATTACHFILE:
219            if (samLoadNewFile( sam_filename))
220            {
221               win->tv.buf = viewtext;          // refresh text in view window
222               txwInvalidateWindow( hwnd, TRUE, FALSE); // and have it painted
223            }
224            break;
225
226         default:
227            rc = txwDefWindowProc( hwnd, msg, mp1, mp2);
228            break;
229      }
230   }
231   else
232   {
233      rc = TX_INVALID_HANDLE;
234   }
235   RETURN( rc);
236}                                               // end 'samStdWindowProc'
237/*---------------------------------------------------------------------------*/
238
239
240/*****************************************************************************/
241// read specified file, and attach to global anchor; update title
242/*****************************************************************************/
243static BOOL samLoadNewFile
244(
245   char               *name                     // IN    name of the file to attach
246)
247{
248   BOOL                rc = FALSE;              // function return
249   FILE               *vf;                      // file pointer
250   ULONG               nr;                      // nr of lines in file
251
252   ENTER();
253
254   if ((vf = fopen( name, "r")) != NULL)
255   {
256      samvDiscardText( viewtext);               // Discard old text, if any
257      if ((viewtext = samvReadText( vf, &nr)) != NULL)
258      {
259         sprintf( sam_title, "%s - %lu lines", name, nr); // updated title
260         rc = TRUE;
261      }
262      else
263      {
264         TxMessage( TRUE, 0, "\nError reading file: %s\n", name);
265      }
266      fclose( vf);
267   }
268   else
269   {
270      TxMessage( TRUE, 0, "\nFile '%s' not found\n", name);
271   }
272   BRETURN (rc);
273}                                               // end 'samLoadNewFile'
274/*---------------------------------------------------------------------------*/
275
276
277/*****************************************************************************/
278// SAMView read-file procedure
279/*****************************************************************************/
280static char **samvReadText                      // RET   ptr to text-array
281(
282   FILE               *file,                    // IN    file opened for read
283   ULONG              *size                     // OUT   size in lines
284)
285{
286   char              **data  = NULL;
287   char               *line  = NULL;
288   char               *new   = NULL;
289   ULONG               lines = 0;
290   ULONG               nr;
291
292   ENTER();
293
294   if ((line = TxAlloc( 1, TXMAX4K)) != NULL)
295   {
296      fseek( file, 0, SEEK_SET);
297      while (fgets( line, TXMAX4K, file) != NULL)
298      {
299         lines++;
300      }
301      TRACES(( "Opened file has %lu lines ...\n", lines));
302      if ((data = TxAlloc( lines +1, sizeof(char *))) != NULL)
303      {
304         fseek( file, 0, SEEK_SET);
305         for (nr = 0; nr < lines; nr++)
306         {
307            if (fgets( line, TXMAX4K, file) != NULL)
308            {
309               if ((new = TxAlloc( 1, strlen(line)+1)) != NULL)
310               {
311                  if (line[strlen(line)-1]=='\n')
312                  {
313                    line[strlen(line)-1]='\0';
314                  }
315                  strcpy( new, line);
316                  data[ nr] = new;
317               }
318               else                             // quit reading on memory error
319               {
320                  break;
321               }
322            }
323            else                                // unexpected read failure
324            {
325               break;
326            }
327         }
328         *size = lines;
329      }
330      TxFreeMem( line);
331   }
332   RETURN( data);
333}                                               // end 'samvReadText'
334/*---------------------------------------------------------------------------*/
335
336
337/*****************************************************************************/
338// SAMView free memory for view-text
339/*****************************************************************************/
340static void  samvDiscardText
341(
342   char              **text                     // IN    ptr to text-array
343)
344{
345   char              **line  = NULL;
346
347   ENTER();
348
349   if (text != NULL)
350   {
351      for (line = text; *line != NULL; line++)
352      {
353         TxFreeMem( *line);
354      }
355      TxFreeMem( text);
356   }
357   VRETURN();
358}                                               // end 'samvDiscardText'
359/*---------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the browser.