Statistics
| Branch: | Tag: | Revision:

root / gnuradio-core / src / lib / runtime / gr_preferences.cc @ 9789365e

History | View | Annotate | Download (2.5 kB)

1 5d69a524 jcorgan
/* -*- c++ -*- */
2 5d69a524 jcorgan
/*
3 d65ae424 Josh Blum
 * Copyright 2003,2010,2011 Free Software Foundation, Inc.
4 5d69a524 jcorgan
 * 
5 5d69a524 jcorgan
 * This file is part of GNU Radio
6 5d69a524 jcorgan
 * 
7 5d69a524 jcorgan
 * GNU Radio is free software; you can redistribute it and/or modify
8 5d69a524 jcorgan
 * it under the terms of the GNU General Public License as published by
9 937b719d eb
 * the Free Software Foundation; either version 3, or (at your option)
10 5d69a524 jcorgan
 * any later version.
11 5d69a524 jcorgan
 * 
12 5d69a524 jcorgan
 * GNU Radio is distributed in the hope that it will be useful,
13 5d69a524 jcorgan
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 5d69a524 jcorgan
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 5d69a524 jcorgan
 * GNU General Public License for more details.
16 5d69a524 jcorgan
 * 
17 5d69a524 jcorgan
 * You should have received a copy of the GNU General Public License
18 5d69a524 jcorgan
 * along with GNU Radio; see the file COPYING.  If not, write to
19 86f5c924 eb
 * the Free Software Foundation, Inc., 51 Franklin Street,
20 86f5c924 eb
 * Boston, MA 02110-1301, USA.
21 5d69a524 jcorgan
 */
22 5d69a524 jcorgan
23 5d69a524 jcorgan
#ifdef HAVE_CONFIG_H
24 5d69a524 jcorgan
#include "config.h"
25 5d69a524 jcorgan
#endif
26 5d69a524 jcorgan
27 5d69a524 jcorgan
#include <gr_preferences.h>
28 d65ae424 Josh Blum
#include <gr_sys_paths.h>
29 5d69a524 jcorgan
#include <stdio.h>
30 5d69a524 jcorgan
#include <stdlib.h>
31 5d69a524 jcorgan
#include <string.h>
32 5d69a524 jcorgan
#include <sys/types.h>
33 5d69a524 jcorgan
#include <sys/stat.h>
34 5d69a524 jcorgan
#include <unistd.h>
35 38ea3a57 eb
#include <string.h>
36 5d69a524 jcorgan
37 05cc02ce Josh Blum
#include <boost/filesystem/operations.hpp>
38 05cc02ce Josh Blum
#include <boost/filesystem/path.hpp>
39 05cc02ce Josh Blum
namespace fs = boost::filesystem;
40 05cc02ce Josh Blum
41 5d69a524 jcorgan
/*
42 5d69a524 jcorgan
 * The simplest thing that could possibly work:
43 5d69a524 jcorgan
 *  the key is the filename; the value is the file contents.
44 5d69a524 jcorgan
 */
45 5d69a524 jcorgan
46 5d69a524 jcorgan
static const char *
47 5d69a524 jcorgan
pathname (const char *key)
48 5d69a524 jcorgan
{
49 05cc02ce Josh Blum
  static fs::path path;
50 d65ae424 Josh Blum
  path = fs::path(gr_appdata_path()) / ".gnuradio" / "prefs" / key;
51 05cc02ce Josh Blum
  return path.string().c_str();
52 5d69a524 jcorgan
}
53 5d69a524 jcorgan
54 5d69a524 jcorgan
static void
55 5d69a524 jcorgan
ensure_dir_path ()
56 5d69a524 jcorgan
{
57 d65ae424 Josh Blum
  fs::path path = fs::path(gr_appdata_path()) / ".gnuradio";
58 05cc02ce Josh Blum
  if (!fs::is_directory(path)) fs::create_directory(path);
59 5d69a524 jcorgan
60 05cc02ce Josh Blum
  path = path / "prefs";
61 05cc02ce Josh Blum
  if (!fs::is_directory(path)) fs::create_directory(path);
62 5d69a524 jcorgan
}
63 5d69a524 jcorgan
64 5d69a524 jcorgan
const char *
65 5d69a524 jcorgan
gr_preferences::get (const char *key)
66 5d69a524 jcorgan
{
67 5d69a524 jcorgan
  static char buf[1024];
68 5d69a524 jcorgan
69 5d69a524 jcorgan
  FILE         *fp = fopen (pathname (key), "r");
70 9eeb6dba Tom Rondeau
  if (fp == 0) {
71 9eeb6dba Tom Rondeau
    perror (pathname (key));
72 5d69a524 jcorgan
    return 0;
73 9eeb6dba Tom Rondeau
  }
74 5d69a524 jcorgan
75 5d69a524 jcorgan
  memset (buf, 0, sizeof (buf));
76 9eeb6dba Tom Rondeau
  size_t ret = fread (buf, 1, sizeof (buf) - 1, fp);
77 9eeb6dba Tom Rondeau
  if(ret == 0) {
78 9eeb6dba Tom Rondeau
    if(ferror(fp) != 0) {
79 9eeb6dba Tom Rondeau
      perror (pathname (key));
80 9eeb6dba Tom Rondeau
      fclose (fp);
81 9eeb6dba Tom Rondeau
      return 0;
82 9eeb6dba Tom Rondeau
    }
83 9eeb6dba Tom Rondeau
  }
84 5d69a524 jcorgan
  fclose (fp);
85 5d69a524 jcorgan
  return buf;
86 5d69a524 jcorgan
}
87 5d69a524 jcorgan
88 5d69a524 jcorgan
void 
89 5d69a524 jcorgan
gr_preferences::set (const char *key, const char *value)
90 5d69a524 jcorgan
{
91 5d69a524 jcorgan
  ensure_dir_path ();
92 5d69a524 jcorgan
93 5d69a524 jcorgan
  FILE *fp = fopen (pathname (key), "w");
94 5d69a524 jcorgan
  if (fp == 0){
95 5d69a524 jcorgan
    perror (pathname (key));
96 5d69a524 jcorgan
    return;
97 5d69a524 jcorgan
  }
98 5d69a524 jcorgan
99 9eeb6dba Tom Rondeau
  size_t ret = fwrite (value, 1, strlen (value), fp);
100 9eeb6dba Tom Rondeau
  if(ret == 0) {
101 9eeb6dba Tom Rondeau
    if(ferror(fp) != 0) {
102 9eeb6dba Tom Rondeau
      perror (pathname (key));
103 9eeb6dba Tom Rondeau
      fclose (fp);
104 9eeb6dba Tom Rondeau
      return;
105 9eeb6dba Tom Rondeau
    }
106 9eeb6dba Tom Rondeau
  }
107 5d69a524 jcorgan
  fclose (fp);
108 5d69a524 jcorgan
};