RayTracing

- Титульная страница
- Классы
- Файлы

- Файлы
- Список членов всех файлов

Serial.cpp
См. документацию.

    00001 //Serializer.cpp
    00002
    00003 #include "stdio.h"
    00004 #include "Serial.h"
    00005
    00006 QString StartStr = "<?xml version = \"1.0\"?>\n<Scene>\n";
    00007 QString EndStr = "</Scene>";
    00008 QString StringStr1 = "<String Name = \"";
    00009 QString StringStr2 = "\"> ";
    00010 QString StringStr3 = " </String>\n";
    00011 QString BoolStr1 = "<Bool Name = \"";
    00012 QString BoolStr2 = "\"> ";
    00013 QString BoolStr3 = " </Bool>\n";
    00014 QString IntStr1 = "<Int Name = \"";
    00015 QString IntStr2 = "\"> ";
    00016 QString IntStr3 = " </Int>\n";
    00017 QString FloatStr1 = "<Float Name = \"";
    00018 QString FloatStr2 = "\"> ";
    00019 QString FloatStr3 = " </Float>\n";
    00020 QString VectorStr1 = "<Vector Name = \"";
    00021 QString VectorStr2 = "\"> ";
    00022 QString VectorStr3 = " </Vector>\n";
    00024 char TmpStr[256];
    00025
    00026 Serializer::Serializer()
    00027 {
    00028     File = NULL;
    00029     format = XML;
    00030 }
    00031
    00032 Serializer::~Serializer(){}
    00033
    00034 void Serializer::SerPic(int &Width, int &Height, char *Name)
    00035 {
    00036     QString StrOpen = "<Screen>\n", StrClose = "</Screen>\n";
    00037     int p = 0;
    00038     if (!readWrite && !format) p = 1;
    00039
    00040     if(readWrite)
    00041     {
    00042         //fprintf(f, StrOpen);
    00043         File->write(StrOpen.toAscii());
    00044         File->flush();
    00045     }
    00046     else
    00047         //fread(TmpStr, 1, strlen(StrOpen), f);
    00048         File->readLine(TmpStr, StrOpen.length() + p);
    00049
    00050     SerStr(Name, "Attribute");
    00051
    00052     SerInt(Width, "Width");
    00053     SerInt(Height, "Height");
    00054
    00055
    00056     if(readWrite)
    00057     {
    00058         //fprintf(f, StrClose);
    00059         File->write(StrClose.toAscii());
    00060         File->flush();
    00061     }
    00062     else
    00063         //fread(TmpStr, 1, strlen(StrClose), f);
    00064         File->readLine(TmpStr, StrClose.length() + p);
    00065 }
    00066
    00067 void Serializer::SerCam(Vector3D &Camera, char *Name)
    00068 {
    00069     int p = 0;
    00070     if (!readWrite && !format) p = 1;
    00071
    00072     QString StrOpen = "<Camera>\n", StrClose = "</Camera>\n";
    00073     if(readWrite)
    00074     {
    00075         //fprintf(f, StrOpen);
    00076         File->write(StrOpen.toAscii());
    00077         File->flush();
    00078     }
    00079     else
    00080         //fread(TmpStr, 1, strlen(StrOpen), f);
    00081         File->readLine(TmpStr, StrOpen.length() + p);
    00082
    00083     SerStr(Name, "Attribute");
    00084
    00085     SerVec(Camera, "Position");
    00086
    00087     if(readWrite)
    00088     {
    00089         //fprintf(f, StrClose);
    00090         File->write(StrClose.toAscii());
    00091         File->flush();
    00092     }
    00093     else
    00094         //fread(TmpStr, 1, strlen(StrClose), f);
    00095         File->readLine(TmpStr, StrClose.length() + p);
    00096 }
    00097
    00098 void Serializer::SerMat(Mater &Material, char * Name)
    00099 {
    00100     int p = 0;
    00101     if (!readWrite && !format) p = 1;
    00102
    00103     QString StrOpen = "<Material>\n", StrClose = "</Material>\n";
    00104     if(readWrite)
    00105     {
    00106         //fprintf(f, StrOpen);
    00107         File->write(StrOpen.toAscii());
    00108         File->flush();
    00109     }
    00110     else
    00111         //fread(TmpStr, 1, strlen(StrOpen), f);
    00112         File->readLine(TmpStr, StrOpen.length() + p);
    00113
    00114     SerStr (Name, "Attribute");
    00115
    00116     SerVec(Material.Colour, "Color");
    00117     SerVec(Material.Specular, "Specular");
    00118     SerVec(Material.Reflectivity, "Reflectivity");
    00119
    00120     if(readWrite)
    00121     {
    00122         //fprintf(f, StrClose);
    00123         File->write(StrClose.toAscii());
    00124         File->flush();
    00125     }
    00126     else
    00127         //fread(TmpStr, 1, strlen(StrClose), f);
    00128         File->readLine(TmpStr, StrClose.length() + p);
    00129 }
    00130
    00131 void Serializer::SerSph(Sphere &Sphere, char *Name)
    00132 {
    00133     int p = 0;
    00134     if (!readWrite && !format) p = 1;
    00135
    00136     QString StrOpen = "<Sphere>\n", StrClose = "</Sphere>\n";
    00137     if(readWrite)
    00138     {
    00139         //fprintf(f, StrOpen);
    00140         File->write(StrOpen.toAscii());
    00141         File->flush();
    00142     }
    00143     else
    00144         //fread(TmpStr, 1, strlen(StrOpen), f);
    00145         File->readLine(TmpStr, StrOpen.length() + p);
    00146
    00147     SerStr(Name, "Attribute");
    00148
    00149     SerVec(Sphere.Position, "Position");
    00150     SerFloat(Sphere.Radius, "Radius");
    00151     SerMat(Sphere.Material, "Material");
    00152
    00153     if(readWrite)
    00154     {
    00155         //fprintf(f, StrClose);
    00156         File->write(StrClose.toAscii());
    00157         File->flush();
    00158     }
    00159     else
    00160         //fread(TmpStr, 1, strlen(StrClose), f);
    00161         File->readLine(TmpStr, StrClose.length() + p);
    00162 }
    00163
    00164 void Serializer::SerThor(Thor &Thor, char *Name)
    00165 {
    00166     int p = 0;
    00167     if (!readWrite && !format) p = 1;
    00168
    00169     QString StrOpen = "<Thor>\n", StrClose = "</Thor>\n";
    00170     if(readWrite)
    00171     {
    00172         //fprintf(f, StrOpen);
    00173         File->write(StrOpen.toAscii());
    00174         File->flush();
    00175     }
    00176     else
    00177         //fread(TmpStr, 1, strlen(StrOpen), f);
    00178         File->readLine(TmpStr, StrOpen.length() + p);
    00179
    00180     SerStr(Name, "Attribute");
    00181
    00182     SerVec(Thor.PosCen, "Position");
    00183     SerFloat(Thor.Radius, "Radius");
    00184     SerFloat(Thor.Width, "Width");
    00185     SerMat(Thor.Material, "Material");
    00186
    00187     if(readWrite)
    00188     {
    00189         //fprintf(f, StrClose);
    00190         File->write(StrClose.toAscii());
    00191         File->flush();
    00192     }
    00193     else
    00194         //fread(TmpStr, 1, strlen(StrClose), f);
    00195         File->readLine(TmpStr, StrClose.length() + p);
    00196 }
    00197
    00198 void Serializer::SerLight(Light &Light, char *Name)
    00199 {
    00200     int p = 0;
    00201     if (!readWrite && !format) p = 1;
    00202
    00203     QString StrOpen = "<Light>\n", StrClose = "</Light>\n";
    00204     if(readWrite)
    00205     {
    00206         //fprintf(f, StrOpen);
    00207         File->write(StrOpen.toAscii());
    00208         File->flush();
    00209     }
    00210     else
    00211         //fread(TmpStr, 1, strlen(StrOpen), f);
    00212         File->readLine(TmpStr, StrOpen.length() + p);
    00213
    00214     SerStr(Name, "Caption");
    00215
    00216     SerVec(Light.Position, "Position");
    00217     SerVec(Light.Colour, "Color");
    00218     SerFloat(Light.ConstAttenuation, "ConstAttenuation");
    00219     SerFloat(Light.LinearAttenuation, "LinearAttenuation");
    00220     SerFloat(Light.QuadricAttenuation, "QuadricAttenuation");
    00221
    00222     if(readWrite)
    00223     {
    00224         //fprintf(f, StrClose);
    00225         File->write(StrClose.toAscii());
    00226         File->flush();
    00227     }
    00228     else
    00229         //fread(TmpStr, 1, strlen(StrClose), f);
    00230         File->readLine(TmpStr, StrClose.length() + p);
    00231 }
    00232
    00233 XMLS::XMLS()
    00234 {
    00235     format = XML;
    00236     readWrite = WR;
    00237 }
    00238
    00239 XMLS::~XMLS()
    00240 {
    00241 }
    00242
    00243 bool XMLS::Start(QString FileName)
    00244 {
    00245
    00246     File = new QFile(FileName);
    00247     if(!File->open(QIODevice::WriteOnly))
    00248         return false;
    00249
    00250     //int Len = StartStr.length();
    00251
    00252     File->write(StartStr.toAscii());
    00253     File->flush();
    00254
    00255     return true;
    00256 }
    00257
    00258 void XMLS::SerStr(char * Value, char * Name)
    00259 {
    00260     //fprintf(f, StringStr, Name, Value);
    00261     File->write(StringStr1.toAscii());
    00262     File->write(Name);
    00263     File->write(StringStr2.toAscii());
    00264     File->write(Value);
    00265     File->write(StringStr3.toAscii());
    00266     File->flush();
    00267 }
    00268
    00269 void XMLS::SerBool(bool &Value, char * Name)
    00270 {
    00271     //fprintf(f, '\t');
    00272     File->write(BoolStr1.toAscii());
    00273     File->write(Name);
    00274     File->write(BoolStr2.toAscii());
    00275     if(Value)
    00276     {
    00277         //fprintf(f, BoolStr, Name, "true");
    00278         File->write("true");
    00279     }
    00280     else
    00281         //fprintf(f, BoolStr, Name, "false");
    00282         File->write("false");
    00283
    00284     File->write(BoolStr3.toAscii());
    00285     File->flush();
    00286 }
    00287
    00288 void XMLS::SerInt(int &Value, char * Name)
    00289 {
    00290     //fprintf(f, '\t');
    00291     //fprintf(f, IntStr, Name, Value);
    00292     File->write(IntStr1.toAscii());
    00293     File->write(Name);
    00294     File->write(IntStr2.toAscii());
    00295     char buffer[20];
    00296     char *p;
    00297     p = itoa(Value, buffer, 10);
    00298     File->write(p);;
    00299     File->write(IntStr3.toAscii());
    00300     File->flush();
    00301 }
    00302
    00303 void XMLS::SerFloat(float &Value, char * Name)
    00304 {
    00305     //fprintf(f, '\t');
    00306     //fprintf(f, FloatStr, Name, Value);
    00307     File->write(FloatStr1.toAscii());
    00308     File->write(Name);
    00309     File->write(FloatStr2.toAscii());
    00310     char buffer[20];
    00311     sprintf(buffer, "%f", Value );
    00312     File->write(buffer);
    00313     File->write(FloatStr3.toAscii());
    00314     File->flush();
    00315 }
    00316
    00317 void XMLS::SerVec(Vector3D &Value, char * Name)
    00318 {
    00319     //fprintf(f, '\t');
    00320     //fprintf(f, VectorStr, Name, Value.X, Value.Y, Value.Z);
    00321     File->write(VectorStr1.toAscii());
    00322     File->write(Name);
    00323     File->write(VectorStr2.toAscii());
    00324     char buffer[20];
    00325     sprintf(buffer, "%f %f %f", Value.X, Value.Y, Value.Z);
    00326     File->write(buffer);
    00327     File->write(VectorStr3.toAscii());
    00328     File->flush();
    00329 }
    00330
    00331 void XMLS::End()
    00332 {
    00333     //int Len = strlen(EndStr);
    00334     //fwrite(EndStr, 1, Len, f);
    00335     File->write(EndStr.toAscii());
    00336     File->flush();
    00337     File->close();
    00338     delete File;
    00339 }
    00340
    00341 XMLL::XMLL()
    00342 {
    00343     format = XML;
    00344     readWrite = RE;
    00345 }
    00346
    00347 XMLL::~XMLL()
    00348 {
    00349 }
    00350
    00351 bool XMLL::Start(QString FileName)
    00352 {
    00353     //fscanf(f, StartStr);
    00354
    00355     File = new QFile(FileName);
    00356     if(!File->open(QIODevice::ReadOnly))
    00357         return false;
    00358
    00359     char *temp = new char [256];
    00360     File->read(temp, StartStr.length());
    00361     delete temp;
    00362
    00363     return true;
    00364 }
    00365
    00366 void XMLL::SerStr(char * Value, char * Name)
    00367 {
    00368     int i;
    00369     char c, p;
    00370
    00371     for(i = 0; i < 2; i++)
    00372     {
    00373         do
    00374         {
    00375             File->getChar(&c);
    00376             //c = fgetc(f);
    00377             if(c == EOF) return;
    00378         }
    00379         while(c != '\"');
    00380     }
    00381
    00382     do
    00383     {
    00384         File->getChar(&c);
    00385         //c = fgetc(f);
    00386         if(c == EOF) return;
    00387     }
    00388     while(c != '>');
    00389     File->getChar(&p);
    00390     //fgetc(f);
    00391
    00392     i = 0;
    00393
    00394     do
    00395     {
    00396
    00397         File->getChar(&c);
    00398         //c = fgetc(f);
    00399         if(c == EOF) return;
    00400
    00401         TmpStr[i] = c;
    00402         i++;
    00403     }
    00404     while(c != '<');
    00405     TmpStr[i-2] = 0;
    00406
    00407     //fread(TmpStr, 1, strlen("/Int>\n"), f);
    00408     File->read(TmpStr, strlen("/Int>\n"));
    00409 }
    00410
    00411 void XMLL::SerBool(bool &Value, char * Name)
    00412 {
    00413     int i;
    00414     char c;
    00415     char str[256];
    00416
    00417     for(i = 0; i < 2; i++)
    00418     {
    00419         do
    00420         {
    00421             //c = fgetc(f);
    00422             File->getChar(&c);
    00423             if(c == EOF) return;
    00424         }
    00425     while(c != '\"');
    00426     }
    00427
    00428     do
    00429     {
    00430         //c = fgetc(f);
    00431         File->getChar(&c);
    00432         if(c == EOF) return;
    00433     }
    00434     while(c != '>');
    00435     //fgetc(f);
    00436     File->getChar(&c);
    00437
    00438     //fscanf(f, "%s", str);
    00439     File->readLine(str, sizeof(str));
    00440     if(!strcmp(str, "true"))
    00441         Value = true;
    00442     else
    00443         Value = false;
    00444     //fread(TmpStr, 1, strlen("</Bool>\n"), f);
    00445     File->read(TmpStr, strlen("</Bool>\n"));
    00446 }
    00447
    00448 void XMLL::SerInt(int &Value, char * Name)
    00449 {
    00450     int i;
    00451     char c;
    00452     char str[256];
    00453
    00454     for(i = 0; i < 2; i++)
    00455     {
    00456         do
    00457         {
    00458             //c = fgetc(f);
    00459             File->getChar(&c);
    00460             if(c == EOF) return;
    00461         }
    00462     while(c != '\"');
    00463     }
    00464
    00465     do
    00466     {
    00467         //c = fgetc(f);
    00468         File->getChar(&c);
    00469         if(c == EOF) return;
    00470     }
    00471     while(c != '>');
    00472     //fgetc(f);
    00473     File->getChar(&c);
    00474
    00475     File->readLine(str, sizeof(str));
    00476     sscanf(str, "%d", &Value);
    00477     //fread(TmpStr, 1, strlen("</Int>\n"), f);
    00478     File->read(TmpStr, strlen("</Int>\\n"));
    00479 }
    00480
    00481 void XMLL::SerFloat(float &Value, char * Name)
    00482 {
    00483     int i;
    00484     char c;
    00485     char str[256];
    00486
    00487     for(i = 0; i < 2; i++)
    00488     {
    00489         do
    00490         {
    00491             //c = fgetc(f);
    00492             File->getChar(&c);
    00493             if(c == EOF) return;
    00494         }
    00495     while(c != '\"');
    00496     }
    00497
    00498     do
    00499     {
    00500         //c = fgetc(f);
    00501         File->getChar(&c);
    00502         if(c == EOF) return;
    00503     }
    00504     while(c != '>');
    00505     //fgetc(f);
    00506     File->getChar(&c);
    00507
    00508     File->readLine(str, sizeof(str));
    00509     sscanf(str, "%f", &Value);
    00510     //fscanf(f, "%f", &Value);
    00511     //fread(TmpStr, 1, strlen("</Float>\n"), f);
    00512     File->read(TmpStr, strlen("</Float>\n"));
    00513 }
    00514
    00515 void XMLL::SerVec(Vector3D &Value, char * Name)
    00516 {
    00517     int i;
    00518     char c;
    00519     char str[256];
    00520
    00521     for(i = 0; i < 2; i++)
    00522     {
    00523         do
    00524         {
    00525             //c = fgetc(f);
    00526             File->getChar(&c);
    00527             if(c == EOF) return;
    00528         }
    00529     while(c != '\"');
    00530     }
    00531
    00532     do
    00533     {
    00534         //c = fgetc(f);
    00535         File->getChar(&c);
    00536         if(c == EOF) return;
    00537     }
    00538     while(c != '>');
    00539     //fgetc(f);
    00540     File->getChar(&c);
    00541
    00542     File->readLine(str, sizeof(str));
    00543     //sscanf(str, "%d", &Value);
    00544     sscanf(str, "%f %f %f", &Value.X, &Value.Y, &Value.Z);
    00545     //fread(TmpStr, 1, strlen("</Float>\n"), f);
    00546     File->read(TmpStr, strlen("</Float>\n"));
    00547 }
    00548
    00549 void XMLL::End()
    00550 {
    00551     //int Len = strlen(EndStr);
    00552     //fread(TmpStr, 1, Len, f);
    00553     File->read(TmpStr, EndStr.length());
    00554     File->close();
    00555     delete File;
    00556 }
    00557
    00558 BinS::BinS()
    00559 {
    00560     format = Bin;
    00561     readWrite = WR;
    00562 }
    00563
    00564 BinS::~BinS()
    00565 {
    00566 }
    00567
    00568 bool BinS::Start(QString FileName)
    00569 {
    00570     //f = fopen(FileName, "wb");
    00571     //return (f != NULL);
    00572
    00573     File = new QFile(FileName);
    00574     return (File->open(QIODevice::WriteOnly));
    00575 }
    00576
    00577 void BinS::SerStr(char * Value, char * Name)
    00578 {
    00579     //int Len = strlen(Value);
    00580     //fwrite(&Len, sizeof(int), 1, f);
    00581     //fwrite(Value, 1, Len, f);
    00582     File->write("char* ");
    00583     File->write(Value, sizeof(Value));
    00584     File->write("\n", 1);
    00585     File->flush();
    00586 }
    00587
    00588 void BinS::SerBool(bool &Value, char * Name)
    00589 {
    00590     //fwrite(&Value, sizeof(bool), 1, f);
    00591     File->write("bool ");
    00592     char buffer[20];
    00593     sprintf(buffer, "%b", Value);
    00594     File->write(buffer);
    00595     File->write("\n", 1);
    00596     File->flush();
    00597 }
    00598
    00599 void BinS::SerInt(int &Value, char * Name)
    00600 {
    00601     //fwrite(&Value, sizeof(int), 1, f);
    00602     File->write("int ");
    00603     char buffer[20];
    00604     sprintf(buffer, "%i", Value);
    00605     File->write(buffer);
    00606     File->write("\n", 1);
    00607     File->flush();
    00608 }
    00609
    00610 void BinS::SerFloat(float &Value, char * Name)
    00611 {
    00612     //fwrite(&Value, sizeof(float), 1, f);
    00613     File->write("float ");
    00614     char buffer[20];
    00615     sprintf(buffer, "%f", Value);
    00616     File->write(buffer);
    00617     File->write("\n", 1);
    00618     File->flush();
    00619 }
    00620
    00621 void BinS::SerVec(Vector3D &Value, char * Name)
    00622 {
    00623     SerStr(Name, "Vector3D");
    00624     SerFloat(Value.X);
    00625     SerFloat(Value.Y);
    00626     SerFloat(Value.Z);
    00627 }
    00628
    00629 void BinS::End()
    00630 {
    00631     File->close();
    00632     delete File;
    00633 }
    00634
    00635 BinL::BinL()
    00636 {
    00637     format = Bin;
    00638     readWrite = RE;
    00639 }
    00640
    00641 BinL::~BinL()
    00642 {
    00643 }
    00644
    00645 bool BinL::Start(QString FileName)
    00646 {
    00647     //f = fopen(FileName, "rb");
    00648     //return (f != NULL);
    00649     File = new QFile(FileName);
    00650     if(!File->open(QIODevice::ReadOnly))
    00651         return false;
    00652
    00653     char buffer[256];
    00654     //File->readLine(buffer, sizeof(buffer));
    00655     return true;
    00656 }
    00657
    00658 void BinL::SerStr(char * Value, char * Name)
    00659 {
    00660     //int Len;
    00661     //fread(&Len, sizeof(int), 1, f);
    00662     //fread(TmpStr, 1, Len, f);
    00663     char buffer[256];
    00664     File->readLine(buffer, sizeof(buffer));
    00665     char buf1 = 'A';
    00666 }
    00667
    00668 void BinL::SerBool(bool &Value, char * Name)
    00669 {
    00670     //fread(&Value, sizeof(bool), 1, f);
    00671     char buffer[256];
    00672     char buf[8];
    00673     File->readLine(buffer, qint64(sizeof(buffer)));
    00674     sscanf(buffer, "%s %b", buf, &Value);
    00675     //delete [] buf;
    00676     //delete [] buffer;
    00677     char buf1 = 'A';
    00678 }
    00679
    00680 void BinL::SerInt(int &Value, char * Name)
    00681 {
    00682     //fread(&Value, sizeof(int), 1, f);
    00683     char buffer[256];
    00684     char buf[8];
    00685     File->readLine(buffer, qint64(sizeof(buffer)));
    00686     sscanf(buffer, "%s %d", buf, &Value);
    00687     //delete [] buf;
    00688     //delete [] buffer;
    00689     char buf1 = 'A';
    00690 }
    00691
    00692 void BinL::SerFloat(float &Value, char * Name)
    00693 {
    00694     //fread(&Value, sizeof(float), 1, f);
    00695     char buffer[256];
    00696     char buf[8];
    00697     File->readLine(buffer, qint64(sizeof(buffer)));
    00698     sscanf(buffer, "%s %f", buf, &Value);
    00699     //delete [] buf;
    00700     //delete [] buffer;
    00701     char buf1 = 'A';
    00702 }
    00703
    00704 void BinL::SerVec(Vector3D &Value, char * Name)
    00705 {
    00706     SerStr(Name, "Caption");
    00707     SerFloat(Value.X);
    00708     SerFloat(Value.Y);
    00709     SerFloat(Value.Z);
    00710     char buf1 = 'A';
    00711 }
    00712
    00713 void BinL::End()
    00714 {
    00715     delete File;
    00716 }