Main Page   Modules   Data Structures   File List   Data Fields   Related Pages   Examples  

sample4.c

Fourth sample use case

This sample use case is much like sample3.c, however it shows off SeDaBl's I/O convenience functions.

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "sedabl.h"


#define die() do { sedabl_free(message); fprintf(stderr,"failed to create message block\n"); return NULL; } while (0)

sedabl_block *mkblk() {
  sedabl_block *message, *status, *substatus, *profile;

  /* create message, populate value sub-blocks */
  if (!(message = sedabl_new_listblock("message",NULL))) die();
  if (-1 == sedabl_add_str(message,"msgid","8hgGg33b-a_aj=+2")) die();
  if (-1 == sedabl_add_str(message,"svc","LMC")) die();
  if (-1 == sedabl_add_str(message,"op","LOGIN")) die();

    /* create status block, add to message */
    if (!(status = sedabl_new_listblock("status",NULL))) die();
    if (-1 == sedabl_add_block(message,status)) die();

      /* create first substatus, add to status, populate with values */
      if (!(substatus = sedabl_new_listblock(NULL,NULL))) die();
      if (-1 == sedabl_add_block(status,substatus)) die();
      if (-1 == sedabl_add_str(substatus,"reply","YES")) die();
      if (-1 == sedabl_add_str(substatus,"login","user1")) die();
      if (-1 == sedabl_add_str(substatus,"key","username1")) die();
        /* create profile block, add to substatus, populate */
        if (!(profile = sedabl_new_listblock("profile",NULL))) die();
        if (-1 == sedabl_add_block(substatus,profile)) die();
        if (-1 == sedabl_add_str(profile,"macs_name_space/macs/user/email","username1@yahoo.com")) die();

      /* create second substatus, add to status, populate with values */
      if (!(substatus = sedabl_new_listblock(NULL,NULL))) die();
      if (-1 == sedabl_add_block(status,substatus)) die();
      if (-1 == sedabl_add_str(substatus,"reply","NO")) die();
      if (-1 == sedabl_add_str(substatus,"login","user2")) die();

      /* create third substatus, add to status, populate with values */
      if (!(substatus = sedabl_new_listblock(NULL,NULL))) die();
      if (-1 == sedabl_add_block(status,substatus)) die();
      if (-1 == sedabl_add_str(substatus,"reply","YES")) die();
      if (-1 == sedabl_add_str(substatus,"login","user3")) die();
      if (-1 == sedabl_add_str(substatus,"key","username3")) die();
        /* create profile block, add to substatus, populate */
        if (!(profile = sedabl_new_listblock("profile",NULL))) die();
        if (-1 == sedabl_add_block(substatus,profile)) die();
        if (-1 == sedabl_add_str(profile,"macs_name_space/macs/user/email","username3@yahoo.com")) die();
  
  return message;
}

#define INDENT for (i=0; i < depth; i++) fputs("\t",stdout)

int showblk(sedabl_block *block,int depth) {
  int i;
  char f[256];

  if (!block) return 0; /* no block to display */
  INDENT; printf("Key:%s",block->key);
  if (block->is_list) {
    printf(", is a list:\n");
    for (i=0; i < block->payload.list->elements; i++) /* for each sub-block... */
      if (!showblk(block->payload.list->blocks[i],depth+1)) /* ...display it */
        return 0; /* couldn't display a sub-block */
    return 1;
  } else {
    printf(", is a value.  As string:\n"); INDENT;
    if (snprintf(f,256,"\t``%%.%ds''\n",block->payload.value->len) > 255)
      return 0; /* failed to create format string */
    printf(f,block->payload.value->val);
    return 1;
  }
}

int main() {
  sedabl_block *msg;
  int fd;
  
  if (!(msg = mkblk())) return 1;

  fd = open("/tmp/sedable.out",O_WRONLY|O_TRUNC|O_CREAT,0644);
  if (fd < 0) { perror("/tmp/sedabl.out for write"); return 1; }
  if (!sedabl_write(fd,msg)) { fprintf(stderr,"sedabl_write failed"); return 1; }
  close(fd);
  printf("Wrote the following block:\n");
  if (!showblk(msg,0)) return 1;
  sedabl_free(msg);

  fd = open("/tmp/sedable.out",O_RDONLY,0644);
  if (fd < 0) { perror("/tmp/sedabl.out for read"); return 1; }
  if (!(msg = sedabl_read(fd))) { fprintf(stderr,"sedabl_read failed"); return 1; }
  close(fd);
  printf("Read the following block:\n");
  if (!showblk(msg,0)) return 1;

  printf("\n\n****** Now multiple reads and writes...\n\n\n");

  fd = open("/tmp/sedable.out",O_WRONLY|O_TRUNC|O_CREAT,0644);
  if (fd < 0) { perror("/tmp/sedabl.out for write"); return 1; }
  if (!sedabl_write(fd,msg)) { fprintf(stderr,"sedabl_write failed"); return 1; }
  if (!sedabl_write(fd,msg)) { fprintf(stderr,"sedabl_write failed"); return 1; }
  if (!sedabl_write(fd,msg)) { fprintf(stderr,"sedabl_write failed"); return 1; }
  close(fd);
  printf("Wrote the following block:\n");
  if (!showblk(msg,0)) return 1;

  fd = open("/tmp/sedable.out",O_RDONLY,0644);
  if (fd < 0) { perror("/tmp/sedabl.out for read"); return 1; }
  if (!(msg = sedabl_read(fd))) { fprintf(stderr,"sedabl_read failed"); return 1; }
  sedabl_free(msg);
  if (!(msg = sedabl_read(fd))) { fprintf(stderr,"sedabl_read failed"); return 1; }
  sedabl_free(msg);
  if (!(msg = sedabl_read(fd))) { fprintf(stderr,"sedabl_read failed"); return 1; }
  close(fd);
  printf("Read the following block:\n");
  if (!showblk(msg,0)) return 1;
  sedabl_free(msg);

  return 0;
}

Generated on Mon Sep 15 07:27:33 2003 for SeDaBl C API by doxygen1.3-rc3