Browse Source

cmData.h/c:More basic development.

master
kpl 11 years ago
parent
commit
ece2c83bf7
2 changed files with 205 additions and 5 deletions
  1. 182
    0
      cmData.c
  2. 23
    5
      cmData.h

+ 182
- 0
cmData.c View File

74
   return 0;
74
   return 0;
75
 }
75
 }
76
 
76
 
77
+bool cmIsValue(  const cmData_t* p )
78
+{ return kMinValDtId <= p->tid && p->tid <= kMaxValDtId; }
79
+
80
+bool cmIsPtr(    const cmData_t* p )
81
+{ return kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId; }
82
+
83
+bool cmIsStruct( const cmData_t* p )
84
+{ return kMinStructDtId <= p->tid && p->tid <= kMaxStructDtId; }
85
+
77
 char            cmDataChar(      const cmData_t* p ) { assert(p->tid==kCharDtId);      return p->u.c; }
86
 char            cmDataChar(      const cmData_t* p ) { assert(p->tid==kCharDtId);      return p->u.c; }
78
 unsigned char   cmDataUChar(     const cmData_t* p ) { assert(p->tid==kUCharDtId);     return p->u.uc; } 
87
 unsigned char   cmDataUChar(     const cmData_t* p ) { assert(p->tid==kUCharDtId);     return p->u.uc; } 
79
 short           cmDataShort(     const cmData_t* p ) { assert(p->tid==kShortDtId);     return p->u.s; } 
88
 short           cmDataShort(     const cmData_t* p ) { assert(p->tid==kShortDtId);     return p->u.s; } 
1013
   _cmDataFree(p);
1022
   _cmDataFree(p);
1014
 }
1023
 }
1015
 
1024
 
1025
+cmData_t* cmDataUnlink( cmData_t* p )
1026
+{
1027
+  if( p->parent == NULL )
1028
+    return p;
1029
+
1030
+  assert( cmDataIsStruct(p->parent) );
1031
+
1032
+  cmData_t* cp = p->u.child;
1033
+  cmData_t* pp = NULL;
1034
+  for(; cp!=NULL; cp=cp->sibling)
1035
+    if( cp == p )
1036
+    {
1037
+      if( pp == NULL )
1038
+        p->parent->u.child = p->sibling;
1039
+      else
1040
+        pp->sibling = cp->sibling;
1041
+    }
1042
+  return p;
1043
+}
1044
+
1045
+unsigned cmDataChildCount( const cmData_t* p )
1046
+{
1047
+  if( !cmDataIsStruct(p) )
1048
+    return 0;
1049
+
1050
+  unsigned n = 0;
1051
+  const cmData_t* cp = p->u.child;
1052
+  for(; cp!=NULL; cp=cp->sibling)
1053
+    ++n;
1054
+
1055
+  return n;
1056
+}
1057
+
1058
+cmData_t* cmDataChild( cmData_t* p, unsigned index )
1059
+{
1060
+  if( !cmDataIsStruct(p) )
1061
+    return NULL;
1062
+
1063
+  unsigned  n  = 0;
1064
+  cmData_t* cp = p->u.child;
1065
+  for(; cp!=NULL; cp=cp->sibling)
1066
+  {
1067
+    if( n == index )
1068
+      break;
1069
+    ++n;
1070
+  }
1071
+
1072
+  return cp;
1073
+}
1074
+
1075
+cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p )
1076
+{
1077
+  assert( cmDataIsStruct(p) );
1078
+  
1079
+  p->u.child    = parent->u.child;
1080
+  parent->u.child = p;
1081
+  return p;
1082
+}
1083
+
1084
+cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p )
1085
+{
1086
+  assert( cmDataIsStruct(p) );
1087
+
1088
+  cmData_t* cp = parent->u.child;
1089
+  if( cp == NULL )
1090
+    parent->u.child = p;
1091
+  else
1092
+  {
1093
+    for(; cp!=NULL; cp=cp->sibling)
1094
+      if( cp->sibling == NULL )
1095
+      {
1096
+        cp->sibling = p;
1097
+        break;
1098
+      }
1099
+  }
1100
+
1101
+  p->sibling = NULL;
1102
+  return p;
1103
+}
1104
+
1105
+cmData_t* cmDataInsertChild( cmData_t* parent, cmData_t* p, unsigned index )
1106
+{
1107
+  if( !cmDataIsStruct(parent) )
1108
+    return NULL;
1109
+
1110
+  unsigned  n  = 0;
1111
+  cmData_t* cp = parent->u.child;
1112
+  cmData_t* pp = NULL;
1113
+  for(; cp!=NULL; cp=cp->sibling)
1114
+  {
1115
+    if( n == index )
1116
+    {
1117
+      if( pp == NULL )
1118
+      {
1119
+        parent->u.child = p;
1120
+        p->sibling = NULL;
1121
+      }
1122
+      else
1123
+      {
1124
+        p->sibling  = pp->sibling;
1125
+        pp->sibling = p;
1126
+      }
1127
+      break;
1128
+        
1129
+    }
1130
+    ++n;
1131
+  }
1132
+
1133
+  return p;
1134
+  
1135
+}
1136
+
1137
+//----------------------------------------------------------------------------
1138
+
1139
+bool _cmDataPairIsValid( const cmData_t* p )
1140
+{
1141
+  assert( p->tid == kPairDtId );
1142
+
1143
+  const cmData_t* cp = p->u.child;
1144
+  bool fl = cp->u.child == NULL || cp->u.child->sibling == NULL || cp->u.child->sibling->sibling!=NULL;
1145
+  return !fl;
1146
+}
1147
+
1148
+// Get the key/value of a pair
1149
+cmData_t* cmDataPairKey(          cmData_t* p )
1150
+{
1151
+  assert( _cmDataPairIsValid(p) );
1152
+  return p->u.child;
1153
+}
1154
+
1155
+cmData_t* cmDataPairValue(        cmData_t* p )
1156
+{
1157
+  assert( _cmDataPairIsValid(p) );
1158
+  return p->u.child->sibling;
1159
+}
1160
+    
1161
+  // Set the key or value of an existing pair node. 
1162
+cmData_t* cmDataPairSetValue(     cmData_t* p, cmData_t* value )
1163
+{
1164
+}
1165
+
1166
+cmData_t* cmDataPairAllocValue(   cmData_t* p, const cmData_t* value )
1167
+{
1168
+}
1169
+
1170
+cmData_t* cmDataPairSetKey(       cmData_t* p, cmData_t* key )
1171
+{
1172
+}
1173
+
1174
+cmData_t* cmDataPairSetKeyId(     cmData_t* p, unsigned id )
1175
+{
1176
+}
1177
+
1178
+cmData_t* cmDataPairSetKeyLabel(  cmData_t* p, const cmChar_t* label )
1179
+{
1180
+}
1181
+
1182
+cmData_t* cmDataPairAllocKey(     cmData_t* p, const cmData_t* key )
1183
+{
1184
+}
1185
+
1186
+// Dynamically allocate a pair node 
1187
+cmData_t* cmDataAllocPair(      cmData_t* parent, const cmData_t* key,  const cmData_t* value )
1188
+{
1189
+}
1190
+
1191
+cmData_t* cmDataAllocPairId(    cmData_t* parent, unsigned  keyId,      cmData_t* value )
1192
+{
1193
+}
1194
+
1195
+cmData_t* cmDataAllocPairLabel( cmData_t* parent, const cmChar_t label, cmData_t* value )
1196
+{
1197
+}
1016
   
