00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "vmsDataFile.h"
00008 using namespace vmsFDM;
00009
00010 #ifdef _DEBUG
00011 #undef THIS_FILE
00012 static char THIS_FILE[]=__FILE__;
00013 #define new DEBUG_NEW
00014 #endif
00015
00016 vmsDataFile::vmsDataFile()
00017 {
00018 m_tData.GetData ().strName = "vmsDataFile v1.0";
00019 }
00020
00021 vmsDataFile::~vmsDataFile()
00022 {
00023
00024 }
00025
00026 LPDATAFILETREE vmsDataFile::FindItem(LPCSTR pszSection, LPCSTR pszValueName, LPDATAFILETREE ptRoot)
00027 {
00028 if (ptRoot == NULL)
00029 ptRoot = &m_tData;
00030
00031 fsString strS;
00032
00033 if (pszSection && *pszSection)
00034 {
00035 while (*pszSection && *pszSection != '\\' && *pszSection != '/')
00036 strS += *pszSection++;
00037
00038 if (*pszSection)
00039 pszSection++;
00040 }
00041
00042 if (strS.IsEmpty () == FALSE)
00043 {
00044
00045 for (int i = 0; i < ptRoot->GetLeafCount (); i++)
00046 {
00047 LPDATAFILETREE ptLeaf = ptRoot->GetLeaf (i);
00048 if (ptLeaf->GetData ().vt.empty () && ptLeaf->GetData ().strName == strS)
00049
00050 return *pszSection || (pszValueName && *pszValueName) ?
00051 FindItem (pszSection, pszValueName, ptLeaf) : ptLeaf;
00052 }
00053
00054 return NULL;
00055 }
00056
00057 for (int i = 0; i < ptRoot->GetLeafCount (); i++)
00058 {
00059 LPDATAFILETREE ptLeaf = ptRoot->GetLeaf (i);
00060 if (ptLeaf->GetData ().vt.empty () == false && ptLeaf->GetData ().strName == pszValueName)
00061 return ptLeaf;
00062 }
00063
00064 return NULL;
00065 }
00066
00067 void vmsDataFile::SaveToFile(HANDLE hFile)
00068 {
00069 vmsFile file;
00070 file.Attach (hFile, false);
00071 SaveToFile (file, &m_tData);
00072 }
00073
00074 void vmsDataFile::SaveToFile(vmsFile &file, LPDATAFILETREE ptRoot)
00075 {
00076 ASSERT (ptRoot != NULL);
00077
00078 SaveToFile (file, ptRoot->GetData ());
00079
00080 if (ptRoot->GetData ().vt.empty () == false)
00081 return;
00082
00083 file.WriteInt (ptRoot->GetLeafCount ());
00084
00085 for (int i = 0; i < ptRoot->GetLeafCount (); i++)
00086 SaveToFile (file, ptRoot->GetLeaf (i));
00087 }
00088
00089 void vmsDataFile::SaveToFile(vmsFile &file, vmsDataFileItem &item)
00090 {
00091
00092 file.WriteString (item.strName);
00093
00094 int iDT = (int)item.vt.type ();
00095 file.Write (&iDT, sizeof (iDT));
00096
00097
00098 switch (item.vt.type ())
00099 {
00100 case VVT_EMPTY: break;
00101 case VVT_INT: file.WriteInt (item.vt); break;
00102 case VVT_DOUBLE: file.WriteDouble (item.vt); break;
00103 case VVT_INT64: file.WriteInt64 (item.vt); break;
00104 case VVT_ASTRING: file.WriteString (item.vt); break;
00105 case VVT_LPBYTE:
00106 file.WriteInt (item.vt.bytebuffersize ());
00107 file.Write ((LPBYTE)item.vt, item.vt.bytebuffersize ());
00108 break;
00109 default: ASSERT (false);
00110 }
00111 }
00112
00113 void vmsDataFile::LoadFromFile(HANDLE hFile)
00114 {
00115 vmsFile file (hFile, false);
00116 m_tData.Clear ();
00117 LoadFromFile (file, &m_tData);
00118 }
00119
00120 void vmsDataFile::LoadFromFile(vmsFile &file, LPDATAFILETREE ptRoot)
00121 {
00122 ASSERT (ptRoot != NULL);
00123
00124 LoadFromFile (file, ptRoot->GetData ());
00125
00126 if (ptRoot->GetData ().vt.empty () == false)
00127 return;
00128
00129 int cLeafs;
00130 file.ReadInt (cLeafs);
00131
00132 for (int i = 0; i < cLeafs; i++)
00133 LoadFromFile (file, ptRoot->AddLeaf (vmsDataFileItem ()));
00134 }
00135
00136 void vmsDataFile::LoadFromFile(vmsFile &file, vmsDataFileItem &item)
00137 {
00138 file.ReadString (item.strName);
00139
00140 int enType;
00141 file.ReadInt (enType);
00142
00143 int i; double f; __int64 i64; fsString str;
00144 LPBYTE pb; int nSize;
00145
00146 switch (enType)
00147 {
00148 case VVT_EMPTY: break;
00149 case VVT_INT: file.ReadInt (i); item.vt = i; break;
00150 case VVT_DOUBLE: file.ReadDouble (f); item.vt = f; break;
00151 case VVT_INT64: file.ReadInt64 (i64); item.vt = i64; break;
00152 case VVT_ASTRING: file.ReadString (str); item.vt = str; break;
00153 case VVT_LPBYTE:
00154 file.ReadInt (nSize);
00155 pb = new BYTE [nSize];
00156 file.Read (pb, nSize);
00157 item.vt.set (pb, nSize);
00158 delete [] pb;
00159 break;
00160 default: ASSERT (false);
00161 }
00162 }
00163
00164 LPDATAFILETREE vmsDataFile::CreateSection(LPCSTR pszSection, LPDATAFILETREE ptRoot)
00165 {
00166 if (ptRoot == NULL)
00167 ptRoot = &m_tData;
00168
00169 if (pszSection == NULL || *pszSection == 0)
00170 return ptRoot;
00171
00172 fsString strS;
00173 while (*pszSection && *pszSection != '\\' && *pszSection != '/')
00174 strS += *pszSection++;
00175
00176 if (*pszSection)
00177 pszSection++;
00178
00179 if (strS != "")
00180 for (int i = 0; i < ptRoot->GetLeafCount (); i++)
00181 {
00182 LPDATAFILETREE ptLeaf = ptRoot->GetLeaf (i);
00183 if (ptLeaf->GetData ().vt.empty () && ptLeaf->GetData ().strName == strS)
00184 return CreateSection (pszSection, ptLeaf);
00185 }
00186
00187 LPDATAFILETREE ptLeaf = ptRoot->AddLeaf (vmsDataFileItem ());
00188 ptLeaf->GetData ().strName = strS;
00189 return CreateSection (pszSection, ptLeaf);
00190 }
00191
00192 LPDATAFILEITEM vmsDataFile::CreateItem(LPDATAFILETREE pSection, LPCSTR pszItemName)
00193 {
00194 LPDATAFILETREE ptLeaf = FindItem (NULL, pszItemName, pSection);
00195 if (ptLeaf == NULL)
00196 {
00197 ptLeaf = pSection->AddLeaf (vmsDataFileItem ());
00198 ptLeaf->GetData ().strName = pszItemName;
00199 }
00200 return &ptLeaf->GetData ().vt;
00201 }
00202
00203 LPDATAFILEITEM vmsDataFile::CreateItem(LPCSTR pszSection, LPCSTR pszItemName)
00204 {
00205 return CreateItem (CreateSection (pszSection), pszItemName);
00206 }
00207
00208 void vmsDataFile::set_Value(LPCSTR pszSection, LPCSTR pszValueName, int iValue)
00209 {
00210 LPDATAFILEITEM item = CreateItem (pszSection, pszValueName);
00211 item->set (iValue);
00212 }
00213
00214 void vmsDataFile::set_Value(LPCSTR pszSection, LPCSTR pszValueName, __int64 i64Value)
00215 {
00216 LPDATAFILEITEM item = CreateItem (pszSection, pszValueName);
00217 item->set (i64Value);
00218 }
00219
00220 void vmsDataFile::set_Value(LPCSTR pszSection, LPCSTR pszValueName, double fValue)
00221 {
00222 LPDATAFILEITEM item = CreateItem (pszSection, pszValueName);
00223 item->set (fValue);
00224 }
00225
00226 void vmsDataFile::set_Value(LPCSTR pszSection, LPCSTR pszValueName, LPCSTR pszValue)
00227 {
00228 LPDATAFILEITEM item = CreateItem (pszSection, pszValueName);
00229 item->set (pszValue);
00230 }
00231
00232 void vmsDataFile::set_Value(LPCSTR pszSection, LPCSTR pszValueName, LPBYTE pbValue, UINT nValueSize)
00233 {
00234 LPDATAFILEITEM item = CreateItem (pszSection, pszValueName);
00235 item->set (pbValue, nValueSize);
00236 }
00237
00238 void vmsDataFile::get_Value(LPCSTR pszSection, LPCSTR pszValueName, int &iValue)
00239 {
00240 LPDATAFILETREE item = FindItem (pszSection, pszValueName);
00241 if (item)
00242 iValue = item->GetData ().vt;
00243 }
00244
00245 void vmsDataFile::get_Value(LPCSTR pszSection, LPCSTR pszValueName, __int64 &i64Value)
00246 {
00247 LPDATAFILETREE item = FindItem (pszSection, pszValueName);
00248 if (item)
00249 i64Value = item->GetData ().vt;
00250 }
00251
00252 void vmsDataFile::get_Value(LPCSTR pszSection, LPCSTR pszValueName, double &fValue)
00253 {
00254 LPDATAFILETREE item = FindItem (pszSection, pszValueName);
00255 if (item)
00256 fValue = item->GetData ().vt;
00257 }
00258
00259 void vmsDataFile::get_Value(LPCSTR pszSection, LPCSTR pszValueName, LPCSTR &pszValue)
00260 {
00261 LPDATAFILETREE item = FindItem (pszSection, pszValueName);
00262 if (item)
00263 pszValue = item->GetData ().vt;
00264 }
00265
00266 void vmsDataFile::get_Value(LPCSTR pszSection, LPCSTR pszValueName, LPBYTE &pbValue, UINT &nValueSize)
00267 {
00268 LPDATAFILETREE item = FindItem (pszSection, pszValueName);
00269 if (item && item->GetData ().vt.type () != VVT_EMPTY)
00270 {
00271 pbValue = item->GetData ().vt;
00272 nValueSize = item->GetData ().vt.bytebuffersize ();
00273 }
00274 }