Example Rover Server Plug-In Module: Irolo.lib

The following Tcl code can be found in the cgi-bin/rover/irolo.lib file.


# Interactive rolodex program. Uses database format of rolo.
#
# Frans Kaashoek
# 12/14/95 
#
# Roverized: Anthony D. Joseph
# 1/3/96
#

#
# In addition to global variables below, irolo has a global variable "display"
# that stores the current rolodex entry. record is an array indexed by fields
# from fieldVariables. Finally, irolo maintains a global array record that
# contains all rolodex entries.
#


set fieldVariables {name wphone hphone company waddress haddress remarks}
set fixedFieldVariables {date}


#
# Code for manipulating a Rolodex object
#


class Rolo {ptr time} {
    set slot(time) $time
    return [$self unmarshall $ptr]
}	    


#
# Marshalled Rolo object format:
#	[]
#	   :
#	   :
#

method Rolo unmarshall {ptr} {
    set count 0
    while {[string length $ptr] > 0} {
	set next [string first "\n" $ptr]
	if {$next == -1} {
	    set next ""
	} else {
	    set current [string range $ptr 0 [expr $next - 1]]
	    set next [string range $ptr [expr $next + 1] end]
	    set ptr $current
	}
	set slot(Names,$count) $ptr
	incr count
	if {$next == ""} break
	set ptr $next
    }
    set slot(nrecord) $count
    return $self
}	    

method Rolo commitIdx {name {commit 0}} {
    Rover_AddLog $self $slot(time) "commit" "$self commitIdx $name 1"
    set found 0
    foreach i [lsort -integer [TwoDArrayNames slot Names]] {
	if [string match $name $slot(Names,$i)] {
	    set found 1
	}
    }

    if {$found == 0} {
	incr slot(nrecord)
	set count $slot(nrecord)
	set slot(Names,$count) $name
	return [$self marshall]
    }
    return ""
}

method Rolo marshall {} {
    set result ""
    foreach i [lsort [TwoDArrayNames slot Names]] {
	set result "${result}$slot(Names,$i)\n"
    }
    return $result
}	    
    

#
# Return the elts of a Rolo object
#

method Rolo elts {} {
    set value {}
    foreach i [lsort [TwoDArrayNames slot Names]] {
	lappend value $slot(Names,$i)
    }
    return $value
}



#
# Delete an entry in the rolodex.
#

method Rolo deleteIdx {name {commit 0}} {
    set found 0
    foreach i [lsort -integer [TwoDArrayNames slot Names]] {
	if [string match $name $slot(Names,$i)] {
	    set found 1
	    set slot(nrecord) [expr $slot(nrecord) - 1]
	    for {set j $i} {$j < $slot(nrecord)} {incr j} {
		set next [expr $j + 1]
		set slot(Names,$j) $slot(Names,$next)
	    }
	    unset slot(Names,$slot(nrecord))
	    if {$commit == 0} {
		set slot(dirty) 1
	    } else {
		set slot(dirty) 0
	    }
	}
    }
    Rover_AddLog $self $slot(time) "commit" "$self deleteIdx $name 1"
    if {$found == 1} {
	return [$self marshall]
    } else {
	return ""
    }
}




############################################################################
############################################################################
############################################################################

#
#
# Code for manipulating a Entry object
#


class Entry {ptr time} {
    set slot(time) $time
    return [$self unmarshall $ptr]
}	    


# Format:
#	[
#	 
#	   :
#	   :
#	 
#	 
#	   :
#	   :
#	 
    
# 
# Unmarshall a rolodex record.
#

method Entry unmarshall {ptr} {
    global fieldVariables fixedFieldVariables

    foreach field [concat $fieldVariables $fixedFieldVariables] {

	# puts stderr "ptr is $ptr"

	if {[string length $ptr] == 0} {
	    set slot($field) ""
	    continue
	}
	set next [string first "\n" $ptr]
	if {$next == -1} {
	    set next ""
	} else {
	    set current [string range $ptr 0 [expr $next - 1]]
	    set next [string range $ptr [expr $next + 1] end]
	    set ptr $current
	}
	set slot($field) $ptr
        set ptr $next
        if {$next == ""} break
    }
    

    # Check for user defined fields

    if {[string length $ptr] == 0} {
	set slot(nuser) 0
	return $self
    }

    set i 0
    set name 0
    while {[string length $ptr] != 0} {
	set next [string first "\n" $ptr]
	if {$next == -1} {
	    set next ""
	} else {
	    set current [string range $ptr 0 [expr $next - 1]]
	    set next [string range $ptr [expr $next + 1] end]
	    set ptr $current
	}
	if {$name == 0} {
	    set slot(UserFields,$i) $ptr
	    # Just in case the field itself is null
	    set slot($i) {}
	    incr name
	} else {
	    set name 0
	    set slot($i) $ptr
            incr i
        }
        if {$next == ""} break
        set ptr $next
    }
    if {$name != 0} {puts stderr "WARNING! Empty user field $i"}
    set slot(nuser) $i
    return $self
}
    
method Entry commitEntry {data} {
    $self unmarshall $data
    # Set date updated
    set slot(date) [Rover_CTime $slot(time)]
    set result [$self marshall]
    Rover_AddLog $self $slot(time) "commit" "$self commitEntry \{$result\}"
    return $result
}


#
# Marshall a rolodex entry
#

method Entry marshall {} {
    global fieldVariables fixedFieldVariables

    set result ""
    foreach field [concat $fieldVariables $fixedFieldVariables] {
	set result "${result}$slot($field)\n"
    }
    for {set i 0} {$i < $slot(nuser)} {incr i} {
	if [info exists slot($i)] {
	    set result "${result}$slot(UserFields,$i)\n$slot($i)\n"
	}
    }
    return [Rover_TclEscape $result]
}	    

method Entry deleteEntry {{commit 0}} {
    Rover_DbDelete $self
    Rover_AddLog $self $slot(time) "commit" "$self deleteEntry 1"
    return ""
}


Last updated by $Author: adj $ on $Date: 1997/12/01 23:41:39 $.
Copyright © 1995-1998 Anthony D. Joseph and Massachusetts Institute of Technology