[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Additions to InFile and ObjectLoader...
From: |
Ken Cline |
Subject: |
Additions to InFile and ObjectLoader... |
Date: |
Fri, 12 Sep 1997 10:02:26 -0400 (EDT) |
Hi all,
Here's a small method that might be useful to add to the
InFile class:
-(int) unGetWord: (char *) aWord {
int i;
if ( !aWord ) return 0;
for ( i = strlen( aWord ) - 1; i >= 0; i-- ) {
if ( [ self unGetChar: aWord[ i ] ] == 0 )
return 0;
}
return 1 ;
}
I added this to my InFile class because I was combining the
setup paramters for several objects into the same file. For
example my setup files look like:
@Begin Producer
agentName foo
...
@End Producer
@Begin Producer
agentName bar
...
@End Producer
@Begin Consumer
agentName foobar
...
@End Consumer
and so on...
In my model swarm, I read the "@Begin" and then the object
type (eg "Producer", "Consumer" etc.). Next, I move the
file pointer backwards two words so that it is pointing at
the "@Begin". Then, based on the object type, I call the
appropriate method to create a new instance and load that
new instance with the ObjectLoader.
I've also modified my objectloader so that if it encounters
an "@Begin" while loading an object, for example:
@Begin Producer
agentName foo
...
@Begin
methodname argument
methodname argument
...
@End
...
@End Producer
then it assumes everything between the inner "@Begin" and
"@End" are method names followed by arguments. I use the
`perform' method to call the appropriate method for the
object being loaded.
Here is my modification of the ObjectLoader:
-loadObject: anObject {
...
SEL aSEL;
...
if( (aString[0] == '@') &&
(aString[1] == 'E') &&
(aString[2] == 'n') &&
(aString[3] == 'd') ) {
[theFileObject skipLine] ; //Note: if this fails we don't care...
//(users may or may not have a newline
//character at the end of their last
//object specification (last line in file).
break ;
}
if( (aString[0] == '@') &&
(aString[1] == 'B') &&
(aString[2] == 'e') &&
(aString[3] == 'g') &&
(aString[4] == 'i') &&
(aString[5] == 'n') )
{
if(![theFileObject skipLine])
[self _crash_: anObject] ;
while(1) {
while(1){
if(!([theFileObject getChar: &aChar]))
[self _crash_: anObject] ;
if(aChar == '#'){
if(![theFileObject skipLine])
[self _crash_: anObject] ;
} else {
if(![theFileObject unGetChar: aChar])
[self _crash_: anObject] ;
break ;
}
}
if(![theFileObject getWord: aString])
[self _crash_: anObject] ;
if( (aString[0] == '@') &&
(aString[1] == 'E') &&
(aString[2] == 'n') &&
(aString[3] == 'd') ) {
if(![theFileObject skipLine])
[self _crash_: anObject] ;
break ;
}
aSEL = sel_get_uid( aString );
if( ![anObject respondsTo: aSEL] )
[self _crash_: anObject] ;
if(![theFileObject getWord: aString])
[self _crash_: anObject] ;
[ anObject perform: aSEL with: (id)aString ];
if(![theFileObject skipLine])
[self _crash_: anObject] ;
}
continue ;
}
aProbe = [aProbeMap getProbeForVariable: aString] ;
if( !aProbe )
[self _crash_: anObject] ;
...
return self ;
}
As you can see most of this is cut-n-paste of what was more
or less already there. I just chose the quick way to
implement this stuff.
Well, I hope these will be useful to some of y'all.
Ken.
PS: Oh yeah, I forgot to mention that I switched from
"@begin" and "@end" to "@Begin" and "@End", just so you
know. Maybe ObjectLoader could have some char *'s called
"beginString" and "endString" and could use strncasecmp to
compare the input to these values?
Something like:
char * beginString;
char * endString;
int n, m;
beginString = "@begin";
endString = "@end";
n = strlen( beginString );
m = strlen( endString );
if ( strlen(aString) >= n &&
strncasecmp(beginString, aString, n) == 0 ) {
...
}
if ( strlen(aString) >= m &&
strncasecmp(endString, aString, m) == 0 ) {
...
}
Whatca think?
_________________________________________________________
Ken Cline address@hidden
SAIC VOICE (410) 571-0413
Annapolis, MD FAX (301) 261-8427
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
==================================
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Additions to InFile and ObjectLoader...,
Ken Cline <=