コード例 #1
0
ファイル: main.c プロジェクト: Rubusch/c
void select_traverse(unsigned int* number_of_nodes)
{
  unsigned int select = 0;

  do{
    printf("\n\navailable traverse operations:\n");
    printf("0\tcancel\n");
    printf("1\ttraverse pre-order\n");
    printf("2\ttraverse post-order\n");
    printf("3\ttraverse in-order\n");
    readdigit(&select, "select an operation:");
    if(1 == select){
      //      int traverse_pre_order(unsigned int*);
      break;

    }else if(2 == select){
      //      int traverse_post_order(unsigned int*);
      break;

    }else if(3 == select){
      //      int traverse_in_order(unsigned int*);
      break;
    }
  }while(0 != select);
}
コード例 #2
0
ファイル: shm_attachdetach.c プロジェクト: Rubusch/c
static int ask()
{
  unsigned int response=0;
  do{
    response = 0;
    fprintf(stderr, "your options are:\n");
    fprintf(stderr, "0\texit\n");
    fprintf(stderr, "1\tshmat\n");
    fprintf(stderr, "2\tshmdt\n");
    fprintf(stderr, "3\tread from segment\n");
    fprintf(stderr, "4\twrite to segment\n");
    readdigit(&response, "enter the number corresponding to your choice");
  }while((0 > response) || (4 < response));
  return response;
}
コード例 #3
0
ファイル: main.c プロジェクト: Rubusch/c
void select_delete(unsigned int* number_of_nodes)
{
  if(0 == *number_of_nodes){
    puts("the tree is basically empty, first create a root node and some leafs to delete");
    return;
  }

  unsigned int select=0;
  do{
    printf("\n\navailable delete operations:\n");
    printf("0\tcancel\n");
    printf("1\tdelete a leaf\n");
    printf("2\tdelete a tree part\n");
    printf("3\tdelete a node\n");
    readdigit(&select, "select an operation:");
    if(1 == select){
      if(0 == *number_of_nodes){
	puts("the tree is empty, first create one before you like to delete the entire thing!");
      }else{
	if(0 > delete_tree(number_of_nodes)){
	  puts("failed.");
	}else{	  
	  printf("%d elements\ndone.\n", *number_of_nodes);
	}
      }
      break;
   
    }else if(2 == select){
      if(0 > delete_leaf(number_of_nodes)){
	puts("failed.");
      }else{
	printf("deleted.\n%d elements\ndone.\n", *number_of_nodes);
      }
      break;

    }else if(3 == select){
     if(0 > delete_element(number_of_nodes)){
	puts("failed.");
      }else{
	printf("deleted.\n%d elements\ndone.\n", *number_of_nodes);
      }
      break;
    }

  }while(0 != select);
}
コード例 #4
0
ファイル: luaformat.c プロジェクト: dansen/luacode
static const string * lex(FormatState * fs){
	//Ò»´Î¶ÁÈ¡Ò»¸ö×Ö·û´®
	while (fs->curpos+1 < fs->len) {
		unsigned char c = next();
		//ÊÇ·ñÊÇ¿Õ°×
		if (c == '_' || isalpha(c)){ //identifier
			return readidentifier(fs);
		}
		else if (isdigit(c)){	//Êý×Ö
			return readdigit(fs);
		}
		else if (c == '-'){
			if (peek() == '-'){ //×¢ÊÍ
				next();
				return readcomment(fs);
			}
			return readpunct(fs);
		} else if (c == '\"') {
			return readstring(fs);
		} else if (c == '\'') {
			return readstring(fs);
		} else if (c == '[') {
			if (peek() == '[') {
				next();
				return readstring(fs);
			}
			return readpunct(fs);
		}
		else if (ispunct(c)){ //±êµã
			return readpunct(fs);
		}
		else if (c == ' ' || c == '\t'){
			return readblank(fs);
		} else if (c == '\r' || c == '\n') {
			return readnewline(fs);
		}
		else {
			assert(0);
		}
		
	}
	return 0;
}
コード例 #5
0
ファイル: main.c プロジェクト: Rubusch/c
void select_insert(unsigned int* number_of_nodes)
{
  unsigned int select = 0;
  do{
    printf("\n\navailable insert options are:\n");
    printf("0\tcancel\n");
    printf("1\tpopulate\n");
    printf("2\tinsert an element\n");
    readdigit(&select, "select an operation:");

    if(1 == select){
      if(0 == *number_of_nodes){
	puts("the tree is empty, first create a tree root\nfailed.");
      }else if(1 <= *number_of_nodes){
	unsigned int number=0;
	do{
	  readnumber(&number, NUM_OF_NODES_DIGITS, "enter a number of nodes");
	}while((number < 0) || (number > MAX_NODES));
	populate_tree(number_of_nodes, number);
	printf("the tree was populated and now contains %d elements\ndone.\n", *number_of_nodes);
      }else{
	printf("%d elements\nfailed.\n", *number_of_nodes);
      }
      break;

    }else if(2 == select){
      if(0 == *number_of_nodes){
	puts("the tree is empty, first create a tree root");
      }else if(MAX_NODES == *number_of_nodes){
	printf("the tree is stuffed, it already contains %d nodes, (max. %d nodes possible)\n", *number_of_nodes, MAX_NODES);
      }else{
	puts("enter a data value for the new node");
	if(0 > add_element(number_of_nodes)){
	  puts("failed.");
	}else{
	  printf("%d elements\ndone.\n", *number_of_nodes);
	}    
      }
      break;
    }
  }while(0 != select);
}
コード例 #6
0
ファイル: main.c プロジェクト: Rubusch/c
void select_rotate(unsigned int* number_of_nodes)
{
  unsigned int select = 0;
  do{
    printf("\n\navailable rotate operations\n");
    printf("0\tcancel\n");
    printf("1\trotate left\n");
    printf("2\trotate right\n");
    readdigit(&select, "select an operation:");
    if(1 == select){
      //      int rotate_left(unsigned int*); 
      break;
    
    }else if(2 == select){
      //      int rotate_right(unsigned int*);
      break;
    
    }   
  }while(0 != select);
}
コード例 #7
0
ファイル: main.c プロジェクト: Rubusch/c
void select_copy(unsigned int* number_of_nodes)
{
  unsigned int select = 0;
  do{
    printf("\n\navailable copy operations:\n");
    printf("0\tcancel\n");
    printf("1\tcopy entire tree\n");
    printf("2\tcopy element from tree A to tree B\n");
    readdigit(&select, "select an operation:");
    if(1 == select){
      puts("TODO - not implemented"); // TODO
      //      int backup_tree(unsigned int*);
      break;

    }else if(2 == select){
      puts("TODO - not implemented"); // TODO
      //      int backup_element(unsigned int*);
      break;
    }
  }while(0 != select);
}
コード例 #8
0
ファイル: input_switch.c プロジェクト: Rubusch/c
int main(int argc, char** argv)
{
  puts("READ A DIGIT FOR A SWITCH CASE SITUATION");

  unsigned int run = 1;
  unsigned int val = 0;
  do{
    // init
    val = 0;

    // read
    readdigit(&val, "enter 1 or 2 ('0' to quit):");

    switch (val){
    case 0:
      puts("'0' - exiting");
      run = 0;
      break;

    case 1:
      puts("input was '1'");
      break;

    case 2:
      puts("input was '2'");
      break;

    default:
      puts("false input - try again");
      break;
    }

  }while(run);

  puts("READY.");
  exit(EXIT_SUCCESS);
}
コード例 #9
0
ファイル: shmget.c プロジェクト: Rubusch/c
int main(int argc, char** argv)
{
  // shm_key
  key_t shm_key;
  readlongnumber((unsigned long*) &shm_key, KEY_SIZE, "enter a key for the shared memory with up to 7 digits:");
  
  // size
  unsigned int size;
  readnumber(&size, KEY_SIZE, "enter a size of shared memory");

  // shm_flag
  int shm_flag;
  {
    unsigned int tmp_flag=0;
    do{
      tmp_flag = 0;

      puts("1\tIPC_CREAT");
      puts("2\tIPC_EXCL");
      puts("3\towner read");
      puts("4\towner write");
      puts("5\tgroup read");
      puts("6\tgroup write");
      puts("7\tother read");
      puts("8\tother write");

      readdigit(&tmp_flag, "set the flag:");

    }while( (1 > tmp_flag) || (8 < tmp_flag));
    
    switch (tmp_flag){

    case 1:
      puts("IPC_CREAT");
      shm_flag = IPC_CREAT;
      break;

    case 2:
      puts("IPC_EXCL");
      shm_flag = IPC_EXCL;
      break;

    case 3:
      fprintf(stderr, "owner read (%#8.8o)\n", 0400);
      shm_flag = 0400;
      break;

    case 4:
      fprintf(stderr, "owner write (%#8.8o)\n", 0200);
      shm_flag = 0200;
      break;

    case 5:
      fprintf(stderr, "group read (%#8.8o)\n", 040);
      shm_flag = 040;
      break;

    case 6:
      fprintf(stderr, "group write (%#8.8o)\n", 020);
      shm_flag = 020;
      break;

    case 7:
      fprintf(stderr, "other read (%#8.8o)\n", 04);
      shm_flag = 04;
      break;

    case 8:
      fprintf(stderr, "owner read (%#8.8o)\n", 02);
      shm_flag = 02;
      break;

    default:
      puts("try again");
      break;
    }
  }

  // shmget
  fprintf(stderr, "shmget(%ld, %d, %#o)\n", (long) shm_key, size, shm_flag);
  int shm_id;
  if(-1 == (shm_id = shmget(shm_key, size, shm_flag))){
    perror("shmget failed");
    exit(EXIT_FAILURE);
  }else{
    fprintf(stderr, "shmget returned %d\n", shm_id);
  }
  
  puts("READY.");
  exit(EXIT_SUCCESS);
}
コード例 #10
0
ファイル: main.c プロジェクト: Rubusch/c
/*
  user access

  arguments: print a tree with
  argv[1] = root
  argv[2] = number of elements
//*/
int main(int argc, char** argv)
{
  puts("common tree");

  unsigned int number_of_nodes=0;
  unsigned int select=0;

  /*  // debugging input
  init_root(&root, &number_of_nodes, 100);
  add_element(root, &number_of_nodes, 50);
  add_element(root, &number_of_nodes, 150);
  add_element(root, &number_of_nodes, 25);
  //  add_element(root, &number_of_nodes, 75);
  add_element(root, &number_of_nodes, 125);
  add_element(root, &number_of_nodes, 175);
  add_element(root, &number_of_nodes, 20);
  add_element(root, &number_of_nodes, 30);
  //  add_element(root, &number_of_nodes, 70);
  //  add_element(root, &number_of_nodes, 80);
  add_element(root, &number_of_nodes, 120);
  add_element(root, &number_of_nodes, 130);
  add_element(root, &number_of_nodes, 170);
  add_element(root, &number_of_nodes, 180);
  write_tree(root);
  quit(&root, &number_of_nodes);
  exit(EXIT_SUCCESS);
  //*/

  // use of command line arguments
  /*
  if(1 < argc){
    puts("..automated with command line arguments");
    if(3 == argc){
      // generate root
      fprintf(stderr, "tree %d root generated\n", atoi(argv[1]));
      init_root(&root, &number_of_nodes, atoi(argv[1]));      

      // populate
      fprintf(stderr, "tree with %d nodes generated\n", atoi(argv[2]));
      populate_tree(root, &number_of_nodes, atoi(argv[2]));      

      // write
      puts("write tree to file..");
      write_tree(root);
      puts("done.");
      
      // deleteall
      quit(&root, &number_of_nodes);
    }else{
      puts("Ooups, try:");
      fprintf(stderr, "%s <root data> <number of nodes>\n", argv[0]);
      exit(EXIT_FAILURE);
    }
    puts("\n");
    exit(EXIT_SUCCESS);
  }
  //*/

  // use of looped front end
  do{
    printf("\n\navailable operations:\n");
    printf("0\tquit\n");
    printf("1\tgenerate root\n");
    printf("2\tinsert\n");
    printf("3\tdelete\n");
    printf("4\tfind\n");
    printf("5\tcopy\n");
    printf("6\ttraverse\n");
    printf("7\trotate\n");
    printf("8\tshow number of nodes\n");
    printf("9\tprint tree to file\n");
    readdigit(&select, "select an operation:");

    if(0 == select){ // exit
      puts("-> QUIT");
      select_quit(&number_of_nodes);
      break;
      
    }else if(1 == select){ // generate root
      puts("-> GENERATE ROOT");
      select_generate_root(&number_of_nodes);

    }else if(2 == select){ // insert a node
      puts("-> INSERT");
      select_insert(&number_of_nodes);
      
    }else if(3 == select){ // delete
      puts("-> DELETE");
      select_delete(&number_of_nodes);

    }else if(4 == select){ // find
      puts("-> FIND ELEMENT");
      select_find(&number_of_nodes);

    }else if(5 == select){ // copy
      puts("-> COPY");
      select_copy(&number_of_nodes);

    }else if(6 == select){ // traverse
      puts("-> TRAVERSE");
      select_traverse(&number_of_nodes);

    }else if(7 == select){ // rotate
      puts("-> ROTATE");
      select_rotate(&number_of_nodes);

    }else if(8 == select){ // number of elements
      puts("-> NUMBER OF ELEMENTS"); 
      select_number_of_nodes(&number_of_nodes);

    }else if(9 == select){ // print to file
      puts("-> PRINT TO FILE");
      select_print(&number_of_nodes);
    }
  }while(1);

  if(0 != number_of_nodes){
    delete_tree(&number_of_nodes);
  }
  puts("READY.");
  exit(EXIT_SUCCESS);
}
コード例 #11
0
ファイル: shm_attachdetach.c プロジェクト: Rubusch/c
int main(int argc, char** argv)
{
  // in case generate shared memory to pointer area_addr
  // address work area
  char *area_addr = NULL; 

  // work area
  register int area_id=0;

  // ptr to current state entry
  register struct state *pState=NULL;

  // SIGSEGV state hold area func ptr
  void (*savefunc) ();

  // action to be performed
  register int action=0;
  while(0 != (action = ask())){
    if(0 < nap){
      fprintf(stderr, "currently attached segments");
      fprintf(stderr, " shm_id address\n");
      fprintf(stderr, "------- ---------\n");
      pState = &attached_segments[nap+1];
      while(--pState != attached_segments){
	fprintf(stderr, "%6d", pState->shm_id);
	fprintf(stderr, "%#11x", (unsigned int) pState->shm_addr);
	fprintf(stderr, " Read%s\n", (pState->shm_flag & SHM_RDONLY) ? "-Only" : "/Write");	
      }
      fprintf(stderr, "\n");

    }else{
      fprintf(stderr, "no segments are currently attached\n");      
      switch(action){
      case 1:
	// 1 - verify that there is space for another attach
	if(nap == MAXnap){
	  fprintf(stderr, "this simple example will only allow %d attached segments\n", MAXnap);
	  break;
	}
	// get the arguments, make the call, report the result and update the current state array
	pState = &attached_segments[++nap];

	readnumber(&pState->shm_id, ID_DIGITS, "enter shm_id of segment to attach:");
	readstring(pState->shm_addr, ADDR_DIGITS, "enter shm_addr");
	fprintf(stderr, "meaningful shm_flag vlaues are:\n");
	fprintf(stderr, "1\tSHM_RDONLY (%d)\n", SHM_RDONLY);
	fprintf(stderr, "2\tSHM_RND (%d)\n", SHM_RND);
	unsigned int select=0;
	unsigned int askagain=0;
	do{
	  askagain=0;
	  readdigit(&select, "select a shm_flag");
	  switch(select){
	  case 1:
	    puts("SHM_RDONLY");
	    pState->shm_flag = SHM_RDONLY;
	    break;

	  case 2:
	    puts("SHM_RND");
	    pState->shm_flag = SHM_RND;
	    break;

	  default:
	    puts("wrong input - try again");
	    askagain = 1;
	    break;
	  }
	}while(askagain);
	fprintf(stderr, "now attach the segment - shmat(%d, %#x, %#o)\n"
		, pState->shm_id, (unsigned int) pState->shm_addr, pState->shm_flag);

	if(0 > (pState->shm_addr = shmat(pState->shm_id, pState->shm_addr, pState->shm_flag))){
	  perror("shmat() failed");
	  --nap; // decrement, because already incremented above, for new pointer element!
	}else{
	  fprintf(stderr, "shmat returned %#8.8x\n", (unsigned int) pState->shm_addr);
	}
	break;
	
      case 2:
	// 2 - shmdt requested
	/*
	  get the address, make the call, report the results and make the 
	  internal state match
	//*/
	readstring(area_addr, ADDR_DIGITS, "enter detach shm_addr");
     	if(-1 == (area_id = shmdt(area_addr))){
	  perror("shmdt failed");
	}else{
	  fprintf(stderr, "shmdt returned %d\n", area_id);
	  for(pState = attached_segments, area_id = nap; --area_id; ++pState){
	    if(0 != (pState->shm_addr = area_addr)) 
	      *pState = attached_segments[--nap];
	  }
	}	
	break;
	
      case 3:
	if(nap == 0) break;
	
	readstring(area_addr, ADDR_DIGITS, "enter address of an attached segment");
	if(good_addr(area_addr))
	  fprintf(stderr, "string @ %#x is '%s'\n",(unsigned int) area_addr, area_addr);
	break;

      case 4:
	if(nap == 0) break;
	readstring(area_addr, ADDR_DIGITS, "enter address of an attached segment");
	
	/*
	  set up SIGSEGV signal handler to trap attempts to write into a read-only 
	  attached segment
	//*/
	savefunc = signal(SIGSEGV, signalhandler);

	if(setjmp(segvbuf)){
	  fprintf(stderr, "SIGSEGV signal caught: write aborted\n");
	  fflush(stdin); 
	  signal(SIGSEGV, savefunc);
	}else{
	  if(good_addr(area_addr)){
	    fflush(stdin);
	    readstring(area_addr, AREA_DIGITS, "enter one line to e copied to the shared segment attached");
	    // TODO: check
	    if(NULL == strncpy(pState->shm_addr, (char*) -1, sizeof(char*))){
	      perror("strncpy to shared memory failed");
	      break;
	    }
	  }
	}
	break;
      }
    }
  }

  // not reached
  puts("READY.");
  exit(EXIT_SUCCESS);
}