Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StreamReader.cpp
Go to the documentation of this file.
1 /****************************************************************************************
2 
3  Copyright (C) 2011 Autodesk, Inc.
4  All rights reserved.
5 
6  Use of this software is subject to the terms of the Autodesk license agreement
7  provided at the time of installation or download, or which otherwise accompanies
8  this software in either electronic or hard copy form.
9 
10 ****************************************************************************************/
11 #include "stdafx.h"
12 #include "StreamReader.h"
13 
14 #include <fbxfilesdk/fbxfilesdk_nsuse.h>
15 
16 StreamReader::StreamReader(KFbxSdkManager &pFbxSdkManager, int pID):
17 KFbxReader(pFbxSdkManager, pID),
18 mFilePointer(NULL),
19 mManager(&pFbxSdkManager)
20 {
21 }
22 
24 {
25  FileClose();
26 }
27 
28 void StreamReader::GetVersion(int& pMajor, int& pMinor, int& pRevision) const
29 
30 {
31  pMajor = 1;
32  pMinor = 0;
33  pRevision=0;
34 }
35 
36 bool StreamReader::FileOpen(char* pFileName)
37 {
38  if(mFilePointer != NULL)
39  FileClose();
40  mFilePointer = fopen(pFileName, "r");
41  if(mFilePointer == NULL)
42  return false;
43  return true;
44 }
46 {
47  if(mFilePointer!=NULL)
48  fclose(mFilePointer);
49  return true;
50 
51 }
53 {
54  if(mFilePointer != NULL)
55  return true;
56  return false;
57 }
58 
59 bool StreamReader::GetReadOptions(bool pParseFileAsNeeded)
60 {
61  return true;
62 }
63 
64 //Read the custom file and reconstruct node hierarchy.
65 bool StreamReader::Read(KFbxDocument* pDocument)
66 {
67  if (!pDocument)
68  {
69  GetError().SetLastErrorID(eINVALID_DOCUMENT_HANDLE);
70  return false;
71  }
72  KFbxScene* lScene = KFbxCast<KFbxScene>(pDocument);
73  bool lIsAScene = (lScene != NULL);
74  bool lResult = false;
75 
76  if(lIsAScene)
77  {
78  KFbxNode* lRootNode = lScene->GetRootNode();
79  KFbxNodeAttribute * lRootNodeAttribute = KFbxNull::Create(lScene,"");
80  lRootNode->SetNodeAttribute(lRootNodeAttribute);
81 
82  int lSize;
83  char* lBuffer = NULL;
84  if(mFilePointer != NULL)
85  {
86  //To obtain file size
87  fseek (mFilePointer , 0 , SEEK_END);
88  lSize = ftell (mFilePointer);
89  rewind (mFilePointer);
90 
91  //Read file content to a string.
92  lBuffer = (char*) malloc (sizeof(char)*lSize + 1);
93  size_t lRead = fread(lBuffer, 1, lSize, mFilePointer);
94  lBuffer[lRead]='\0';
95  KString lString(lBuffer);
96 
97  //Parse the string to get name and relation of Nodes.
98  KString lSubString, lChildName, lParentName;
99  KFbxNode* lChildNode;
100  KFbxNode* lParentNode;
101  KFbxNodeAttribute* lChildAttribute;
102  int lEndTokenCount = lString.GetTokenCount("\n");
103 
104  for (int i = 0; i < lEndTokenCount; i++)
105  {
106  lSubString = lString.GetToken(i, "\n");
107  KString lNodeString;
108  lChildName = lSubString.GetToken(0, "\"");
109  lParentName = lSubString.GetToken(2, "\"");
110 
111  //Build node hierarchy.
112  if(lParentName == "RootNode")
113  {
114  lChildNode = KFbxNode::Create(lScene,lChildName.Buffer());
115  lChildAttribute = KFbxNull::Create(mManager,"");
116  lChildNode->SetNodeAttribute(lChildAttribute);
117 
118  lRootNode->AddChild(lChildNode);
119  }
120  else
121  {
122  lChildNode = KFbxNode::Create(lScene,lChildName.Buffer());
123  lChildAttribute = KFbxNull::Create(lScene,"");
124  lChildNode->SetNodeAttribute(lChildAttribute);
125 
126  lParentNode = lRootNode->FindChild(lParentName.Buffer());
127  lParentNode->AddChild(lChildNode);
128  }
129  }
130  free(lBuffer);
131  }
132  lResult = true;
133  }
134  return lResult;
135 }
virtual bool Read(KFbxDocument *pDocument)
virtual bool GetReadOptions(bool pParseFileAsNeeded=true)
virtual void GetVersion(int &pMajor, int &pMinor, int &pRevision) const
virtual bool FileClose()
virtual bool IsFileOpen()
virtual bool FileOpen(char *pFileName)
virtual ~StreamReader()
StreamReader(KFbxSdkManager &pFbxSdkManager, int pID)