Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
1acc0e49
Commit
1acc0e49
authored
Jul 06, 2008
by
beder
Browse files
Added a (recursive) ordering, so we have a canonical output that we can compare.
parent
3cad5a2e
Changes
15
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
219 additions
and
53 deletions
+219
-53
content.h
content.h
+12
-2
iterator.cpp
iterator.cpp
+1
-1
ltnode.h
ltnode.h
+10
-0
map.cpp
map.cpp
+70
-25
map.h
map.h
+9
-3
node.cpp
node.cpp
+28
-2
node.h
node.h
+7
-2
scalar.cpp
scalar.cpp
+15
-0
scalar.h
scalar.h
+6
-0
sequence.cpp
sequence.cpp
+22
-0
sequence.h
sequence.h
+6
-0
test.yaml
test.yaml
+3
-17
tests.cpp
tests.cpp
+2
-1
tests/scalars.yaml
tests/scalars.yaml
+24
-0
yaml-reader.vcproj
yaml-reader.vcproj
+4
-0
No files found.
content.h
View file @
1acc0e49
...
...
@@ -5,12 +5,16 @@
#include <map>
#include "parserstate.h"
#include "exceptions.h"
#include "ltnode.h"
namespace
YAML
{
class
Scanner
;
class
Parser
;
class
Node
;
class
Scalar
;
class
Sequence
;
class
Map
;
class
Content
{
...
...
@@ -22,9 +26,9 @@ namespace YAML
virtual
void
Write
(
std
::
ostream
&
out
,
int
indent
,
bool
startedLine
,
bool
onlyOneCharOnLine
)
=
0
;
virtual
bool
GetBegin
(
std
::
vector
<
Node
*>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
bool
GetBegin
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
bool
GetBegin
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
bool
GetEnd
(
std
::
vector
<
Node
*>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
bool
GetEnd
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
bool
GetEnd
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
{
return
false
;
}
virtual
Node
*
GetNode
(
unsigned
i
)
const
{
return
0
;
}
virtual
unsigned
GetSize
()
const
{
return
0
;
}
...
...
@@ -37,6 +41,12 @@ namespace YAML
virtual
void
Read
(
double
&
d
)
{
throw
InvalidScalar
();
}
virtual
void
Read
(
char
&
c
)
{
throw
InvalidScalar
();
}
// ordering
virtual
int
Compare
(
Content
*
pContent
)
{
return
0
;
}
virtual
int
Compare
(
Scalar
*
pScalar
)
{
return
0
;
}
virtual
int
Compare
(
Sequence
*
pSeq
)
{
return
0
;
}
virtual
int
Compare
(
Map
*
pMap
)
{
return
0
;
}
protected:
};
}
iterator.cpp
View file @
1acc0e49
...
...
@@ -11,7 +11,7 @@ namespace YAML
{
}
Node
::
Iterator
::
Iterator
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
it
)
:
mapIter
(
it
),
type
(
IT_MAP
)
Node
::
Iterator
::
Iterator
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
it
)
:
mapIter
(
it
),
type
(
IT_MAP
)
{
}
...
...
ltnode.h
0 → 100644
View file @
1acc0e49
#pragma once
namespace
YAML
{
class
Node
;
struct
ltnode
{
bool
operator
()(
const
Node
*
pNode1
,
const
Node
*
pNode2
)
const
;
};
}
map.cpp
View file @
1acc0e49
...
...
@@ -24,13 +24,13 @@ namespace YAML
m_data
.
clear
();
}
bool
Map
::
GetBegin
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
bool
Map
::
GetBegin
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
{
it
=
m_data
.
begin
();
return
true
;
}
bool
Map
::
GetEnd
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
bool
Map
::
GetEnd
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
{
it
=
m_data
.
end
();
return
true
;
...
...
@@ -68,8 +68,8 @@ namespace YAML
Node
*
pKey
=
new
Node
;
Node
*
pValue
=
new
Node
;
m_data
[
pKey
]
=
pValue
;
try
{
// grab key
pKey
->
Parse
(
pScanner
,
state
);
...
...
@@ -78,6 +78,13 @@ namespace YAML
pScanner
->
PopNextToken
();
pValue
->
Parse
(
pScanner
,
state
);
}
m_data
[
pKey
]
=
pValue
;
}
catch
(
Exception
&
e
)
{
delete
pKey
;
delete
pValue
;
throw
e
;
}
}
}
...
...
@@ -105,8 +112,8 @@ namespace YAML
Node
*
pKey
=
new
Node
;
Node
*
pValue
=
new
Node
;
m_data
[
pKey
]
=
pValue
;
try
{
// grab key
pKey
->
Parse
(
pScanner
,
state
);
...
...
@@ -122,6 +129,14 @@ namespace YAML
pScanner
->
EatNextToken
();
else
if
(
pToken
->
type
!=
TT_FLOW_MAP_END
)
throw
MapEndNotFound
();
m_data
[
pKey
]
=
pValue
;
}
catch
(
Exception
&
e
)
{
// clean up and rethrow
delete
pKey
;
delete
pValue
;
throw
e
;
}
}
}
...
...
@@ -148,4 +163,34 @@ namespace YAML
if
(
m_data
.
empty
())
out
<<
std
::
endl
;
}
int
Map
::
Compare
(
Content
*
pContent
)
{
return
-
pContent
->
Compare
(
this
);
}
int
Map
::
Compare
(
Map
*
pMap
)
{
node_map
::
const_iterator
it
=
m_data
.
begin
(),
jt
=
pMap
->
m_data
.
begin
();
while
(
1
)
{
if
(
it
==
m_data
.
end
())
{
if
(
jt
==
pMap
->
m_data
.
end
())
return
0
;
else
return
-
1
;
}
if
(
jt
==
pMap
->
m_data
.
end
())
return
1
;
int
cmp
=
it
->
first
->
Compare
(
*
jt
->
first
);
if
(
cmp
!=
0
)
return
cmp
;
cmp
=
it
->
second
->
Compare
(
*
jt
->
second
);
if
(
cmp
!=
0
)
return
cmp
;
}
return
0
;
}
}
map.h
View file @
1acc0e49
...
...
@@ -14,17 +14,23 @@ namespace YAML
virtual
~
Map
();
void
Clear
();
virtual
bool
GetBegin
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
;
virtual
bool
GetEnd
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
&
it
)
const
;
virtual
bool
GetBegin
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
;
virtual
bool
GetEnd
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
&
it
)
const
;
virtual
void
Parse
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
virtual
void
Write
(
std
::
ostream
&
out
,
int
indent
,
bool
startedLine
,
bool
onlyOneCharOnLine
);
// ordering
virtual
int
Compare
(
Content
*
pContent
);
virtual
int
Compare
(
Scalar
*
pScalar
)
{
return
1
;
}
virtual
int
Compare
(
Sequence
*
pSeq
)
{
return
1
;
}
virtual
int
Compare
(
Map
*
pMap
);
private:
void
ParseBlock
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseFlow
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
protected:
typedef
std
::
map
<
Node
*
,
Node
*>
node_map
;
typedef
std
::
map
<
Node
*
,
Node
*
,
ltnode
>
node_map
;
node_map
m_data
;
};
}
node.cpp
View file @
1acc0e49
...
...
@@ -9,6 +9,12 @@
namespace
YAML
{
// the ordering!
bool
ltnode
::
operator
()(
const
Node
*
pNode1
,
const
Node
*
pNode2
)
const
{
return
*
pNode1
<
*
pNode2
;
}
Node
::
Node
()
:
m_pContent
(
0
),
m_alias
(
false
)
{
}
...
...
@@ -150,7 +156,7 @@ namespace YAML
if
(
m_pContent
->
GetBegin
(
seqIter
))
return
Iterator
(
seqIter
);
std
::
map
<
Node
*
,
Node
*>::
const_iterator
mapIter
;
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
mapIter
;
if
(
m_pContent
->
GetBegin
(
mapIter
))
return
Iterator
(
mapIter
);
...
...
@@ -168,7 +174,7 @@ namespace YAML
if
(
m_pContent
->
GetEnd
(
seqIter
))
return
Iterator
(
seqIter
);
std
::
map
<
Node
*
,
Node
*>::
const_iterator
mapIter
;
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
mapIter
;
if
(
m_pContent
->
GetEnd
(
mapIter
))
return
Iterator
(
mapIter
);
...
...
@@ -274,4 +280,24 @@ namespace YAML
node
.
Write
(
out
,
0
,
false
,
false
);
return
out
;
}
int
Node
::
Compare
(
const
Node
&
rhs
)
const
{
// Step 1: no content is the smallest
if
(
!
m_pContent
)
{
if
(
rhs
.
m_pContent
)
return
-
1
;
else
return
0
;
}
if
(
!
rhs
.
m_pContent
)
return
1
;
return
m_pContent
->
Compare
(
rhs
.
m_pContent
);
}
bool
operator
<
(
const
Node
&
n1
,
const
Node
&
n2
)
{
return
n1
.
Compare
(
n2
)
<
0
;
}
}
node.h
View file @
1acc0e49
...
...
@@ -6,6 +6,7 @@
#include <map>
#include "parserstate.h"
#include "exceptions.h"
#include "ltnode.h"
namespace
YAML
{
...
...
@@ -20,7 +21,7 @@ namespace YAML
public:
Iterator
();
Iterator
(
std
::
vector
<
Node
*>::
const_iterator
it
);
Iterator
(
std
::
map
<
Node
*
,
Node
*>::
const_iterator
it
);
Iterator
(
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
it
);
~
Iterator
();
friend
bool
operator
==
(
const
Iterator
&
it
,
const
Iterator
&
jt
);
...
...
@@ -37,7 +38,7 @@ namespace YAML
ITER_TYPE
type
;
std
::
vector
<
Node
*>::
const_iterator
seqIter
;
std
::
map
<
Node
*
,
Node
*>::
const_iterator
mapIter
;
std
::
map
<
Node
*
,
Node
*
,
ltnode
>::
const_iterator
mapIter
;
};
public:
...
...
@@ -95,6 +96,10 @@ namespace YAML
// insertion
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
Node
&
node
);
// ordering
int
Compare
(
const
Node
&
rhs
)
const
;
friend
bool
operator
<
(
const
Node
&
n1
,
const
Node
&
n2
);
private:
void
ParseHeader
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseTag
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
...
...
scalar.cpp
View file @
1acc0e49
...
...
@@ -88,4 +88,19 @@ namespace YAML
if
(
!
data
)
throw
InvalidScalar
();
}
int
Scalar
::
Compare
(
Content
*
pContent
)
{
return
-
pContent
->
Compare
(
this
);
}
int
Scalar
::
Compare
(
Scalar
*
pScalar
)
{
if
(
m_data
<
pScalar
->
m_data
)
return
-
1
;
else
if
(
m_data
>
pScalar
->
m_data
)
return
1
;
else
return
0
;
}
}
scalar.h
View file @
1acc0e49
...
...
@@ -23,6 +23,12 @@ namespace YAML
virtual
void
Read
(
double
&
d
);
virtual
void
Read
(
char
&
c
);
// ordering
virtual
int
Compare
(
Content
*
pContent
);
virtual
int
Compare
(
Scalar
*
pScalar
);
virtual
int
Compare
(
Sequence
*
pSeq
)
{
return
-
1
;
}
virtual
int
Compare
(
Map
*
pMap
)
{
return
-
1
;
}
protected:
std
::
string
m_data
;
};
...
...
sequence.cpp
View file @
1acc0e49
...
...
@@ -151,4 +151,26 @@ namespace YAML
if
(
m_data
.
empty
())
out
<<
std
::
endl
;
}
int
Sequence
::
Compare
(
Content
*
pContent
)
{
return
-
pContent
->
Compare
(
this
);
}
int
Sequence
::
Compare
(
Sequence
*
pSeq
)
{
unsigned
n
=
m_data
.
size
(),
m
=
pSeq
->
m_data
.
size
();
if
(
n
<
m
)
return
-
1
;
else
if
(
n
>
m
)
return
1
;
for
(
unsigned
i
=
0
;
i
<
n
;
i
++
)
{
int
cmp
=
m_data
[
i
]
->
Compare
(
*
pSeq
->
m_data
[
i
]);
if
(
cmp
!=
0
)
return
cmp
;
}
return
0
;
}
}
sequence.h
View file @
1acc0e49
...
...
@@ -22,6 +22,12 @@ namespace YAML
virtual
void
Parse
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
virtual
void
Write
(
std
::
ostream
&
out
,
int
indent
,
bool
startedLine
,
bool
onlyOneCharOnLine
);
// ordering
virtual
int
Compare
(
Content
*
pContent
);
virtual
int
Compare
(
Scalar
*
pScalar
)
{
return
1
;
}
virtual
int
Compare
(
Sequence
*
pSeq
);
virtual
int
Compare
(
Map
*
pMap
)
{
return
-
1
;
}
private:
void
ParseBlock
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseImplicit
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
...
...
test.yaml
View file @
1acc0e49
literal
:
|
Here's a literal scalar.
That's a newline.
Let's go...
folded
:
>
Here's a folded scalar that
wraps over to a newline.
Let's go...
regular
:
Here's a regular
scalar that keeps
on wrapping...
Let's go!
and last key
:
so it doesn't go bonkers
\ No newline at end of file
abeginning
:
value
zend
:
value
middle
:
value
\ No newline at end of file
tests.cpp
View file @
1acc0e49
...
...
@@ -15,6 +15,7 @@ namespace YAML
std
::
vector
<
std
::
string
>
files
;
files
.
push_back
(
"tests/simple.yaml"
);
files
.
push_back
(
"tests/mixed.yaml"
);
files
.
push_back
(
"tests/scalars.yaml"
);
bool
passed
=
true
;
for
(
unsigned
i
=
0
;
i
<
files
.
size
();
i
++
)
{
...
...
@@ -63,7 +64,7 @@ namespace YAML
if
(
firstTry
==
secondTry
)
return
true
;
std
::
ofstream
fout
(
"out.yaml"
);
std
::
ofstream
fout
(
"
tests/
out.yaml"
);
fout
<<
"---
\n
"
;
fout
<<
firstTry
<<
std
::
endl
;
fout
<<
"---
\n
"
;
...
...
tests/scalars.yaml
0 → 100644
View file @
1acc0e49
-
normal scalar, but
over several lines
-
|
literal scalar - so we can draw ASCII:
- -
| - |
------
-
>
and a folded scalar... so we
can just keep writing various
things. And if we want to keep indentation:
we just indent a little
see, this stays indented
-
>-
Here's a folded scalar
that gets chomped.
-
|-
And here's a literal scalar
that gets chomped.
-
>
2
Here's a folded scalar
that starts with some indentation.
\ No newline at end of file
yaml-reader.vcproj
View file @
1acc0e49
...
...
@@ -302,6 +302,10 @@
RelativePath=
".\content.h"
>
</File>
<File
RelativePath=
".\ltnode.h"
>
</File>
<File
RelativePath=
".\map.h"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment