File: prefops.c

package info (click to toggle)
denemo 0.5.9-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,500 kB
  • ctags: 2,415
  • sloc: ansic: 23,057; sh: 3,321; yacc: 1,737; makefile: 449; lex: 376
file content (197 lines) | stat: -rw-r--r-- 5,437 bytes parent folder | download
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
/* prefops.c
 * Functions for initializing preferences
 *
 * for Denemo, a gtk+ frontend to GNU Lilypond
 * (c) 1999, 2000, 2001 Matthew Hiller
 */

#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "config.h"
#include "datastructures.h"

enum scannertoks
{
  IMMEDIATEPLAYBACK = G_TOKEN_LAST,
  LILYPATH,
  MIDIPLAYER,
  PLAYBACKOUTPUT
};

/* This checks to see if there's a .denemo/ directory in the user's
 * home directory, tries to create one if there isn't, and returns the
 * path to it
 *
 * .denemo/ is used for holding configuration files, templates, and so on.
 *
 * I suspect that this may have some Windows portability issues,
 * but I'm not sure on this. */

gchar *
locatedotdenemo ()
{
  static GString *dotdenemo = NULL;
  DIR *dir;

  if (!dotdenemo)
    {
      dotdenemo = g_string_new (getenv ("HOME"));
      g_string_append (dotdenemo, "/.denemo");
    }
  if (dir = opendir (dotdenemo->str))
    closedir (dir);
  else
    {
      if (errno == ENOTDIR || errno == ENOENT)
	{
	  if (errno == ENOTDIR)
	    if (unlink (dotdenemo->str) == -1)
	      g_warning (_("locatedotdenemo : error unlinking %s : %s"),
			 dotdenemo->str, g_strerror (errno));
#ifdef G_OS_WIN32
	  if (mkdir (dotdenemo->str) == -1)
#else
	  if (mkdir (dotdenemo->str, 0770) == -1)
#endif
	    g_warning (_("locatedotdenemo : error making directory %s : %s"),
		       dotdenemo->str, g_strerror (errno));
	}
      else
	g_warning (_("locatedotdenemo : error opening directory %s : %s"),
		   dotdenemo->str, g_strerror (errno));
    }
  return dotdenemo->str;
}

static GScannerConfig scanner_config_template = {
  (" \t\r\n") /* cset_skip_characters */ ,
  (G_CSET_a_2_z "_/" G_CSET_A_2_Z) /* cset_identifier_first */ ,
  (G_CSET_a_2_z
   "_0123456789/"
   G_CSET_A_2_Z G_CSET_LATINS G_CSET_LATINC) /* cset_identifier_nth */ ,
  ("#\n") /* cpair_comment_single */ ,

  FALSE /* case_sensitive */ ,

  TRUE /* skip_comment_multi */ ,
  TRUE /* skip_comment_single */ ,
  TRUE /* scan_comment_multi */ ,
  TRUE /* scan_identifier */ ,
  FALSE /* scan_identifier_1char */ ,
  FALSE /* scan_identifier_NULL */ ,
  TRUE /* scan_symbols */ ,
  FALSE /* scan_binary */ ,
  TRUE /* scan_octal */ ,
  TRUE /* scan_float */ ,
  TRUE /* scan_hex */ ,
  FALSE /* scan_hex_dollar */ ,
  TRUE /* scan_string_sq */ ,
  TRUE /* scan_string_dq */ ,
  TRUE /* numbers_2_int */ ,
  FALSE /* int_2_float */ ,
  FALSE /* identifier_2_string */ ,
  TRUE /* char_2_token */ ,
  TRUE /* symbol_2_token */ ,
  FALSE /* scope_0_fallback */ ,
};

/* Actually read through a file looking for configuration settings
 * and save them to prefs */

void
readpreffile (gchar * filename, struct prefinfo *prefs)
{
  gint fd;
  gint result;
  GScanner *scanner;

  if ((fd = open (filename, O_RDONLY)) == -1)
    g_warning (_("readpreffile : error opening %s : %s"), filename,
	       g_strerror (errno));
  else
    {
      scanner = g_scanner_new (&scanner_config_template);
      g_scanner_freeze_symbol_table (scanner);
      g_scanner_add_symbol (scanner, "immediateplayback",
			    GINT_TO_POINTER (IMMEDIATEPLAYBACK));
      g_scanner_add_symbol (scanner, "lilypath", GINT_TO_POINTER (LILYPATH));
      g_scanner_add_symbol (scanner, "midiplayer",
			    GINT_TO_POINTER (MIDIPLAYER));
      g_scanner_add_symbol (scanner, "playbackoutput",
			    GINT_TO_POINTER (PLAYBACKOUTPUT));
      g_scanner_thaw_symbol_table (scanner);
      g_scanner_input_file (scanner, fd);
      while ((result = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
	{
	  switch (result)
	    {
	    case IMMEDIATEPLAYBACK:
	      if (g_scanner_get_next_token (scanner) == G_TOKEN_EQUAL_SIGN
		  && g_scanner_get_next_token (scanner) == G_TOKEN_IDENTIFIER)
		{
		  prefs->immediateplayback =
		    (g_strcasecmp (scanner->value.v_identifier, "true") == 0);
		}
	    case LILYPATH:
	      if (g_scanner_get_next_token (scanner) == G_TOKEN_EQUAL_SIGN
		  && g_scanner_get_next_token (scanner) == G_TOKEN_IDENTIFIER)
		{
		  g_string_assign (prefs->lilypath,
				   scanner->value.v_identifier);
		}
	      break;
	    case MIDIPLAYER:
	      if (g_scanner_get_next_token (scanner) == G_TOKEN_EQUAL_SIGN
		  && g_scanner_get_next_token (scanner) == G_TOKEN_IDENTIFIER)
		{
		  g_string_assign (prefs->midiplayer,
				   scanner->value.v_identifier);
		}
	      break;
	    case PLAYBACKOUTPUT:
	      if (g_scanner_get_next_token (scanner) == G_TOKEN_EQUAL_SIGN
		  && g_scanner_get_next_token (scanner) == G_TOKEN_IDENTIFIER)
		{
		  prefs->playbackoutput =
		    (g_strcasecmp (scanner->value.v_identifier, "true") == 0);
		}
	      break;
	    }
	}
      close (fd);
      g_scanner_destroy (scanner);
    }
}

struct prefinfo *
initprefs ()
{
  gchar *systemwide = PKGDATADIR "denemo.conf";
  struct prefinfo *ret = g_malloc (sizeof (struct prefinfo));
  gchar *localrc = g_strconcat (locatedotdenemo (), "/denemorc", NULL);

  /* Reasonable default values */

  ret->lilypath = g_string_new ("lilypond");
  ret->midiplayer = g_string_new ("playmidi");
  ret->immediateplayback = TRUE;
  ret->playbackoutput = FALSE;
  ret->lilyentrystyle = FALSE;
  ret->createclones = FALSE;

  /* Read values from systemwide preferences file */

  readpreffile (systemwide, ret);

  /* Read values from personal preferences file */

  readpreffile (localrc, ret);

  g_free (localrc);
  return ret;
}