annotate base/UnitDatabase.cpp @ 979:c598b1d880f2

Fix potential null pointer deref
author Chris Cannam
date Wed, 03 Sep 2014 13:10:19 +0100
parents 417a1a08aadf
children
rev   line source
Chris@116 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@116 2
Chris@116 3 /*
Chris@116 4 Sonic Visualiser
Chris@116 5 An audio file viewer and annotation editor.
Chris@116 6 Centre for Digital Music, Queen Mary, University of London.
Chris@116 7 This file copyright 2006 Chris Cannam.
Chris@116 8
Chris@116 9 This program is free software; you can redistribute it and/or
Chris@116 10 modify it under the terms of the GNU General Public License as
Chris@116 11 published by the Free Software Foundation; either version 2 of the
Chris@116 12 License, or (at your option) any later version. See the file
Chris@116 13 COPYING included with this distribution for more information.
Chris@116 14 */
Chris@116 15
Chris@116 16 #include "UnitDatabase.h"
Chris@116 17
Chris@116 18 UnitDatabase
Chris@116 19 UnitDatabase::m_instance;
Chris@116 20
Chris@116 21 UnitDatabase *
Chris@116 22 UnitDatabase::getInstance()
Chris@116 23 {
Chris@116 24 return &m_instance;
Chris@116 25 }
Chris@116 26
Chris@116 27 UnitDatabase::UnitDatabase() :
Chris@116 28 m_nextId(0)
Chris@116 29 {
Chris@116 30 }
Chris@116 31
Chris@116 32 QStringList
Chris@116 33 UnitDatabase::getKnownUnits() const
Chris@116 34 {
Chris@116 35 QStringList list;
Chris@116 36 for (UnitMap::const_iterator i = m_units.begin(); i != m_units.end(); ++i) {
Chris@116 37 list.push_back(i->first);
Chris@116 38 }
Chris@116 39 return list;
Chris@116 40 }
Chris@116 41
Chris@116 42 void
Chris@116 43 UnitDatabase::registerUnit(QString unit)
Chris@116 44 {
Chris@116 45 if (m_units.find(unit) == m_units.end()) {
Chris@116 46 m_units[unit] = m_nextId++;
Chris@268 47 emit unitDatabaseChanged();
Chris@116 48 }
Chris@116 49 }
Chris@116 50
Chris@116 51 int
Chris@199 52 UnitDatabase::getUnitId(QString unit, bool registerNew)
Chris@116 53 {
Chris@116 54 if (m_units.find(unit) == m_units.end()) {
Chris@199 55 if (registerNew) registerUnit(unit);
Chris@199 56 else return -1;
Chris@116 57 }
Chris@116 58 return m_units[unit];
Chris@116 59 }
Chris@116 60
Chris@116 61 QString
Chris@116 62 UnitDatabase::getUnitById(int id)
Chris@116 63 {
Chris@116 64 for (UnitMap::const_iterator i = m_units.begin(); i != m_units.end(); ++i) {
Chris@116 65 if (i->second == id) return i->first;
Chris@116 66 }
Chris@116 67 return "";
Chris@116 68 }
Chris@116 69