QTfrontend/util/namegen.cpp
changeset 12499 d97050a7c074
parent 12295 ac57d564efce
child 12501 92c597704e57
equal deleted inserted replaced
12498:9f135bd021fe 12499:d97050a7c074
    16  * along with this program; if not, write to the Free Software
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18  */
    18  */
    19 
    19 
    20 #include <QFile>
    20 #include <QFile>
       
    21 #include <QFileInfo>
    21 #include <QTextStream>
    22 #include <QTextStream>
    22 #include <QStringList>
    23 #include <QStringList>
    23 #include <QLineEdit>
    24 #include <QLineEdit>
    24 
    25 
    25 #include "hwform.h"
    26 #include "hwform.h"
   174 
   175 
   175 QStringList HWNamegen::dictsForHat(const QString hatname)
   176 QStringList HWNamegen::dictsForHat(const QString hatname)
   176 {
   177 {
   177     QStringList list;
   178     QStringList list;
   178 
   179 
   179     // find .cfg to load the dicts from
   180     // Find and check .cfg to load the dicts from
   180     QFile file(QString("physfs://Names/%1.cfg").arg(hatname));
   181     QString path = QString("physfs://Names/%1.cfg").arg(hatname);
   181 
   182     QFileInfo check_file(path);
   182     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   183 
   183     {
   184     // Note: The .cfg file is optional; a fallback mechanism is in place (see below)
   184         QTextStream in(&file);
   185 
   185         QString line;
   186     // Check if file exists to prevent PhysFS from complaining in console so much
   186         do
   187     if (check_file.exists() && check_file.isFile())
   187         {
   188     {
   188             line = in.readLine();
   189         QFile file(path);
   189 
   190 
   190             if(!line.isEmpty())
   191         if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   191                 list.append(line);
   192         {
   192         } while (!line.isNull());
   193             QTextStream in(&file);
   193     }
   194             QString line;
   194 
   195             do
       
   196             {
       
   197                 line = in.readLine();
       
   198 
       
   199                 if(!line.isEmpty())
       
   200                     list.append(line);
       
   201             } while (!line.isNull());
       
   202         }
       
   203     }
       
   204 
       
   205     // Use Data/Names/generic.cfg by default
   195     if (list.size() == 0)
   206     if (list.size() == 0)
   196         list.append(QString("generic"));
   207         list.append(QString("generic"));
   197 
   208 
   198     return list;
   209     return list;
   199 }
   210 }