tmpCode

  1 void BlkGroupWriteReader::_WriteBlockGroup(std::string fileName, int blockGroupID, BlockGroup* blkGroup)
  2 {
  3     ifstream  fileIn;
  4     ofstream fileOut;
  5     fileIn.clear();
  6     fileOut.clear();
  7     //test.clear();
  8     ///文件转写时需要的变量
  9     blkGroupStaticUnit tmpUnit;
 10     int *tmpInt = NULL, i, j, k;
 11     std::vector<int*> storage;
 12 
 13     ///文件流
 14     printf("Creating IFstream...\n");
 15 
 16     fileIn.open(fileName.c_str(), ios_base::binary);
 17     printf("CompleteI\n");
 18     ///如果读入源不能打开,尝试新建文件
 19     if(fileIn.fail())
 20     {
 21         printf("CANNOT OPEN FILE, CREATING NEW FILE...\n");
 22         fileIn.close();
 23         printf("Creating temp OFstream...\n");
 24         ofstream test;
 25         test.open(fileName.c_str(), ios_base::binary);
 26         printf("Opening file...\n");
 27         fileIn.open(fileName.c_str(), ios_base::binary);
 28         if(!fileIn.fail())
 29         {
 30             printf("File Opened, Writing Data...\n");
 31             tmpInt = 0;
 32             test.write((char*)&tmpInt, sizeof(int));
 33             printf("Write Complete.\n");
 34         }
 35         printf("Closing File...\n");
 36         test.close();
 37         printf("Complete.\n");
 38         //delete &test;
 39         printf("************************\nFileName: %s\nblockGroupID: %d\n************************\n", fileName.c_str(), blockGroupID);
 40         system("pause");
 41     }
 42     printf("Creating OFstream..\n");
 43     fileOut.open((fileName + "tmp").c_str(), ios_base::binary);
 44     printf("CompleteO\n");
 45     if(blockGroupID > 500)
 46     {
 47         printf("************************\nFileName: %s\nblockGroupID: %d\n************************\n", fileName.c_str(), blockGroupID);
 48         system("pause");
 49     }
 50 
 51     if(fileIn.fail() || fileOut.fail())
 52     {
 53         printf("WriteBlockGroup Failed: Cannot open file!\n");
 54         printf("************************\nFileName: %s\nblockGroupID: %d\n************************\n", fileName.c_str(), blockGroupID);
 55         system("pause");
 56         return;
 57     }
 58     if(blkGroup == NULL)
 59     {
 60         printf("WriteBlockGroup Failed: BlockGroup do NOT exist\n");
 61         printf("************************\nFileName: %s\nblockGroupID: %d\n************************\n", fileName.c_str(), blockGroupID);
 62         system("pause");
 63         return;
 64     }
 65 
 66     ///让方块组生成静态存储单元
 67     blkGroup->Group2Struct();
 68 
 69     ///读取文件头,确认是否存在方块组
 70     int blkGroupCount;
 71     printf("WRI!!!!!\n");
 72     fileIn.read((char*)&blkGroupCount, sizeof(int));
 73     printf("WRI!!!!!\t%d\n",blkGroupCount);
 74 
 75     ///文件中并未记录过该方块组的历史版本(方块组存储位置大于文件中的方块组数目,或存储位置<0,即在文件末尾添加该方块组),需要进行处理
 76     if(blockGroupID > blkGroupCount - 1 || blockGroupID < 0)
 77     {
 78         blockGroupID = blkGroupCount;
 79         blkGroupCount++;
 80     }
 81 
 82     ///转写文件
 83     fileOut.write((char*)&blkGroupCount, sizeof(int));
 84     for(i=0;i<blkGroupCount;i++)
 85     {
 86         ///对于不需要覆盖的方块组,照抄过去
 87         if(i != blockGroupID)
 88         {
 89             ///7个Vector3型变量:dirc movePro rotateAim rotateSpeed rotatePro nowPos centerPos
 90             for(j=0;j<7;j++)
 91             {
 92                 fileIn.read((char*)&tmpVec3,                            sizeof(Vector3));
 93                 fileOut.write((char*)&tmpVec3,                          sizeof(Vector3));
 94             }
 95 
 96             ///叶子节点数目
 97             fileIn.read((char*)&numLeaves,                              sizeof(    int));
 98             fileOut.write((char*)&numLeaves,                            sizeof(    int));
 99             for(j=0;j<numLeaves;j++)
100             {
101                 ///叶子节点中7个int型变量:minX minY minZ maxX maxY maxZ blockData
102                 for(k=0;k<7;k++)
103                 {
104                     fileIn.read((char*)&tmpInt,                         sizeof(    int));
105                     fileOut.write((char*)&tmpInt,                       sizeof(    int));
106                 }
107 
108                 ///叶子节点中1个short型变量:blockID
109                 fileIn.read((char*)&tmpShort,                           sizeof(  short));
110                 fileOut.write((char*)&tmpShort,                         sizeof(  short));
111             }
112         }
113 
114         ///该方块组需要覆盖
115         else
116         {
117             fileOut.write((char*)&(blkGroup->dirc),                     sizeof(Vector3));
118             fileOut.write((char*)&(blkGroup->movePro),                  sizeof(Vector3));
119             fileOut.write((char*)&(blkGroup->rotateAim),                sizeof(Vector3));
120             fileOut.write((char*)&(blkGroup->rotateSpeed),              sizeof(Vector3));
121             fileOut.write((char*)&(blkGroup->rotatePro),                sizeof(Vector3));
122             fileOut.write((char*)&(blkGroup->nowPos),                   sizeof(Vector3));
123             fileOut.write((char*)&(blkGroup->centerPos),                sizeof(Vector3));
124 
125             ///读取叶子节点
126             blkGroup->Group->walkLeafNode(&storage);
127             numLeaves = storage.size();
128 
129             fileOut.write((char*)&numLeaves,                            sizeof(    int));
130             for(j=0;j<numLeaves;j++)
131             {
132                 tmpInt = storage.at(j)[0]>>16;
133                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
134                 tmpInt = storage.at(j)[1]>>16;
135                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
136                 tmpInt = storage.at(j)[2]>>16;
137                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
138                 tmpInt = storage.at(j)[0]&((1<<16)-1);
139                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
140                 tmpInt = storage.at(j)[1]&((1<<16)-1);
141                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
142                 tmpInt = storage.at(j)[2]&((1<<16)-1);
143                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
144                 tmpInt = storage.at(j)[4];
145                 fileOut.write((char*)&(tmpInt),                         sizeof(    int));
146 
147                 tmpShort = (short)storage.at(j)[3];
148                 fileOut.write((char*)&(tmpShort),                       sizeof(  short));
149             }
150         }
151     }
152 
153     ///将转写的文件重命名
154     fileIn.close();
155     fileOut.close();
156     system(("del "+fileName).c_str());
157     system(("ren "+fileName+"tmp "+fileName).c_str());
158     if(blockGroupID > 500)
159     {
160         printf("************************\nFileName: %s\nblockGroupID: %d\n************************\n", fileName.c_str(), blockGroupID);
161         system("pause");
162     }
163     //delete &fileIn;
164     //delete &fileOut;
165     ///Last Edit: 2014-6-8 01:03(GMT+8)
166 }

 

posted @ 2014-06-10 20:19  Betairy linkzeldagg  阅读(243)  评论(0编辑  收藏  举报