/[svn]/linuxsampler/trunk/src/db/InstrumentsDb.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/db/InstrumentsDb.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3090 by schoenebeck, Thu Dec 8 12:36:56 2016 UTC revision 3091 by schoenebeck, Mon Jan 16 15:01:21 2017 UTC
# Line 41  namespace LinuxSampler { Line 41  namespace LinuxSampler {
41      InstrumentsDb InstrumentsDb::instance;      InstrumentsDb InstrumentsDb::instance;
42    
43      void InstrumentsDb::CreateInstrumentsDb(String FilePath) {      void InstrumentsDb::CreateInstrumentsDb(String FilePath) {
44            if (FilePath.empty()) {
45                FilePath = GetDefaultDBLocation();
46                dmsg(0,("InstrumentsDb: Creating database at default location '%s'\n", FilePath.c_str()));
47            }
48    
49          File f = File(FilePath);          File f = File(FilePath);
50          if (f.Exist()) {          if (f.Exist()) {
51              throw Exception("File exists: " + FilePath);              throw Exception("File exists: " + FilePath);
52          }          }
53            
54          GetInstrumentsDb()->SetDbFile(FilePath);          SetDbFile(FilePath);
55    
56          String sql =          String sql =
57              "  CREATE TABLE instr_dirs (                                      "              "  CREATE TABLE instr_dirs (                                      "
# Line 60  namespace LinuxSampler { Line 65  namespace LinuxSampler {
65              "      UNIQUE (parent_dir_id,dir_name)                            "              "      UNIQUE (parent_dir_id,dir_name)                            "
66              "  );                                                             ";              "  );                                                             ";
67                    
68          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
69    
70          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";
71          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
72    
73          sql =          sql =
74              "  CREATE TABLE instruments (                                "              "  CREATE TABLE instruments (                                "
# Line 86  namespace LinuxSampler { Line 91  namespace LinuxSampler {
91              "      UNIQUE (dir_id,instr_name)                            "              "      UNIQUE (dir_id,instr_name)                            "
92              "  );                                                        ";              "  );                                                        ";
93                    
94          GetInstrumentsDb()->ExecSql(sql);          ExecSql(sql);
95      }      }
96    
97      InstrumentsDb::InstrumentsDb() {      InstrumentsDb::InstrumentsDb() {
# Line 118  namespace LinuxSampler { Line 123  namespace LinuxSampler {
123          DbFile = File;          DbFile = File;
124      }      }
125    
126      sqlite3* InstrumentsDb::GetDb() {      String InstrumentsDb::GetDefaultDBLocation() {
127          if ( db != NULL) return db;          #ifdef WIN32
128            char* userprofile = getenv("USERPROFILE");
129            if (userprofile) {
130                String s = userprofile;
131                s += "\\.linuxsampler\\instruments.db";
132                return s;
133            } else {
134                // in case USERPROFILE is not set (which should not occur)
135                return "instruments.db";
136            }
137            #else // POSIX ...
138            String s = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;
139            # if defined(__APPLE__)
140            if (s.find("~") == 0)
141                s.replace(0, 1, getenv("HOME"));
142            # endif
143            return s;
144            #endif
145        }
146    
147          if (DbFile.empty()) {      void InstrumentsDb::EnsureDBFileExists() {
148                      #ifndef WIN32          if (DbFile.empty())
149                      DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;              DbFile = GetDefaultDBLocation();
                         #else  
                         char *userprofile = getenv("USERPROFILE");  
                         if(userprofile) {  
                             String DbPath = userprofile;  
                                 DbPath += "\\.linuxsampler";  
                             DbFile = DbPath + "\\instruments.db";  
                                 File InstrumentsDbFile(DbFile);  
                                 // if no DB exists create the subdir and then the DB  
                                 if( !InstrumentsDbFile.Exist() ) {  
                                     _mkdir( DbPath.c_str() );  
                                         // formats the DB, which creates a new instruments.db file  
                                         Format();  
                                 }  
                     }  
                         else {  
                             // in case USERPROFILE is not set (which should not occur)  
                             DbFile = "instruments.db";  
                         }  
                         #endif  
             }  
150                  #if defined(__APPLE__)  /* 20071224 Toshi Nagata  */                  #if defined(__APPLE__)  /* 20071224 Toshi Nagata  */
151                  if (DbFile.find("~") == 0)                  if (DbFile.find("~") == 0)
152                          DbFile.replace(0, 1, getenv("HOME"));                          DbFile.replace(0, 1, getenv("HOME"));
153                  #endif                  #endif
154            Path DbPath(DbFile);
155            String DbDir = DbPath.stripLastName();
156            // create directory if it does not exist yet
157            if (!DbPath.nodes().empty()) {
158                File d(DbDir);
159                if (!d.Exist()) {
160                    #ifdef WIN32
161                    if (_mkdir(DbDir.c_str()))
162                        throw Exception("Could not create instruments DB directory '" + DbDir + "'");
163                    #else
164                    if (mkdir(DbDir.c_str(), S_IRWXU))
165                        throw Exception("Could not create instruments DB directory '" + DbDir + "'");
166                    #endif
167                }
168            }
169            // create database file if it does not exist yet
170            File f(DbFile);
171            if (!f.Exist()) {
172                // formats the DB, which creates a new instruments.db file
173                Format();
174            }
175        }
176    
177        sqlite3* InstrumentsDb::GetDb() {
178            if ( db != NULL) return db;
179    
180            if (DbFile.empty())
181                DbFile = GetDefaultDBLocation();
182    
183            {
184                // first check if the instruments DB's directory exists, if not give up
185                Path path(DbFile);
186                String sDir = path.stripLastName();
187                File d(sDir);
188                if (!d.Exist())
189                    throw Exception("Instruments DB directory '" + sDir + "' does not exist!");
190    
191                // just to give the user a notice about the DB file being created in case it does not exist yet
192                File f(DbFile);
193                if (!f.Exist())
194                    dmsg(0,("Instruments DB file '%s' does not exist yet. Trying to create it now.\n", DbFile.c_str()));
195            }
196    
197            dmsg(0,("Opening instruments DB at '%s'\n", DbFile.c_str()));
198          int rc = sqlite3_open(DbFile.c_str(), &db);          int rc = sqlite3_open(DbFile.c_str(), &db);
199          if (rc) {          if (rc) {
200              sqlite3_close(db);              sqlite3_close(db);
# Line 1730  namespace LinuxSampler { Line 1777  namespace LinuxSampler {
1777                  db = NULL;                  db = NULL;
1778              }              }
1779    
1780              if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;              if (DbFile.empty()) DbFile = GetDefaultDBLocation();
1781              String bkp = DbFile + ".bkp";              String bkp = DbFile + ".bkp";
1782              remove(bkp.c_str());              remove(bkp.c_str());
1783              if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) {              if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) {

Legend:
Removed from v.3090  
changed lines
  Added in v.3091

  ViewVC Help
Powered by ViewVC