~vbursian/research-assistant/intervers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
////////////////////////////////////////////////////////////////////////////////
/*! @file FileNode.cpp   Базовые типы терминальных объектов сети.
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
- Copyright(C) 2016, Viktor E. Bursian, St.Petersburg, Russia.
                     Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#include "FileNode.h"
#include "Symbols.h"
#include <QMutexLocker>
#include <fstream>
namespace RA {
//------------------------------------------------------------------------------

//DEFINE_CLASS_TAG(sUndoAssignValue2FileNode)

//---------------------------------------------------------------- sFileNode ---

sFileNode::sFileNode (std::istream &  a_stream ,rcsVersion  version)
    :sByteArrayNode(a_stream,version)
{
  sString                     PathString;
  PathString.Read(a_stream);
  ThePath = PathString;
}


void sFileNode::Store (std::ostream &  a_stream) const
{
  sByteArrayNode::Store(a_stream);
  ThePath.PathStr().Write(a_stream);
}


sFileNode::sFileNode (rcsPath  path)
    :sByteArrayNode()
    ,ThePath(path)
{
  ImportFrom(ThePath);
}


sString  sFileNode::Text (eTextFormat        /*F*/
                         ,eTextDetalization  D)
{
  sString                     P(Symbol::BinaryData);
  sString                     T(ThePath.PathStr());
  P += P;
  if( D == Laconic ){
    if( T.Length() > CutOffLengthForLaconic-4 ){
      T = ThePath.Name() + "." + ThePath.Ext();
      if( T.Length() > CutOffLengthForLaconic-4 ){
        T = ThePath.Ext();
      }
    }
  }
  return P + T + P;
}


sPath  sFileNode::Path ()
{
  QMutexLocker                BodyLocker(&BodyMutex);
  return ThePath;
}


void  sFileNode::AssignPath (rcsPath  new_path)
{
  QMutexLocker                BodyLocker(&BodyMutex);
//  sNet::Manager->Undo.Push( new sUndoAssignValue2FileNode(sNodePtr(this)
//                                                     ,value) );
  if( new_path.PathStr() !=  ThePath.PathStr() ){
    ThePath = new_path;
    MarkAsEdited();
  }
}


void  sFileNode::AssignPath (rcsString  new_path_string)
{
  QMutexLocker                BodyLocker(&BodyMutex);
//  sNet::Manager->Undo.Push( new sUndoAssignValue2FileNode(sNodePtr(this)
//                                                     ,value) );
  if( new_path_string !=  ThePath.PathStr() ){
    ThePath = new_path_string;
    MarkAsEdited();
  }
}


void  sFileNode::CopyFrom (psNode  other)
{
  sByteArrayNode::CopyFrom(other);
  psFileNode                  Other = dynamic_cast<psFileNode>(other);
  if( Other ){
    sPath                       P = Other->Path();
    if( P.PathStr() !=  ThePath.PathStr() ){
      ThePath = P;
      MarkAsEdited();
    }
  }
}


bool  sFileNode::ImportFrom (sPath  path)
{
  bool                        OK = false;
  std::ifstream               F(path.PathStr());   // opens file
  if( F.is_open() ){
    char                        C;
    while( F.get(C) ){
      TheValue.push_back(C);
    }
    OK = F.eof();
    F.close();
  }
  return OK;
}


bool  sFileNode::ExportTo (sPath  path)
{
  bool                        OK = false;
  std::ofstream               F(path.PathStr());   // opens file
  if( F.is_open() ){
//    char                        C;
//    while( F.get(C) ){
//      TheValue.append(C);
//    }
//    OK = F.eof();
    F.close();
  }
  return OK;
}

//------------------------------------------------ sUndoAssignValue2FileNode ---

//void  sUndoAssignValue2FileNode::UndoIt ()
//{
//  rsFileNode(*NodePtr).SetValue( literal(OldValue) );
//};


//void  sUndoAssignValue2FileNode::RedoIt ()
//{
//  rsFileNode(*NodePtr).SetValue( literal(NewValue) );
//};


//sString  sUndoAssignValue2FileNode::Hint ()
//{
//  return  sString("change value '") + OldValue
//        + sString("' -> '") + NewValue + sString("'");
//};


//------------------------------------------------------------------------------
} //namespace RA