1 /****************************************************************
4 * Date: November 1, 2000
5 * Author: Edward H. Flora <ehflora@access1.net>
7 * This file is a part of the wmcalc application. As such, this
8 * file is licensed under the GNU General Public License, version 2.
9 * A copy of this license may be found in the file COPYING that should
10 * have been distributed with this file. If not, please refer to
11 * http://www.gnu.org/copyleft/gpl.html for details.
13 ****************************************************************
15 This file contains system level functions, such as read/write of
16 the config file, character map boundaries, etc.
20 11/1/00 Updated Function headers, cleaned up comments some.
22 ****************************************************************/
28 #include "wmcalc_err.h"
32 /****************************************************************
34 ****************************************************************
36 This function determines which button was pressed, and performs
37 the appropriate function.
41 11/01/00 Function header updated
42 ****************************************************************/
43 void ExecFunc( int val
) {
46 if (Verbose
) printf("Execute function for button %d\n", val
);
251 } /* End of switch statement */
253 } /* End of function ExecFunc() *********************************/
255 /****************************************************************
256 * Function: getboundaries
257 ****************************************************************
259 This function returns the x,y boundaries for each character
260 that is to be displayed on the display.
262 There must be a better way to do this, as by changing the file
263 charmap.xpm, one may have to adjust these constants.
267 11/01/00 Function header updated
268 10/30/00 Added characters for the Memory indicator bar.
269 ****************************************************************/
270 ButtonArea
getboundaries(char ch
) {
275 xybounds
.x
= 1; xybounds
.i
= 6;
276 xybounds
.y
= 1; xybounds
.j
= 8;
279 xybounds
.x
= 7; xybounds
.i
= 12;
280 xybounds
.y
= 1; xybounds
.j
= 8;
283 xybounds
.x
= 13; xybounds
.i
= 18;
284 xybounds
.y
= 1; xybounds
.j
= 8;
287 xybounds
.x
= 19; xybounds
.i
= 24;
288 xybounds
.y
= 1; xybounds
.j
= 8;
291 xybounds
.x
= 25; xybounds
.i
= 30;
292 xybounds
.y
= 1; xybounds
.j
= 8;
295 xybounds
.x
= 31; xybounds
.i
= 36;
296 xybounds
.y
= 1; xybounds
.j
= 8;
299 xybounds
.x
= 37; xybounds
.i
= 42;
300 xybounds
.y
= 1; xybounds
.j
= 8;
303 xybounds
.x
= 43; xybounds
.i
= 48;
304 xybounds
.y
= 1; xybounds
.j
= 8;
307 xybounds
.x
= 49; xybounds
.i
= 54;
308 xybounds
.y
= 1; xybounds
.j
= 8;
311 xybounds
.x
= 55; xybounds
.i
= 60;
312 xybounds
.y
= 1; xybounds
.j
= 8;
315 xybounds
.x
= 55; xybounds
.i
= 60;
316 xybounds
.y
= 28; xybounds
.j
= 35;
319 xybounds
.x
= 49; xybounds
.i
= 54;
320 xybounds
.y
= 28; xybounds
.j
= 35;
323 xybounds
.x
= 7; xybounds
.i
= 12;
324 xybounds
.y
= 10; xybounds
.j
= 17;
327 xybounds
.x
= 1; xybounds
.i
= 6;
328 xybounds
.y
= 37; xybounds
.j
= 44;
331 xybounds
.x
= 7; xybounds
.i
= 12;
332 xybounds
.y
= 37; xybounds
.j
= 44;
335 xybounds
.x
= 13; xybounds
.i
= 18;
336 xybounds
.y
= 37; xybounds
.j
= 44;
339 xybounds
.x
= 19; xybounds
.i
= 24;
340 xybounds
.y
= 37; xybounds
.j
= 44;
343 xybounds
.x
= 25; xybounds
.i
= 30;
344 xybounds
.y
= 37; xybounds
.j
= 44;
347 xybounds
.x
= 31; xybounds
.i
= 36;
348 xybounds
.y
= 37; xybounds
.j
= 44;
351 xybounds
.x
= 1; xybounds
.i
= 6;
352 xybounds
.y
= 10; xybounds
.j
= 17;
355 xybounds
.x
= 55; xybounds
.i
= 60;
356 xybounds
.y
= 55; xybounds
.j
= 56;
359 xybounds
.x
= 55; xybounds
.i
= 60;
360 xybounds
.y
= 57; xybounds
.j
= 58;
363 xybounds
.x
= 55; xybounds
.i
= 60;
364 xybounds
.y
= 59; xybounds
.j
= 60;
367 xybounds
.x
= 1; xybounds
.i
= 6;
368 xybounds
.y
= 10; xybounds
.j
= 17;
370 } /* end of switch statement */
373 } /***** End of function getboundaries() ************************/
375 /****************************************************************
376 * Function: write_config
377 ****************************************************************
379 This function updates the configuration file as memory locations
380 are updated in the program. It re-writes the entire file, but
381 should ignore all lines that do not start with "Mem".
385 11/01/00 Function Header updated
386 11/05/00 Added Locked Memory Handling
387 ****************************************************************/
388 int write_config(void) {
390 extern double MemArray
[];
391 extern char configfile
[];
392 extern char tempfile
[];
393 extern char *CfgVarList
[];
398 char movefilecmd
[2 * CONFIGFILEMAX
+ 10]; /* make sure enough room in string */
400 /* Open current Config file */
401 if ((fp
= fopen(configfile
, "r")) == NULL
) { /* Can't find config file */
402 printf("%s: Cannot create configuration file\n", configfile
);
403 return(ERR_FILE_NOT_FOUND
);
406 /* We cannot write to the global config-file... */
407 if(!strcmp(configfile
, CONFIGGLOBAL
)) {
408 strcpy(configfile
, getenv("HOME")); /* Added to wmbutton by Gordon Fraser, 9/21/01 */
409 strcat(configfile
, CONFFILENAME
);
412 /* Open Temporary File */
413 if ((fptmp
= fopen(tempfile
, "w")) == NULL
) { /* Can't open file in /tmp */
414 fprintf(stderr
, "%s: Temporary File Open Failed\n", tempfile
);
415 strcpy(tempfile
, getenv("HOME"));
416 strcat(tempfile
, "wmcalc.tmp");
417 if ((fptmp
= fopen(tempfile
, "w")) == NULL
) { /* Can't open file in HOME */
418 fprintf(stderr
, "%s: Temporary File Open Failed\n", tempfile
);
419 return(ERR_TMP_FILE_FAILED
);
423 while ((line
= readln(fp
)) != NULL
) { /* Read Lines in config file */
424 if (Verbose
) printf("line:%s", line
);
426 if ((strncmp(line
, CfgVarList
[MEM_LABEL_0
],
427 strlen(CfgVarList
[MEM_LABEL_0
]) - 1) == 0)) {
428 /* -1 to generalize to all Mem? strings */
429 /* If we've found a memory entry */
430 mem_ndx
= atoi(line
+strlen(CfgVarList
[MEM_LABEL_0
])-1);
431 if ((mem_ndx
>= 0) && (mem_ndx
<= (NUM_MEM_CELLS
- 1))) {
432 fprintf(fptmp
, "%s\t%f\n", CfgVarList
[mem_ndx
],
437 fprintf(fptmp
, "%s", line
);
440 } /* End of while loop */
442 /* Close open files */
446 /* Copy temp file over original */
447 /* Note: If changing command, make sure to adjust size of string above!! */
448 sprintf(movefilecmd
, "mv -f %s %s\n", tempfile
, configfile
);
449 err_code
= system(movefilecmd
);
451 if(Verbose
) printf("New config file written.\n");
454 } /***** End of function write_config() *************************/
457 /****************************************************************
458 * Function: read_config
459 ****************************************************************
461 This function reads the configuration file on program startup,
462 and sets the appropriate configuration options.
463 (By default, this is ~/.wmcalc, or a user set value)
467 11/01/00 Function header updated.
468 11/05/00 Added Lcoked Memory Capabilities
469 ****************************************************************/
470 int read_config(void) {
472 extern double MemArray
[];
473 extern int MemLock
[];
474 extern char *CfgVarList
[];
475 extern char ImagChar
;
476 extern char configfile
[];
477 extern char SysCalcCmd
[];
483 char *cfg_var_ptr
= NULL
;
486 if ((fp
= fopen(configfile
, "r")) == NULL
) { /* Can't find config file */
487 strcpy(configfile
, CONFIGGLOBAL
); /* ...so try to open global config */
488 if ((fp
= fopen(configfile
, "r")) == NULL
) { /* Can't find global config file */
489 fprintf(stderr
, "%s: Configuration File not found\n", configfile
);
490 return(ERR_FILE_NOT_FOUND
);
492 write_config(); /* if global config opened, save in homedir */
496 if (Verbose
) printf("%s: Found Configuration File\n", configfile
);
499 do { /* Read Lines in config file */
501 if (Verbose
) printf("Line Read:%s\n", line
);
502 if (line
== NULL
) break; /* if end of file, quit */
503 if ( (line
[0] != '#') && (line
[0] != '\n')) { /* Ignore comments and
505 if (strchr(line
, sep_ch
) != NULL
) { /* The line has a tab, so let's
506 see if the variable is
509 /* Loop while we haven't found the variable */
510 while (((strncmp(line
, CfgVarList
[i
], strlen(CfgVarList
[i
]))) != 0)
511 && (i
< MAX_LABEL
)) {
514 /* If we've found the variable, let's set the appropriate value */
515 if (i
<= MAX_LABEL
) {
516 if (Verbose
) printf("Variable %s found\n", CfgVarList
[i
]);
518 /* Point to the 'tab' character, to read the value */
519 cfg_var_ptr
= strchr(line
, sep_ch
);
520 cfg_var_ptr
++; /* ++ to avoid tab character itself */
522 /* Now set the appropriate variable */
534 /* Set Locked Flag */
536 /* Set Memory Element */
537 MemArray
[i
] = atof(cfg_var_ptr
);
539 printf("Assign Memory Element %d to %f\n", i
, MemArray
[i
]);
552 /* Set Locked Flag */
553 MemLock
[i
- MEM_LOCK_0
] = 1;
554 /* Set Memory Element */
555 MemArray
[i
- MEM_LOCK_0
] = atof(cfg_var_ptr
);
557 printf("Assign Memory Element %d to %f\n", i
-
558 MEM_LOCK_0
, MemArray
[i
- MEM_LOCK_0
]);
562 /* Get the character that the user wishes to use to
563 represent sqrt(-1) (i or j) */
564 if ((cfg_var_ptr
[0] == 'i') || cfg_var_ptr
[0] == 'j') {
565 ImagChar
= cfg_var_ptr
[0];
568 printf("Use character '%c' to represent sqrt(-1)\n", ImagChar
);
572 /* Set external calculator start command */
573 strcpy(SysCalcCmd
, cfg_var_ptr
);
575 printf("Assign Calc Command: %s\n", cfg_var_ptr
);
579 if (Verbose
) printf("Unknown Config Variable: %s\n", line
);
584 /* Otherwise, we don't recognize the variable */
586 if (Verbose
) printf("Unknown Config Variable: %s\n", line
);
589 /* Otherwise, we had a poorly formatted line in the config file. */
591 if (Verbose
) printf("Poorly formatted config file line\n:%s\n", line
);
595 } while (line
!= NULL
);
602 } /***** End of function read_config **************************/