wcalc  2.5
About: Wcalc is a natural-expression command-line calculator.
  Fossies Dox: wcalc-2.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

PersVarList.m
Go to the documentation of this file.
1 #include "string_manip.h"
2 #include "calculator.h"
3 #include "list.h"
4 #include "PersVarList.h"
5 
6 struct pers_var {
7  char *name;
8  char *exp;
9 };
10 
11 static List persVars = NULL;
12 
13 @implementation PersVarList
14 
15 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
16 {
17  return listLen(persVars);
18 }
19 
20 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn*)col row:(int)rowIndex
21 {
22  struct pers_var * cursor = peekListElement(persVars, rowIndex);
23 
24  if (! cursor) {
25  return @"BAD ROW";
26  }
27  if ([[col identifier] isEqualToString:@"value"]) {
28  return [NSString stringWithUTF8String:cursor->exp];
29  } else if ([[col identifier] isEqualToString:@"name"]) {
30  return [NSString stringWithUTF8String:(cursor->name)];
31  } else {
32  return @"BAD COLUMN";
33  }
34 }
35 
36 - (void)tableView:(NSTableView*)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn*)col row:(int)rowIndex
37 {
38  struct pers_var *cursor = peekListElement(persVars, rowIndex);
39  NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:listLen(persVars)];
40  ListIterator li = NULL;
41  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
42 
43  if ([[col identifier] isEqualToString:@"value"]) {
44  free(cursor->exp);
45  cursor->exp = strdup([anObject UTF8String]);
46  } else if ([[col identifier] isEqualToString:@"name"]) {
47  free(cursor->name);
48  cursor->name = strdup([anObject UTF8String]);
49  strstrip(' ',cursor->name);
50  }
52  while ((cursor = (struct pers_var *)nextListElement(li)) != NULL) {
53  [temp setObject:[NSString stringWithUTF8String:cursor->exp]
54  forKey:[NSString stringWithUTF8String:cursor->name]];
55  }
56  [prefs setObject:temp forKey:@"persistentVariables"];
57 }
58 
59 struct pers_var *getpersvar(char *key)
60 {
61  struct pers_var *cursor = NULL;
62  ListIterator li = NULL;
63 
64  if (!persVars || key == NULL || *key == 0 || listLen(persVars) == 0) {
65  return NULL;
66  }
67 
69  while ((cursor = (struct pers_var *)nextListElement(li)) != NULL) {
70  if (!strncmp(cursor->name, key, strlen(cursor->name)+1))
71  break;
72  else
73  cursor = NULL;
74  }
75  freeListIterator(li);
76  return cursor;
77 }
78 
79 int putpersvar(char *name, char *expr)
80 {
81  struct pers_var *cursor = NULL;
82 
83  if (name == NULL || *name == 0) {
84  return -1;
85  }
86  cursor = getpersvar(name);
87  if (!cursor) {
88  cursor = calloc(1, sizeof(struct pers_var));
89  addToList(&persVars, cursor);
90  }
91  if (!cursor->name) {
92  cursor->name = strdup(name);
93  } else if (cursor->exp) {
94  free(cursor->exp);
95  }
96  cursor->exp = strdup(expr);
97  return 0;
98 }
99 
100 int persvarexists(char* key)
101 {
102  struct pers_var *cursor = NULL;
103  ListIterator li = NULL;
104 
105  if (!persVars || !strlen(key) || listLen(persVars) == 0) {
106  return 0;
107  }
109  while ((cursor = (struct pers_var *)nextListElement(li)) != NULL) {
110  if (!strncmp(cursor->name, key, strlen(cursor->name) + 1))
111  break;
112  else
113  cursor = NULL;
114  }
115  freeListIterator(li);
116  return cursor ? 1 : 0;
117 }
118 
119 - (IBAction)addVar:(id)sender
120 {
121  char varname[20];
122  int i=1;
123 
124  sprintf(varname,"NewVariable%i",i);
125  while(persvarexists(varname)) {
126  ++i;
127  sprintf(varname,"NewVariable%i",i);
128  }
129  putpersvar(varname,"0");
130  [theList reloadData];
131 }
132 
133 - (IBAction)clearVars:(id)sender
134 {
135  while (listLen(persVars) > 0) {
137  }
138  [theList reloadData];
139 }
140 
141 - (IBAction)delVar:(id)sender
142 {
143  if ([theList selectedRow] > -1) {
144  getListElement(persVars,[theList selectedRow]);
145  [theList reloadData];
146  }
147 }
148 
149 - (IBAction)showList:(id)sender
150 {
151  if (persVars == NULL) {
152  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
153  NSDictionary *temp = [prefs dictionaryForKey:@"persistentVariables"];
154  NSEnumerator *enumerator = [temp keyEnumerator];
155  id key;
156 
157  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClose:) name:NSWindowWillCloseNotification object:theWindow];
158  while ((key = [enumerator nextObject])) {
159  putpersvar(strdup([key UTF8String]),strdup([[temp objectForKey:key] UTF8String]));
160  }
161  }
162  [theWindow makeKeyAndOrderFront:self];
163 }
164 
165 - (IBAction)onClose:(id)sender
166 {
167  struct pers_var *cursor = NULL;
168  NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:listLen(persVars)];
169  ListIterator li = NULL;
170  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
171 
172  [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:theWindow];
174  while ((cursor = (struct pers_var *)nextListElement(li)) != NULL) {
175  [temp setObject:[NSString stringWithUTF8String:cursor->exp]
176  forKey:[NSString stringWithUTF8String:cursor->name]];
177  }
178  [prefs setObject:temp forKey:@"persistentVariables"];
179  freeListIterator(li);
180 }
181 
182 @end
peekListElement
void * peekListElement(List, size_t)
Definition: list.c:284
_list_iterator
Definition: list.c:27
string_manip.h
calculator.h
strstrip
void strstrip(const char, char *)
Definition: string_manip.c:29
free
void free(void *)
listLen
size_t listLen(List)
Definition: list.c:352
PersVarList::theWindow
IBOutlet NSWindow * theWindow
Definition: PersVarList.h:9
_list
Definition: list.c:19
addToList
void addToList(List *, void *)
Definition: list.c:204
pers_var::exp
char * exp
Definition: PersVarList.m:8
persVars
static List persVars
Definition: PersVarList.m:11
getListIterator
ListIterator getListIterator(List)
Definition: list.c:362
nextListElement
void * nextListElement(ListIterator)
Definition: list.c:385
pers_var::name
char * name
Definition: PersVarList.m:7
freeListIterator
void freeListIterator(ListIterator)
Definition: list.c:406
PersVarList::theList
IBOutlet NSTableView * theList
Definition: PersVarList.h:8
PersVarList
Definition: PersVarList.h:6
list.h
PersVarList.h
pers_var
Definition: PersVarList.m:6
getListElement
void * getListElement(List, size_t)
Definition: list.c:305