configUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * 读写文件
  3. */
  4. #ifndef _CONFIG_UTILS_H_
  5. #define _CONFIG_UTILS_H_
  6. #include <unistd.h>
  7. #include <sstream>
  8. #include <iomanip>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <QFile>
  12. #include <QTextStream>
  13. #include <QTextCodec>
  14. using namespace std;
  15. inline void saveConfigString(const char* data,QString filename)
  16. {
  17. QFile file(filename);
  18. file.open(QIODevice::WriteOnly | QIODevice::Append);
  19. file.write(data);
  20. file.close();
  21. }
  22. inline bool saveConfigQString(QString data,QString filename)
  23. {
  24. QFile file(filename);
  25. if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
  26. {
  27. cout<<"file open field!"<<endl;
  28. return false;
  29. }
  30. QTextStream out(&file);
  31. if(data.size() > 0)
  32. {
  33. out<< data+"\n";
  34. }
  35. file.close() ;
  36. return true;
  37. }
  38. inline bool LoadConfigString(double& value, string name)
  39. {
  40. ifstream ifs(name.c_str());
  41. if(!ifs){
  42. ifs.close();
  43. return false;
  44. }
  45. string bufline;
  46. getline(ifs, bufline, '\n');
  47. value = atof(bufline.c_str());
  48. ifs.close();
  49. return true;
  50. }
  51. inline bool LoadConfigString(int& value, string name)
  52. {
  53. ifstream ifs(name.c_str());
  54. if(!ifs){
  55. ifs.close();
  56. return false;
  57. }
  58. string bufline;
  59. getline(ifs, bufline, '\n');
  60. value = atoi(bufline.c_str());
  61. ifs.close();
  62. return true;
  63. }
  64. inline bool LoadConfigString(string& value, string name)
  65. {
  66. ifstream ifs(name.c_str());
  67. if(!ifs)
  68. {
  69. ifs.close();
  70. return false;
  71. }
  72. string bufline;
  73. getline(ifs, bufline, '\n');
  74. value = bufline;
  75. ifs.close();
  76. return true;
  77. }
  78. inline bool SaveConfigString(const double& value, string name)
  79. {
  80. if( (access(name.c_str(), 06 )) == -1 )
  81. {
  82. stringstream ss;
  83. ss << "echo 0 > \"" << (name) << "\"";
  84. system(ss.str().c_str());
  85. }
  86. ofstream fs(name.c_str());
  87. if(!fs){
  88. fs.close();
  89. return false;
  90. }
  91. fs << value << endl;
  92. fs.close();
  93. return true;
  94. }
  95. inline bool SaveConfigString(const int& value, string name)
  96. {
  97. if( (access(name.c_str(), 06 )) == -1 )
  98. {
  99. stringstream ss;
  100. ss << "echo 0 > \"" << (name) << "\"";
  101. system(ss.str().c_str());
  102. }
  103. ofstream fs(name.c_str());
  104. if(!fs){
  105. fs.close();
  106. return false;
  107. }
  108. fs << value << endl;
  109. fs.close();
  110. return true;
  111. }
  112. inline bool SaveConfigString(const string& value, string name)
  113. {
  114. if((access(name.c_str(), 06 )) == -1 )
  115. {
  116. stringstream ss;
  117. ss << "echo 0 > \"" << (name) << "\"";
  118. system(ss.str().c_str());
  119. }
  120. ofstream fs(name.c_str());
  121. if(!fs)
  122. {
  123. fs.close();
  124. return false;
  125. }
  126. fs << value << endl;
  127. fs.close();
  128. return true;
  129. }
  130. inline bool saveDateToCsv(QString filename,QStringList lsit,bool isOneLine)
  131. {
  132. QFile file(filename);
  133. if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
  134. {
  135. cout<<"file open field!"<<endl;
  136. return false;
  137. }
  138. QTextStream out(&file);
  139. if(lsit.size())
  140. {
  141. for(int i=0;i<lsit.size();i++)
  142. {
  143. out<< lsit.at(i)+",";
  144. }
  145. if(isOneLine)
  146. {
  147. out<<",\n";
  148. }
  149. }
  150. file.close() ;
  151. return true;
  152. }
  153. inline bool saveMedDateToCsv(QString filename,QStringList lsit,bool isOneLine)
  154. {
  155. QFile file(filename);
  156. if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
  157. {
  158. cout<<"file open field!"<<endl;
  159. return false;
  160. }
  161. //情况1将字符编码设置为ANSI即GB码,因为window下的excel的编码默认为ANSI码
  162. //避免在window下打开该文件中文乱码
  163. QTextCodec *codec = QTextCodec::codecForName("GBK");
  164. QTextCodec::setCodecForLocale(codec);
  165. QTextStream out(&file);
  166. if(lsit.size())
  167. {
  168. for(int i=0;i<lsit.size();i++)
  169. {
  170. out<< lsit.at(i)+",";
  171. }
  172. if(isOneLine)
  173. {
  174. out<<",\n";
  175. }
  176. }
  177. file.close() ;
  178. //情况2 Linux下编码默认为utf-8编码,即unicode编码,前面设置了ANSI编码(GB码)
  179. //这里恢复utf-8编码,避免系统中其它地方出现乱码现象
  180. QTextCodec *utf8 = QTextCodec::codecForName("utf-8");
  181. QTextCodec::setCodecForLocale(utf8);
  182. return true;
  183. }
  184. #endif