/[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 1345 by iliev, Thu Sep 13 21:46:25 2007 UTC revision 1350 by iliev, Sun Sep 16 23:06:10 2007 UTC
# Line 58  namespace LinuxSampler { Line 58  namespace LinuxSampler {
58                    
59          GetInstrumentsDb()->ExecSql(sql);          GetInstrumentsDb()->ExecSql(sql);
60    
61          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, 0, '/');";          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";
62          GetInstrumentsDb()->ExecSql(sql);          GetInstrumentsDb()->ExecSql(sql);
63    
64          sql =          sql =
# Line 136  namespace LinuxSampler { Line 136  namespace LinuxSampler {
136          }          }
137          rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL);          rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL);
138          if (rc) { throw Exception("Failed to add user function for handling regular expressions."); }          if (rc) { throw Exception("Failed to add user function for handling regular expressions."); }
139    
140            // TODO: remove this in the next version
141            try {
142                int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0");
143                // The parent ID of the root directory should be -2 now.
144                if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0");
145            } catch(Exception e) { }
146            ////////////////////////////////////////
147                    
148          return db;          return db;
149      }      }
# Line 149  namespace LinuxSampler { Line 157  namespace LinuxSampler {
157                    
158          int count = ExecSqlInt(sql.str());          int count = ExecSqlInt(sql.str());
159    
         // While the root dir has ID 0 and parent ID 0, the directory  
         // count for the root dir will be incorrect, so we should fix it.  
         if (count != -1 && DirId == 0) count--;  
160          return count;          return count;
161      }      }
162    
# Line 496  namespace LinuxSampler { Line 501  namespace LinuxSampler {
501          }          }
502    
503          EndTransaction();          EndTransaction();
504          FireDirectoryNameChanged(Dir, Name);          FireDirectoryNameChanged(Dir, toAbstractName(Name));
505      }      }
506    
507      void InstrumentsDb::MoveDirectory(String Dir, String Dst) {      void InstrumentsDb::MoveDirectory(String Dir, String Dst) {
# Line 808  namespace LinuxSampler { Line 813  namespace LinuxSampler {
813                  sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId;                  sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId;
814    
815                  pInstrs = ExecSqlStringList(sql.str());                  pInstrs = ExecSqlStringList(sql.str());
816                    // Converting to abstract names
817                    for (int i = 0; i < pInstrs->size(); i++) {
818                        for (int j = 0; j < pInstrs->at(i).length(); j++) {
819                            if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0';
820                        }
821                    }
822              }              }
823              EndTransaction();              EndTransaction();
824              return pInstrs;              return pInstrs;
# Line 968  namespace LinuxSampler { Line 979  namespace LinuxSampler {
979              throw e;              throw e;
980          }          }
981          EndTransaction();          EndTransaction();
982          FireInstrumentNameChanged(Instr, Name);          FireInstrumentNameChanged(Instr, toAbstractName(Name));
983      }      }
984    
985      void InstrumentsDb::MoveInstrument(String Instr, String Dst) {      void InstrumentsDb::MoveInstrument(String Instr, String Dst) {
# Line 1036  namespace LinuxSampler { Line 1047  namespace LinuxSampler {
1047                  return;                  return;
1048              }              }
1049    
             if (GetInstrumentId(dstId, instrName) != -1) {  
                 String s = toEscapedPath(instrName);  
                 throw Exception("Cannot copy. Instrument with that name already exists: " + s);  
             }  
   
             if (GetDirectoryId(dstId, instrName) != -1) {  
                 String s = toEscapedPath(instrName);  
                 throw Exception("Cannot copy. Directory with that name already exists: " + s);  
             }  
   
