GVGraphDefaultAttributes.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at http://www.graphviz.org/
  9. *************************************************************************/
  10. #import "GVGraphDefaultAttributes.h"
  11. #import "GVZGraph.h"
  12. @interface GVGraphDefaultAttributeKeyEnumerator : NSEnumerator
  13. {
  14. graph_t *_graph;
  15. int _kind;
  16. Agsym_t *_nextSymbol;
  17. }
  18. - (id)initWithGraphLoc:(graph_t *)graph prototype:(int)kind;
  19. - (NSArray *)allObjects;
  20. - (id)nextObject;
  21. @end
  22. @implementation GVGraphDefaultAttributeKeyEnumerator
  23. - (id)initWithGraphLoc:(graph_t *)graph prototype:(int)kind;
  24. {
  25. if (self = [super init]) {
  26. _kind = kind;
  27. _graph = graph;
  28. _nextSymbol = agnxtattr(_graph, _kind, NULL);
  29. }
  30. return self;
  31. }
  32. - (NSArray *)allObjects
  33. {
  34. NSMutableArray* all = [NSMutableArray array];
  35. for (; _nextSymbol; _nextSymbol = agnxtattr(_graph, _kind, _nextSymbol)) {
  36. char *attributeValue = _nextSymbol->defval;
  37. if (attributeValue && *attributeValue)
  38. [all addObject:[NSString stringWithUTF8String:attributeValue]];
  39. }
  40. return all;
  41. }
  42. - (id)nextObject
  43. {
  44. for (; _nextSymbol; _nextSymbol = agnxtattr(_graph, _kind, _nextSymbol)) {
  45. char *attributeValue = _nextSymbol->defval;
  46. if (attributeValue && *attributeValue)
  47. return [NSString stringWithUTF8String:attributeValue];
  48. }
  49. return nil;
  50. }
  51. @end
  52. @implementation GVGraphDefaultAttributes
  53. - (id)initWithGraph:(GVZGraph *)graph prototype:(int)kind
  54. {
  55. if (self = [super init]) {
  56. _graph = graph;
  57. _kind = kind;
  58. }
  59. return self;
  60. }
  61. - (NSUInteger)count
  62. {
  63. NSUInteger symbolCount = 0;
  64. Agsym_t *nextSymbol = NULL;
  65. for (nextSymbol = agnxtattr(_graph->_graph,_kind, nextSymbol); nextSymbol; nextSymbol = agnxtattr(_graph->_graph, _kind, nextSymbol))
  66. if (nextSymbol->defval && *(nextSymbol->defval))
  67. ++symbolCount;
  68. return symbolCount;
  69. }
  70. - (NSEnumerator *)keyEnumerator
  71. {
  72. return [[[GVGraphDefaultAttributeKeyEnumerator alloc] initWithGraphLoc:_graph->_graph prototype:_kind] autorelease];
  73. }
  74. - (id)objectForKey:(id)aKey
  75. {
  76. id object = nil;
  77. Agsym_t *attributeSymbol = agattr(_graph->_graph, _kind, (char*)[aKey UTF8String], 0);
  78. if (attributeSymbol) {
  79. char *attributeValue = attributeSymbol->defval;
  80. if (attributeValue && *attributeValue)
  81. object = [NSString stringWithUTF8String:attributeValue];
  82. }
  83. return object;
  84. }
  85. - (void)setObject:(id)anObject forKey:(id)aKey
  86. {
  87. agattr(_graph->_graph, _kind, (char *)[aKey UTF8String], (char *)[anObject UTF8String]);
  88. [_graph noteChanged:YES];
  89. }
  90. - (void)removeObjectForKey:(id)aKey
  91. {
  92. agattr(_graph->_graph, _kind, (char *)[aKey UTF8String], "");
  93. [_graph noteChanged:YES];
  94. }
  95. @end