1198
   
1017
   
1199
   
1018
 unsigned cmDataSerializeByteCount( const cmData_t* p )
1200
 unsigned cmDataSerializeByteCount( const cmData_t* p )

+ 23
- 5
cmData.h View File

27
   {
27
   {
28
     kInvalidDtId,
28
     kInvalidDtId,
29
 
29
 
30
-    kNullDtId,
30
+    kMinValDtId,
31
+
32
+    kNullDtId = kMinValDtId,
31
 
33
 
32
     kUCharDtId,
34
     kUCharDtId,
33
     kCharDtId,
35
     kCharDtId,
42
 
44
 
43
     kStrDtId,
45
     kStrDtId,
44
     kConstStrDtId,
46
     kConstStrDtId,
47
+    kMaxValDtId = kConstStrDtId,
45
 
48
 
46
     kMinPtrDtId,
49
     kMinPtrDtId,
47
     kUCharPtrDtId = kMinPtrDtId,  // cnt=array element count
50
     kUCharPtrDtId = kMinPtrDtId,  // cnt=array element count
117
 
120
 
118
   typedef unsigned cmDtRC_t;
121
   typedef unsigned cmDtRC_t;
119
 
122
 
123
+  bool cmDataIsValue(  const cmData_t* p );
124
+  bool cmDataIsPtr(    const cmData_t* p );
125
+  bool cmDataIsStruct( const cmData_t* p );
126
+  
127
+
120
   // Get the value of an object without conversion.
128
   // Get the value of an object without conversion.
121
   // The data type id must match the return type or the
129
   // The data type id must match the return type or the
122
   // conversion must be an automatic C conversion.
130
   // conversion must be an automatic C conversion.
265
   //
273
   //
266
 
274
 
267
   // Unlink 'p' from its parents and siblings.
275
   // Unlink 'p' from its parents and siblings.
276
+  // Asserts if parent is not a structure. 
277
+  // Returns 'p'.
268
   cmData_t* cmDataUnlink( cmData_t* p );
278
   cmData_t* cmDataUnlink( cmData_t* p );
269
 
279
 
270
-  unsigned  cmDataChildCount( cmData_t* p );
280
+  unsigned  cmDataChildCount( const cmData_t* p );
281
+
282
+  // Returns NULL if p has no children or index is invalid.
271
   cmData_t* cmDataChild( cmData_t* p, unsigned index );
283
   cmData_t* cmDataChild( cmData_t* p, unsigned index );
272
 
284
 
273
-  cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* parent );
274
-  cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* child );
275
-  cmData_t* cmDataInsertChild( cmData_t* parent, cmData_t* child, unsigned index );
285
+  // Prepend 'p' to 'parents' child list.
286
+  cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p );
287
+
288
+  // Append 'p' to the end of 'parent' child list.
289
+  cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p );
290
+
291
+  // Insert 'p' at index.  Index must be in the range: 
292
+  // 0 to cmDataChildCount(parent).
293
+  cmData_t* cmDataInsertChild( cmData_t* parent, cmData_t* p, unsigned index );
276
 
294
 
277
 
295
 
278
   //----------------------------------------------------------------------------
296
   //----------------------------------------------------------------------------

Loading…
Cancel
Save