1050              CopyInstrument(instrId, instrName, dstId, Dst);              CopyInstrument(instrId, instrName, dstId, Dst);
1051          } catch (Exception e) {          } catch (Exception e) {
1052              EndTransaction();              EndTransaction();
# Line 1056  namespace LinuxSampler { Line 1057  namespace LinuxSampler {
1057      }      }
1058    
1059      void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) {      void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) {
1060            if (GetInstrumentId(DstDirId, InstrName) != -1) {
1061                String s = toEscapedPath(InstrName);
1062                throw Exception("Cannot copy. Instrument with that name already exists: " + s);
1063            }
1064    
1065            if (GetDirectoryId(DstDirId, InstrName) != -1) {
1066                String s = toEscapedPath(InstrName);
1067                throw Exception("Cannot copy. Directory with that name already exists: " + s);
1068            }
1069    
1070          DbInstrument i = GetInstrumentInfo(InstrId);          DbInstrument i = GetInstrumentInfo(InstrId);
1071          sqlite3_stmt *pStmt = NULL;          sqlite3_stmt *pStmt = NULL;
1072          std::stringstream sql;          std::stringstream sql;
# Line 1069  namespace LinuxSampler { Line 1080  namespace LinuxSampler {
1080              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1081          }          }
1082    
1083          BindTextParam(pStmt, 1, toDbName(InstrName));          String s = toDbName(InstrName);
1084            BindTextParam(pStmt, 1, s);
1085          BindTextParam(pStmt, 2, i.InstrFile);          BindTextParam(pStmt, 2, i.InstrFile);
1086          BindTextParam(pStmt, 3, i.FormatFamily);          BindTextParam(pStmt, 3, i.FormatFamily);
1087          BindTextParam(pStmt, 4, i.FormatVersion);          BindTextParam(pStmt, 4, i.FormatVersion);
# Line 1170  namespace LinuxSampler { Line 1182  namespace LinuxSampler {
1182                  throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));                  throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1183              }              }
1184    
1185              BindTextParam(pStmt, 2, File);              String s = toEscapedFsPath(File);
1186                BindTextParam(pStmt, 2, s);
1187              String ver = "";              String ver = "";
1188              if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major);              if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major);
1189              BindTextParam(pStmt, 4, ver);              BindTextParam(pStmt, 4, ver);
# Line 1229  namespace LinuxSampler { Line 1242  namespace LinuxSampler {
1242          std::stringstream sql2;          std::stringstream sql2;
1243          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";
1244          sql2 << "instr_nr=" << Index;          sql2 << "instr_nr=" << Index;
1245          if (ExecSqlInt(sql2.str(), File) > 0) return;          String s = toEscapedFsPath(File);
1246            if (ExecSqlInt(sql2.str(), s) > 0) return;
1247    
1248          BindTextParam(pStmt, 1, name);          BindTextParam(pStmt, 1, name);
1249          BindIntParam(pStmt, 3, Index);          BindIntParam(pStmt, 3, Index);
# Line 1638  namespace LinuxSampler { Line 1652  namespace LinuxSampler {
1652    
1653      String InstrumentsDb::toEscapedPath(String AbstractName) {      String InstrumentsDb::toEscapedPath(String AbstractName) {
1654          for (int i = 0; i < AbstractName.length(); i++) {          for (int i = 0; i < AbstractName.length(); i++) {
1655              if (AbstractName.at(i) == '\0')      AbstractName.replace(i++, 1, "\\/");              if (AbstractName.at(i) == '\0')      AbstractName.replace(i++, 1, "\\x2f");
1656              else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\");              else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\");
1657              else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'");              else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'");
1658              else if (AbstractName.at(i) == '"')  AbstractName.replace(i++, 1, "\\\"");              else if (AbstractName.at(i) == '"')  AbstractName.replace(i++, 1, "\\\"");
# Line 1659  namespace LinuxSampler { Line 1673  namespace LinuxSampler {
1673          return text;          return text;
1674      }      }
1675            
1676      String InstrumentsDb::toEscapedName(String AbstractName) {      String InstrumentsDb::toEscapedFsPath(String FsPath) {
1677          for (int i = 0; i < AbstractName.length(); i++) {          return toEscapedText(FsPath);
             if (AbstractName.at(i) == '\0')      AbstractName.at(i) = '/';  
             else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\");  
             else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'");  
             else if (AbstractName.at(i) == '"')  AbstractName.replace(i++, 1, "\\\"");  
             else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r");  
             else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n");  
         }  
         return AbstractName;  
1678      }      }
1679            
1680      String InstrumentsDb::toAbstractName(String DbName) {      String InstrumentsDb::toAbstractName(String DbName) {

Legend:
Removed from v.1345  
changed lines
  Added in v.1350

  ViewVC Help
Powered by ViewVC