1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/****************************************************************************/
/* */
/* ./ascii/prefs.c - Set, Load and Save preferences */
/* */
/* This file is part of gpstrans - a program to communicate with garmin gps */
/* */
/* */
/* Copyright (c) 1995 by Carsten Tschach (tschach@zedat.fu-berlin.de) */
/* */
/* Permission to use, copy, modify, and distribute this software and its */
/* documentation for non-commercial purpose, is hereby granted without fee, */
/* providing that the copyright notice appear in all copies and that both */
/* the copyright notice and this permission notice appear in supporting */
/* documentation. I make no representations about the suitability of this */
/* software for any purpose. It is provides "as is" without express or */
/* implid warranty. */
/* */
/****************************************************************************/
#include "defs.h"
#include "Garmin.h"
#include "Prefs.h"
#include "getline.h"
#include <pwd.h>
#include <sys/time.h>
/* define external variables */
extern struct DATUM const gDatum[]; /* Variables from gps/datum.c */
extern struct ELLIPSOID const gEllipsoid[];
extern short nDatums;
extern char gMessageStr[];
/* define global variables */
char prop_scratch[255];
struct PREFS gPrefs;
/* prototype functions */
void SetEllipsoid(int n);
/****************************************************************************/
/* Expend a filename to absolute path. */
/****************************************************************************/
char *AbsolutFilename(ch)
char *ch;
{
char temp[255], temp1[255];
char *search;
struct passwd *pwdentry;
if (ch[0] != '~') {
sprintf(prop_scratch, "%s", ch);
return(&prop_scratch[0]);
}
sprintf(temp, "%s", ch);
if (ch[1] == '/') {
sprintf(temp1, "%s%s", getenv("HOME"), &temp[1]);
sprintf(temp, "%s", temp1);
} else {
sprintf(temp1, "%s", temp);
search = strchr(temp1, '/');
*search = (char) 0;
pwdentry = getpwnam(&temp1[1]);
search = strchr(temp, '/');
if (pwdentry != NULL) {
sprintf(temp1, "%s%s", pwdentry->pw_dir, search);
sprintf(temp, "%s", temp1);
} else {
temp[0] = (char) 0;
}
}
sprintf(prop_scratch, "%s", temp);
return (&prop_scratch[0]);
}
/****************************************************************************/
/* Analyzes a dataline and separate keyword and data. */
/****************************************************************************/
int AnalyzeLine(line, key, data)
char line[255], key[255], data[255];
{
int i;
if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = '\0';
if (line[0] == '#') return (1 == 2);
if (strlen(line) < 5) return (1 == 2);
i = 0;
while ((i <= strlen(line)) && (line[i] != ':'))
i++;
if (i >= strlen(line)) return (1 == 2);
sprintf(key, "%s", line);
key[i] = '\0';
while (key[0] == ' ') sprintf(key, "%s", &key[1]);
while (key[0] == '\t') sprintf(key, "%s", &key[1]);
if (strlen(key) != 4) return (1 == 2);
sprintf(data, "%s", &line[i + 2]);
return (1 == 1);
}
/****************************************************************************/
/* Save preferences in Preference-File defined in defs.h. */
/****************************************************************************/
void SavePrefs()
{
FILE *preffp;
preffp = fopen(AbsolutFilename(PrefFileName), "wt");
if (preffp == (FILE *) 0) {
sprintf( gMessageStr, "Can't write preferences in file '%s'",
AbsolutFilename(PrefFileName));
Message( gMessageStr );
} else {
fprintf(preffp, "# Properties file of gpstrans - do not edit this file\n");
fprintf(preffp, "FMAT: %d\n", gPrefs.format);
fprintf(preffp, "OFFS: %1.2f\n", gPrefs.offset);
fprintf(preffp, "DATU: %d\n", gPrefs.datum);
fprintf(preffp, "DEVI: %s\n", gPrefs.Device);
fclose(preffp);
}
}
/****************************************************************************/
/* Load preferences from Preference-File - set default if not exist. */
/****************************************************************************/
void LoadPrefs()
{
char Line[255], Keyword[255], Data[255];
int value;
float floatval;
FILE *preffp;
gPrefs.format = DMS;
gPrefs.offset = +0.0;
gPrefs.datum = 100;
sprintf(gPrefs.Device, "%s", DefaultServerDevice);
preffp = fopen(AbsolutFilename(PrefFileName), "rt");
if (preffp != (FILE *) 0) {
fgets(Line, 255, preffp);
while (!feof(preffp)) {
if (AnalyzeLine(Line, Keyword, Data)) {
if (!strcmp(Keyword, "FMAT")) {
sscanf(Data, "%d", &value);
gPrefs.format = value;
} else
if (!strcmp(Keyword, "OFFS")) {
sscanf(Data, "%f", &floatval);
gPrefs.offset = (double) floatval;
} else
if (!strcmp(Keyword, "DATU")) {
sscanf(Data, "%d", &value);
gPrefs.datum = value;
} else
if (!strcmp(Keyword, "DEVI")) {
sprintf(gPrefs.Device, "%s", Data);
}
}
fgets(Line, 255, preffp);
}
fclose(preffp);
}
}
/****************************************************************************/
/* Init preferences. */
/****************************************************************************/
void InitPrefs()
{
LoadPrefs();
}
/****************************************************************************/
/* Wait for RETURN when called. */
/****************************************************************************/
void WaitforReturn()
{
printf("Press RETURN for more ");
fflush(stdout);
getchar();
}
/****************************************************************************/
/* Setup preferences - datum, format, time offset and serial device. */
/****************************************************************************/
void SetupProgram()
{
int value;
short v;
float floatval;
char *p;
for (v = 0; v <= nDatums; v++) {
printf("%3d - %-25s ", v, gDatum[v].name);
if (((v + 1) % 2) == 0) printf("\n");
if ((v == 39) || (v == 85)) WaitforReturn();
}
printf("\n");
do {
p = getline("Please select datum: ");
sscanf(p, "%d", &value);
printf("\n");
} while ((value < 0) || (value > nDatums) || (p[0] < '0') || (p[0] > '9'));
gPrefs.datum = value;
printf("\n\n");
printf(" 0 - lat/lon dddmm'ss.s\"\n");
printf(" 1 - lat/lon dddmm.mmm'\n");
printf(" 2 - lat/lon ddd.ddddd\n");
printf(" 3 - utm/ups Grid\n");
printf(" 4 - British Grid\n");
printf(" 5 - Irish Grid\n");
printf(" 6 - Finnish Grid \n\n");
do {
p = getline("Please select format: ");
sscanf(p, "%d", &value);
printf("\n");
} while ((value < 0) || (value > 6) || (p[0] < '0') || (p[0] > '9'));
gPrefs.format = value;
printf("\n");
do {
p = getline("Please select time offset (-12 to +12): ");
sscanf(p, "%f", &floatval);
printf("\n");
} while ((floatval < -12.0) || (floatval > 12.0));
gPrefs.offset = (double)floatval;
printf("\n");
do {
p = getline("Please enter serial device name: ");
sprintf(gPrefs.Device, "%s", p);
printf("\n");
} while (strlen(gPrefs.Device) == 0);
printf("\n\n");
printf("Preferences have been saved to ~/.gpstrans and will be used in the");
printf(" future.\n");
SavePrefs();
exit(0);
}
|