QIni > QT 자료실

본문 바로가기
사이트 내 전체검색

QT 자료실

일반 QIni

페이지 정보

작성자 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 17,438회 작성일 04-09-18 13:02

본문

Declaration [code=c] /*************************************************************************** * Copyright (C) 2004 by TBScope * tbscope@hotmail.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ***************************************************************************/ #ifndef QINI_H #define QINI_H #include #include #include /** * @author TBScope * @brief A helper class for storing ini data * It's not meant to be used directly, but it's used in QIni to store a pointer list of all ini data. */ class QIniData{ public: /** * The constructor for an ini data class.\n * This class contains a data structure for an ini file.\n * * @param section The section for a key=value pair.\n * Each section name is written as [sectionname] * * @param key The key name for a setting\n * Each key is unique for each section. This means that if you have a key named myKey in the section mySection1, you can't have a key with the same name in the same section. However, you can have a key with the same name in another section though. So myKey can be in mySection1 but also in section mySection2 * * @param value The value for a certain setting. * */ QIniData(const QString& section = "", const QString& key = "", const QString value = "") : m_strSection(section), m_strKey(key), m_strValue(value) {} ~QIniData() {} QString section() {return m_strSection;} QString key() {return m_strKey;} QString value() {return m_strValue;} void setSection(const QString& newSection) {m_strSection = newSection;} void setKey(const QString& newKey) {m_strKey = newKey;} void setValue(const QString& newValue) {m_strValue = newValue;} private: QString m_strSection; QString m_strKey; QString m_strValue; }; /** * @author TBScope * @brief A helper class for sorting the pointer list with stored ini data * It's not meant to be used directly, but it's used in QIni as a pointer list of all ini data. */ class QSortedIniPtrList : public QPtrList { public: /** * Internally used to compare two items, in order to sort them.\n * NOTE: Sorting happens with the sectionnames and keynames, not the values. */ int compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2); }; /** * @author TBScope * @brief A class for reading and storing settings from an ini file. * * Because QSettings isn't really what I was looking for, and because I'm used to using ini files, I created this class. * This class creates "standard" ini files.\n\n * Here's an example:\n\n * [aSection]\n * aKey=aValue\n\n * Note that this class doesn't parse or write comments (at the moment). */ class QIni{ public: /** * The constructor. * You have to manually set the path. */ QIni(); /** * The recommended constructor.\n * You can enter the path name via this constructor.\n * The ini file will automatically be read. */ QIni(const QString& pathName); /** * Destructor. */ ~QIni(); //Path + file management. /** * Set a new path name. * * @param pathName The new pat hname. * * Use this function to open another ini file for example.\n * The file will automatically be read. */ void setPathName(const QString& pathName) {m_strPathName = pathName;} /** * Get the current path name. * * @return A string containing the current path name. */ QString getPathName() {return m_strPathName;} //Read the inifile. /** * Read the ini file and store all records into a pointer list. */ void readIniFile(); //Reading and writing strings. /** * Write a string to the ini file. * * @param section The section where you want to store your string in. * @param key The key name for this string. * @param value The actual string itself. * * Note that both section and key can be empty strings.\n * If you don't use keys, please use a stringlist and readSectionValues to read all the values. Without a key, it's very hard to know which value you want. And if you don't use a key, it most likely is to store a list of some kind. It's adviced to use a key though. */ void writeString(const QString& section, const QString& key, const QString& value); /** * Read a string from the ini file. * * @param section The section where you want to read the string from. * @param key The key you want to read from. * @return The found value.\n * If no value could be found, the function will return an empty string. * * Note that both section and key can be empty.\n * It's not adviced to use this method for empty keynames, use readSectionValues for reading all the values from a certain section.\n\n * Key-value pairs that don't belong to a certain section (empty section), are stored on top of the ini file. If you leave the section string empty, it will search those key-value pairs. */ QString readString(const QString& section, const QString& key); /** * Read all the values from a certain section into a stringlist. * * @param section The section you want to read from. * @return A stringlist containing all the values from the given section. If nothing could be found, the function will return an empty stringlist. * * Use this function to read stored lists from an ini file.\n * This function is best used when you only want to read the values.\n * Usually, lists are stored without a key name. */ QStringList readSectionValues(const QString& section); /** * Read all the key=value pairs from a certain section into a stringlist. * * @param section The section you want to read from. * @return A stringlist containing all the values from the given section. If nothing could be found, the function will return an empty stringlist. * * Use this function to read stored lists from an ini file.\n * This function is best used when you also want to read the key names.\n * Usually, lists are stored without a key name. */ QStringList readSection(const QString& section); //Reading and writing integers. /** * Write an integer to the ini file. * * @param section The section where you want to store your string in. * @param key The key name for this string. * @param value The actual integer itself. * * Note that both section and key can be empty strings.\n * If you don't use keys, please use a stringlist and readSectionValues to read all the values. Without a key, it's very hard to know which value you want. And if you don't use a key, it most likely is to store a list of some kind. It's adviced to use a key though. */ void writeInteger(const QString& section, const QString& key, int value); /** * Read an integer from the ini file. * * @param section The section where you want to read the string from. * @param key The key you want to read from. * @return The found value.\n * If no value could be found, the function will return zero. * * Note that both section and key can be empty.\n * It's not adviced to use this method for empty keynames, use readSectionValues for reading all the values from a certain section.\n\n * Key-value pairs that don't belong to a certain section (empty section), are stored on top of the ini file. If you leave the section string empty, it will search those key-value pairs. */ int readInteger(const QString& section, const QString& key); //Reading and writing doubles. /** * Write a double to the ini file. * * @param section The section where you want to store your string in. * @param key The key name for this string. * @param value The actual double itself. * * Note that both section and key can be empty strings.\n * If you don't use keys, please use a stringlist and readSectionValues to read all the values. Without a key, it's very hard to know which value you want. And if you don't use a key, it most likely is to store a list of some kind. It's adviced to use a key though. */ void writeDouble(const QString& section, const QString& key, double value); /** * Read a double from the ini file. * * @param section The section where you want to read the string from. * @param key The key you want to read from. * @return The found value.\n * If no value could be found, the function will return zero. * * Note that both section and key can be empty.\n * It's not adviced to use this method for empty keynames, use readSectionValues for reading all the values from a certain section.\n\n * Key-value pairs that don't belong to a certain section (empty section), are stored on top of the ini file. If you leave the section string empty, it will search those key-value pairs. */ double readDouble(const QString& section, const QString& key); /** * Delete an entry (key) from a section. * * @param section The section where you want to delete the key from. * @param key The key you want to delete. * * Section and key can be empty. If so, then it will delete only the first occurence of such an entry.\n * So if you have a list of values, without a sectionname and without keynames, you must call this function every time you want to delete an entry. But this situation is quiet rare I think. */ void deleteKey(const QString& section, const QString& key); protected: /** * Used internally to store the pathname. */ QString m_strPathName; /** * Used internally to store a list of records from the ini file. */ QSortedIniPtrList iniData; private: /** * Writes the iniData list to the file.\n\n * WARNING: This will overwrite the file. So if you remove a key-value pair, it will be removed from the file too. * * This function is automatically called when you use a write... function. */ void sync(); }; #endif [/code] implementation [code=c] /*************************************************************************** * Copyright (C) 2004 by TBScope * tbscope@hotmail.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ***************************************************************************/ #include "qini.h" #include #include int QSortedIniPtrList::compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2) { QIniData *i1 = (QIniData*)item1; QIniData *i2 = (QIniData*)item2; if (i1->section() == i2->section()) { if (i1->key() == i2->key()) return 0; else if (i1->key() > i2->key()) return 1; else if (i1->key() < i2->key()) return -1; } else if (i1->section() > i2->section()) return 1; else if (i1->section() < i2->section()) return -1; return 0; } QIni::QIni() { // Make sure you set a pathname before writing anything to the ini file. } QIni::QIni(const QString& pathName) { m_strPathName = pathName; readIniFile(); } QIni::~QIni() { } void QIni::writeString(const QString& section, const QString& key, const QString& value) { QIniData *item; bool found = false; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section && item->key() == key && item->key().stripWhiteSpace() != "") { item->setValue(value); found = true; break; } } if (!found) { item = new QIniData(section, key, value); iniData.append(item); } sync(); } QString QIni::readString(const QString& section, const QString& key) { QIniData *item; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section && item->key() == key) { return item->value(); } } return QString::null; } QStringList QIni::readSectionValues(const QString& section) { QStringList fillMe; fillMe.clear(); QIniData *item; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section) { fillMe.append(item->value()); } } return fillMe; } QStringList QIni::readSection(const QString& section) { QStringList fillMe; fillMe.clear(); QIniData *item; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section) { fillMe.append(item->key() + "=" + item->value()); } } return fillMe; } void QIni::writeInteger(const QString& section, const QString& key, int value) { QString val; val.setNum(value); writeString(section, key, val); } int QIni::readInteger(const QString& section, const QString& key) { QIniData *item; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section && item->key() == key) { return item->value().toInt(); } } return 0; } void QIni::writeDouble(const QString& section, const QString& key, double value) { QString val; val.setNum(value); writeString(section, key, val); } double QIni::readDouble(const QString& section, const QString& key) { QIniData *item; for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section && item->key() == key) { return item->value().toDouble(); } } return 0; } void QIni::sync() { if (m_strPathName.stripWhiteSpace() == "") { qFatal("Fatal error. You didn't specify a ini filename to write the settings to."); return; } QFile file(m_strPathName); QIniData *item; QString prevSection = ""; int i = 0; if (file.open(IO_WriteOnly)) { QTextStream stream(&file); iniData.sort(); for(item = iniData.first(); item; item = iniData.next()) { if (prevSection != item->section()) { // Just some cosmetics. If the first line in the file, don't make it empty. if (i == 0) stream << "[" << item->section() << "]\n"; else stream << "\n[" << item->section() << "]\n"; } prevSection = item->section(); if (item->key().stripWhiteSpace() == "") stream << item->value() << "\n"; else stream << item->key() << "=" << item->value() << "\n"; i++; } file.close(); } } void QIni::readIniFile() { if (m_strPathName.stripWhiteSpace() == "") { qWarning("Warning. You didn't specify an ini filename to read settings from."); return; } QFile file(m_strPathName); QIniData *item; QString section = ""; if (file.open(IO_ReadOnly)) { QTextStream stream(&file); QString line; iniData.clear(); while (!stream.atEnd()) { line = stream.readLine(); if(line.stripWhiteSpace().startsWith("[")) section = line.section("[",1,1).section("]",0,0); if(line.contains('=')) { item = new QIniData(section, line.stripWhiteSpace().section("=",0,0), line.stripWhiteSpace().section("=",1,1)); iniData.append(item); } else { if(line.stripWhiteSpace() != "" && !line.stripWhiteSpace().startsWith("[")) { item = new QIniData(section, "", line.stripWhiteSpace()); iniData.append(item); } } } file.close(); } iniData.sort(); } void QIni::deleteKey(const QString& section, const QString& key) { QIniData *item; iniData.setAutoDelete(true); iniData.sort(); for(item = iniData.first(); item; item = iniData.next()) { if (item->section() == section && item->key() == key) { iniData.remove(item); //delete item; break; } } sync(); } [/code] Example [code=c] #include #include "qini.h" int main( int argc, char **argv ) { QApplication a( argc, argv ); QIni *myIni = new QIni(a.applicationDirPath() + "/test.ini"); myIni->writeString("aSection","aKeyname","aValue"); qDebug(myIni->readString("aSection","aKeyname")); delete myIni; return a.exec(); } [/code]

추천0

댓글목록

등록된 댓글이 없습니다.

Total 13건 1 페이지
QT 자료실 목록
번호 제목 글쓴이 조회 추천 날짜
13 일반 no_profile 이창규 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 27963 0 06-08
12 일반 유랑자 메일보내기 이름으로 검색 26675 0 04-29
11 QT/X11 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 31612 0 04-15
10 QT/Embedded no_profile 전창민 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 26956 0 03-29
9 QT/Embedded no_profile 이우진 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 26196 0 03-13
8 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 20865 0 10-13
7 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 21167 0 10-13
6 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 19384 0 10-13
5 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17911 0 10-13
4 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17337 0 10-13
열람중 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17439 0 09-18
2 일반 no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 21566 0 09-17
1 QT/X11
QStar Screensaver 댓글+ 1
no_profile korone 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 19565 0 09-17
게시물 검색

  • 게시물이 없습니다.

회원로그인

설문조사

새로운 홈페이지에 대한 평가

접속자집계

오늘
745
어제
880
최대
4,337
전체
5,123,126

Copyright © korone.net. All rights reserved.