configUtils.h 4.9 KB

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