table.h
Upload User: mxh851202
Upload Date: 2014-12-23
Package Size: 3593k
Code Size: 2k
Development Platform:

Unix_Linux

  1. #ifndef _TABLE_H_
  2. #define _TABLE_H_
  3. #include <vector.h>
  4. #include <Tool.h>
  5. using namespace std;
  6. class table
  7. {
  8. private:
  9.     void readAttrib()
  10.     {
  11. checkFileExist(name);
  12. int fd=openFile(name);
  13. char* temp = getNextLine(fd);
  14. if(temp!=NULL)
  15. {
  16. StringToken token(temp,',');
  17. int pos=0;
  18. while(token.hasMore())
  19. {
  20.     key.push_back(token.next());
  21.     pos++;
  22. }
  23.     }
  24. temp = getNextLine(fd);
  25. if(temp!=NULL)
  26. {
  27. StringToken token1(temp,',');
  28. int pos=0;
  29. while(token1.hasMore()) 
  30. {
  31.     attrib.push_back(token1.next());
  32.     pos++;
  33. }}
  34. temp = getNextLine(fd);
  35. while(temp!=NULL){
  36.             if(strcmp(temp,"")!=0){
  37.                 content.push_back(temp);
  38.                                  }            
  39.             temp=getNextLine(fd);           
  40.                      }
  41. closeFile(fd);
  42. //cout<<attrib[0]<<attrib[1]<<endl;
  43. //cout<<content[0]<<content[1]<<endl;
  44.     }
  45. public:
  46.     QString name;
  47.     vector<QString> key;
  48.     vector<QString> attrib; 
  49.     vector<char*> content;
  50.     table(const char* x)
  51.     {
  52. name = x;
  53. key = vector<QString>(0);
  54. attrib = vector<QString>(0);
  55. content = vector<char*>(0);
  56. readAttrib();
  57.     }
  58.     int getCols()
  59.     {
  60. return attrib.size();
  61.     }
  62.     int getRows()
  63.     {
  64. return content.size();
  65.     }
  66.     int isKey(QString x)
  67.     {
  68. for(int i= 0;i<key.size();i++)
  69. {
  70.     if (x == key[i])
  71. return 1;
  72. }
  73. return 0;
  74.     }
  75.     
  76.     char* getItem(int i,int j)
  77.     {
  78. StringToken token(content[i],',');
  79. int pos=0;
  80. for(;pos<j && token.hasMore(); pos++) 
  81.     token.next();
  82. if(token.hasMore()) 
  83.     return token.next();
  84. else {
  85.     perror("读行错误!!!");
  86.         return 0;
  87.     }
  88.     }
  89. };
  90. #endif