home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume17 / tcl-editor / part06 / anaObjects.c next >
Encoding:
C/C++ Source or Header  |  1992-03-18  |  10.7 KB  |  518 lines

  1. /* $Header: /nfs/unmvax/faculty/crowley/x/pt/RCS/anaObjects.c,v 1.3 1992/03/04 17:07:18 crowley Exp crowley $ */
  2.  
  3. #ifdef HYPERTEXT
  4. #include <sys/types.h>
  5. #include <sys/file.h>
  6. #include <string.h>
  7. #include "pt.h"
  8.  
  9. typedef struct object_table_tag {
  10.     char *name;
  11.     int size;
  12. } object_table_entry;
  13.  
  14. static object_table_entry ObjectTable[] = {
  15.     {"", 0},    /* dummy so we can avoid magic numbers of 0 */
  16.     {"Attribute", sizeof(AttributeStruct)},
  17.     {"Block", sizeof(BlockStruct)},
  18.     {"Document", sizeof(DocumentStruct)},
  19.     {"File", sizeof(FileStruct)},
  20.     {"Link", sizeof(LinkStruct)},
  21.     {"Map", sizeof(MapStruct)},
  22.     {"Marker", sizeof(BlockMarkerStruct)},
  23.     {"Text", sizeof(TextStruct)},
  24.     {"View", sizeof(ViewStruct)},
  25. };
  26.  
  27. DBM *
  28. OpenObjects(name)
  29.     String name;
  30. {
  31.     return dbm_open( name, O_RDWR | O_CREAT, 0644 );
  32. }
  33.  
  34. void
  35. CloseObjects( db )
  36.     DBM *db;
  37. {
  38.     dbm_close( db );
  39. }
  40.  
  41. void
  42. DumpDB( db )
  43.     DBM *db;
  44. {
  45.     datum d_out;
  46.     
  47.     d_out = dbm_firstkey( db );
  48.     while( d_out.dptr != NULL ) {
  49.         AnaObject object = (AnaObject)d_out.dptr;
  50.         printf("magic=%d id=%d next=%d name is %s\n",
  51.             object->magic, object->this_one,
  52.             object->next, object->name);
  53.         d_out = dbm_nextkey( db );
  54.     }
  55. }
  56.  
  57. AnaObject
  58. GetObject( db, magic, id, allocate )
  59.     DBM *db;
  60.     MagicNumber magic;
  61.     ID id;
  62.     AllocationMode allocate;
  63. {
  64.     datum d_in, d_out;
  65.     AnaObject object;
  66.  
  67.     d_in.dptr = (char *)&id;
  68.     d_in.dsize = sizeof(ID);
  69.     d_out = dbm_fetch( db, d_in );
  70.     if( d_out.dptr == NULL ) {
  71.         printf("ndbm ERROR in Get%s can't find id=%d\n",
  72.             ObjectTable[magic].name, id);
  73.         dbm_clearerr( db );
  74.         return NULL;
  75.     }
  76.     if( d_out.dsize != ObjectTable[magic].size ) {
  77.         printf("ndbm ERROR in Get%s id=%d, wrong size.",
  78.             ObjectTable[magic].name, id);
  79.         printf(" Size is %d and it should be %d\n", d_out.dsize,
  80.             ObjectTable[magic].size );
  81.         return NULL;
  82.     }
  83.     object = (AnaObject)d_out.dptr;
  84.     if( object->magic != magic ) {
  85.         printf("ndbm ERROR in Get%s id=%d, wrong magic number.",
  86.             ObjectTable[magic].name, id);
  87.         printf("Magic number is %d and it should be %d\n",
  88.             object->magic, magic);
  89.         return NULL;
  90.     }
  91.     if( allocate ) {
  92.         object = (AnaObject)PtMalloc( d_out.dsize, "ana object" );
  93.         /* copy d_out.dptr into object */
  94.         memcpy( (char *)object, (char *)d_out.dptr, d_out.dsize );
  95.     }
  96.     return object;
  97. }
  98.  
  99. void
  100. PutObject( db, magic, object, release )
  101.     DBM *db;
  102.     MagicNumber magic;
  103.     AnaObject object;
  104.     ReleaseMode release;
  105. {
  106.     datum d_key, d_object;
  107.     int ret;
  108.  
  109.     if( object == NULL )
  110.         return;
  111.     d_key.dptr = (char *)&(object->this_one);
  112.     d_key.dsize = sizeof(ID);
  113.     d_object.dptr = (char *)object;
  114.     d_object.dsize = ObjectTable[magic].size;
  115.     ret = dbm_store( db, d_key, d_object, DBM_REPLACE);
  116.     if( ret != 0 )
  117.         printf("ndbm ERROR in Put%s, ret=%d\n",
  118.             ObjectTable[magic].name, ret);
  119.     if( release )
  120.         PtFree( (char *)object );
  121. }
  122.  
  123. Block
  124. GetBlock( db, blockID, allocate )
  125.     DBM *db;
  126.     BlockID blockID;
  127.     AllocationMode allocate;
  128. {
  129.     return (Block)GetObject( db, BlockMagic, (ID)blockID, allocate );
  130. }
  131.  
  132. void
  133. PutBlock( db, block, release )
  134.     DBM *db;
  135.     Block block;
  136.     ReleaseMode release;
  137. {
  138.     PutObject( db, BlockMagic, (AnaObject)block, release );
  139. }
  140.  
  141. Attribute
  142. GetAttribute( db, attributeID, allocate )
  143.     DBM *db;
  144.     AttributeID attributeID;
  145.     AllocationMode allocate;
  146. {
  147.     return (Attribute)GetObject(db,AttributeMagic,(ID)attributeID,allocate);
  148. }
  149.  
  150. void
  151. PutAttribute( db, attribute, release )
  152.     DBM *db;
  153.     Attribute attribute;
  154.     ReleaseMode release;
  155. {
  156.     PutObject( db, AttributeMagic, (AnaObject)attribute, release );
  157. }
  158.  
  159. Map
  160. GetMap( db, mapID, allocate )
  161.     DBM *db;
  162.     MapID mapID;
  163.     AllocationMode allocate;
  164. {
  165.     return (Map)GetObject( db, MapMagic, (ID)mapID, allocate );
  166. }
  167.  
  168. void
  169. PutMap( db, map, release )
  170.     DBM *db;
  171.     Map map;
  172.     ReleaseMode release;
  173. {
  174.     PutObject( db, MapMagic, (AnaObject)map, release );
  175. }
  176.  
  177. File
  178. GetFile( db, fileID, allocate )
  179.     DBM *db;
  180.     FileID fileID;
  181.     AllocationMode allocate;
  182. {
  183.     return (File)GetObject( db, FileMagic, (ID)fileID, allocate );
  184. }
  185.  
  186. void
  187. PutFile( db, file, release )
  188.     DBM *db;
  189.     File file;
  190.     ReleaseMode release;
  191. {
  192.     PutObject( db, FileMagic, (AnaObject)file, release );
  193. }
  194.  
  195. Text
  196. GetText( db, textID, allocate )
  197.     DBM *db;
  198.     TextID textID;
  199.     AllocationMode allocate;
  200. {
  201.     return (Text)GetObject( db, TextMagic, (ID)textID, allocate );
  202. }
  203.  
  204. void
  205. PutText( db, text, release )
  206.     DBM *db;
  207.     Text text;
  208.     ReleaseMode release;
  209. {
  210.     PutObject( db, TextMagic, (AnaObject)text, release );
  211. }
  212.  
  213. Link
  214. GetLink( db, linkID, allocate )
  215.     DBM *db;
  216.     LinkID linkID;
  217.     AllocationMode allocate;
  218. {
  219.     return (Link)GetObject( db, LinkMagic, (ID)linkID, allocate );
  220. }
  221.  
  222. void
  223. PutLink( db, link, release )
  224.     DBM *db;
  225.     Link link;
  226.     ReleaseMode release;
  227. {
  228.     PutObject( db, LinkMagic, (AnaObject)link, release );
  229. }
  230.  
  231. View
  232. GetView( db, viewID, allocate )
  233.     DBM *db;
  234.     ViewID viewID;
  235.     AllocationMode allocate;
  236. {
  237.     return (View)GetObject( db, ViewMagic, (ID)viewID, allocate );
  238. }
  239.  
  240. void
  241. PutView( db, view, release )
  242.     DBM *db;
  243.     View view;
  244.     ReleaseMode release;
  245. {
  246.     PutObject( db, ViewMagic, (AnaObject)view, release );
  247. }
  248.  
  249. Document
  250. GetDocument( db, documentID, allocate )
  251.     DBM *db;
  252.     DocumentID documentID;
  253.     AllocationMode allocate;
  254. {
  255.     return (Document)GetObject(db, DocumentMagic, (ID)documentID, allocate);
  256. }
  257.  
  258. void
  259. PutDocument( db, document, release )
  260.     DBM *db;
  261.     Document document;
  262.     ReleaseMode release;
  263. {
  264.     PutObject( db, DocumentMagic, (AnaObject)document, release );
  265. }
  266.  
  267. Block
  268. CreateBlock( db, document, name, attribute, hint, file )
  269.     DBM *db;
  270.     Document document;
  271.     char *name;
  272.     AttributeID attribute;
  273.     Offset hint;
  274.     FileID file;
  275. {
  276.     Block block;
  277.     int i;
  278.  
  279.     block = (Block)PtMalloc( sizeof(BlockStruct), "ana block" );
  280.     block->magic = BlockMagic;
  281.     block->this_one = (document->nextFreeID)++;
  282.     block->next = document->firstBlock;
  283.     document->firstBlock = block->this_one;
  284.     strncpy( block->name, name, NAME_SIZE );
  285.     for( i = 1; i < MAX_ATTRIBUTES; ++i )
  286.          block->attribute[i] = NullObject;
  287.     block->attribute[0] = attribute;
  288.     block->hint = hint;
  289.     block->file = file;
  290.     block->numLinks = 0;
  291.     block->firstFromLink = NullObject;
  292.     block->firstToLink = NullObject;
  293.     PutBlock( db, block, NO_RELEASE );
  294.     return block;
  295. }
  296.  
  297. Attribute
  298. CreateAttribute( db, document, name )
  299.     DBM *db;
  300.     Document document;
  301.     char *name;
  302. {
  303.     Attribute attribute;
  304.  
  305.     attribute = (Attribute)PtMalloc( sizeof(AttributeStruct),
  306.                         "ana attribute" );
  307.     attribute->magic = AttributeMagic;
  308.     attribute->this_one = (document->nextFreeID)++;
  309.     attribute->next = document->firstAttribute;
  310.     document->firstAttribute = attribute->this_one;
  311.     strncpy( attribute->name, name, NAME_SIZE );
  312.     PutAttribute( db, attribute, NO_RELEASE );
  313.     return attribute;
  314. }
  315.  
  316. /*ARGSUSED*/
  317. AttributeID
  318. LookupAttributeByName( db, document, name )
  319.     DBM *db;
  320.     Document document;
  321.     char *name;
  322. {
  323.     extern DBM *currentDB;
  324.  
  325.     Attribute attribute;
  326.     int attributeID = document->firstAttribute;
  327.  
  328.     while( attributeID != NullObject ) {
  329.         attribute = GetAttribute( currentDB, attributeID, NO_ALLOCATE );
  330.         if( strcmp(name,attribute->name) == 0 )
  331.             return attributeID;
  332.         attributeID = attribute->next;
  333.     }
  334.     return NullObject;
  335. }
  336.  
  337. Map
  338. CreateMap( db, document, name )
  339.     DBM *db;
  340.     Document document;
  341.     char *name;
  342. {
  343.     Map map;
  344.     int i;
  345.  
  346.     map = (Map)PtMalloc( sizeof(MapStruct), "ana map" );
  347.     map->magic = MapMagic;
  348.     map->this_one = (document->nextFreeID)++;
  349.     map->next = document->firstMap;
  350.     document->firstMap = map->this_one;
  351.     strncpy( map->name, name, NAME_SIZE );
  352.     for( i = 0; i < MAP_SIZE; ++i ) {
  353.         map->domain[i] = NullObject;
  354.         map->range[i][0] = '\0';
  355.     }
  356.     PutMap( db, map, NO_RELEASE );
  357.     return map;
  358. }
  359.  
  360. /*ARGSUSED*/
  361. MapID
  362. LookupMapByName( db, document, name )
  363.     DBM *db;
  364.     Document document;
  365.     char *name;
  366. {
  367.     extern DBM *currentDB;
  368.  
  369.     Map map;
  370.     int mapID = document->firstMap;
  371.  
  372.     while( mapID != NullObject ) {
  373.         map = GetMap( currentDB, mapID, NO_ALLOCATE );
  374.         if( strcmp(name,map->name) == 0 )
  375.             return mapID;
  376.         mapID = map->next;
  377.     }
  378.     return NullObject;
  379. }
  380.  
  381. Link
  382. CreateLink( db, document, name, attribute, from, to )
  383.     DBM *db;
  384.     Document document;
  385.     char *name;
  386.     AttributeID attribute;
  387.     BlockID from, to;
  388. {
  389.     Link link;
  390.     int i;
  391.  
  392.     link = (Link)PtMalloc( sizeof(LinkStruct), "ana link" );
  393.     link->magic = LinkMagic;
  394.     link->this_one = (document->nextFreeID)++;
  395.     link->next = document->firstLink;
  396.     document->firstLink = link->this_one;
  397.     strncpy( link->name, name, NAME_SIZE );
  398.     for( i = 1; i < MAX_ATTRIBUTES; ++i )
  399.          link->attribute[i] = NullObject;
  400.     link->attribute[0] = attribute;
  401.     link->nextFromLink = NullObject;
  402.     link->from = from;
  403.     link->nextToLink = NullObject;
  404.     link->to = to;
  405.     PutLink( db, link, NO_RELEASE );
  406.     return link;
  407. }
  408.  
  409. File
  410. CreateFile( db, document, name )
  411.     DBM *db;
  412.     Document document;
  413.     char *name;
  414. {
  415.     File file;
  416.  
  417.     file = (File)PtMalloc( sizeof(FileStruct), "ana file" );
  418.     file->magic = FileMagic;
  419.     strncpy( file->name, name , NAME_SIZE );
  420.     file->this_one = (document->nextFreeID)++;
  421.     file->next = document->firstFile;
  422.     document->firstFile = file->this_one;
  423.     PutFile( db, file, NO_RELEASE );
  424.     return file;
  425. }
  426.  
  427. /*ARGSUSED*/
  428. FileID
  429. LookupFileByName( db, document, name )
  430.     DBM *db;
  431.     Document document;
  432.     char *name;
  433. {
  434.     extern DBM *currentDB;
  435.  
  436.     File file;
  437.     int fileID = document->firstFile;
  438.  
  439.     while( fileID != NullObject ) {
  440.         file = GetFile( currentDB, fileID, NO_ALLOCATE );
  441.         if( strcmp(name,file->name) == 0 )
  442.             return fileID;
  443.         fileID = file->next;
  444.     }
  445.     return NullObject;
  446. }
  447.  
  448. Text
  449. CreateText( db, document, s )
  450.     DBM *db;
  451.     Document document;
  452.     char *s;
  453. {
  454.     Text text;
  455.  
  456.     text = (Text)PtMalloc( sizeof(TextStruct), "ana text" );
  457.     text->magic = TextMagic;
  458.     text->this_one = (document->nextFreeID)++;
  459.     text->next = document->firstText;
  460.     document->firstText = text->this_one;
  461.     strncpy( text->s, s, NAME_SIZE );
  462.     PutText( db, text, NO_RELEASE );
  463.     return text;
  464. }
  465.  
  466. View
  467. CreateView( db, document, name, blockID, fromLinkMap, toLinkMap, blockMap )
  468.     DBM *db;
  469.     Document document;
  470.     char *name;
  471.     BlockID blockID;
  472.     MapID fromLinkMap, toLinkMap, blockMap;
  473. {
  474.     View view;
  475.  
  476.     view = (View)PtMalloc( sizeof(ViewStruct), "ana view" );
  477.     view->magic = ViewMagic;
  478.     view->this_one = (document->nextFreeID)++;
  479.     view->next = document->firstView;
  480.     document->firstView = view->this_one;
  481.     strncpy( view->name, name, NAME_SIZE );
  482.     view->blockID = blockID;
  483.     view->fromLinkMap = fromLinkMap;
  484.     view->toLinkMap = toLinkMap;
  485.     view->blockMap = blockMap;
  486.     PutView( db, view, NO_RELEASE );
  487.     return view;
  488. }
  489.  
  490. Document
  491. CreateDocument( db, name )
  492.     DBM *db;
  493.     char *name;
  494. {
  495.     Document document;
  496.  
  497.     document = (Document)PtMalloc( sizeof(DocumentStruct), "ana document" );
  498.     document->magic = DocumentMagic;
  499.     document->this_one = 1;
  500.     document->next = NullObject;
  501.     strncpy( document->name, name, NAME_SIZE );
  502.     /* start IDs at 100 so we can use low ids for conventional */
  503.     /* purposes, like open fileIds as IDs */
  504.     document->nextFreeID = 100;
  505.     document->initialView = NullObject;
  506.     document->firstBlock = NullObject;
  507.     document->firstAttribute = NullObject;
  508.     document->firstLink = NullObject;
  509.     document->firstFile = NullObject;
  510.     document->firstView = NullObject;
  511.     document->firstMap = NullObject;
  512.     document->firstText = NullObject;
  513.     PutDocument( db, document, NO_RELEASE );
  514.     return document;
  515. }
  516. #endif
  517.  
  518.