aboutsummaryrefslogtreecommitdiffstats
path: root/dl_write_save.c
blob: feeea8619e98baf1425b2312dd4e87503c1e26b7 (plain)
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
/* 
 * Copyright 1996-2002 - Karl R. Hakimian and David Fries
 *
 * This file is part of datalink.
 *
 * Datalink is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Datalink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with datalink (see COPYING); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <unistd.h>

#include "datalink.h"
#include "datalink_private.h"

int dl_write_save(char *datafile, char *wristappfile, char *melodyfile)
{
	FILE *fp;
	char bakfile[1024];
	int last_warn = 0;
	TimePtr tip;
	AlarmPtr alp;
	AppointmentPtr ap;
	ToDoPtr tp;
	PhonePtr pp;
	AnniversaryPtr anp;
	SystemPtr sp;
	int hour, min;
	int i;

/* Create backup of old datafile. */

	sprintf(bakfile, "%s.bak", datafile);
	(void) unlink(bakfile);

	if (link(datafile, bakfile) < 0)
		last_warn = (*dl_warn_proc) ("Could not make backup.");

	(void) unlink(datafile);

	if ((fp = fopen(datafile, "w")) == NULL)
		return ((*dl_error_proc) ("Could not write save file."));

	fprintf(fp,
		"# Data Link save file created by the datalink library.\n\n");

/* Save each type of data. */

	for (i = 0; i < dl_download_data.num_times; i++)
	{
		tip = &dl_download_data.times[i];
		fprintf(fp, "timezone = %d, \"%s\", %d, %d, %d\n",
			tip->tz_num, tip->label, tip->offset,
			tip->hour_fmt, tip->date_fmt);
	}

	if (dl_download_data.num_times)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_alarms; i++)
	{
		alp = &dl_download_data.alarms[i];
		fprintf(fp,
			"alarm = %d, %02d/%02d, %02d:%02d, \"%s\", %d\n",
			alp->alarm_num, alp->month, alp->day, alp->hours,
			alp->minutes, alp->label, alp->audible);
	}

	if (dl_download_data.num_alarms)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_apps; i++)
	{
		ap = &dl_download_data.apps[i];
		min = 0;

		for (i = 1; i < 0xFF; i <<= 1)
		{

			if ((i & ap->time))
				min += i * 15;

		}

		hour = min / 60;
		min = hour * 60;

		fprintf(fp, "appointment = %02d/%02d, %02d:%02d, \"%s\"\n",
			ap->month, ap->day, hour, min, ap->label);
	}

	if (dl_download_data.num_apps)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_todos; i++)
	{
		tp = &dl_download_data.todos[i];
		fprintf(fp, "todo = %d, \"%s\"\n", tp->priority,
			tp->label);
	}

	if (dl_download_data.num_todos)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_phones; i++)
	{
		pp = &dl_download_data.phones[i];
		fprintf(fp, "phone = \"%s\", \"%s\"\n", pp->number,
			pp->label);
	}

	if (dl_download_data.num_phones)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_annivs; i++)
	{
		anp = &dl_download_data.annivs[i];
		fprintf(fp, "anniversary = %02d/%02d, \"%s\"\n",
			anp->month, anp->day, anp->label);
	}

	if (dl_download_data.num_annivs)
		fprintf(fp, "\n");

	for (i = 0; i < dl_download_data.num_system; i++)
	{
		sp = &dl_download_data.system[i];
		fprintf(fp, "system = %d, %d\n", sp->chime, sp->beep);
	}

	if (dl_download_data.num_system)
		fprintf(fp, "\n");

	if (wristappfile && *wristappfile)
		fprintf(fp, "wristapp = \"%s\"\n\n", wristappfile);

	if (melodyfile && *melodyfile)
		fprintf(fp, "melody = \"%s\"\n\n", melodyfile);

	return (last_warn);
}