示例#1
0
文件: main.c 项目: unixdj/depcb
static int
play_add(void)
{
	PcbItem	**cur;

	if (find_action_item(&cur, 0)) {
		switch (pcb.action->act & PCB_ITEM) {
		case PCB_POINT:
			g_warning("invalid add point: %f %f\n",
			    pcb.action->c.x, pcb.action->c.y);
			break;
		case PCB_LINE:
			g_warning("invalid add line: %f %f %f %f\n",
			    pcb.action->c.x, pcb.action->c.y,
			    pcb.action->l.x, pcb.action->l.y);
			break;
		}
		return 0;
	}
	*cur = new_pcb_item(pcb);
	(*cur)->type = pcb.action->act & PCB_ITEM;
	(*cur)->layers = pcb.action->layers;
	(*cur)->flags = pcb.action->flags;
	switch ((*cur)->type) {
	case PCB_POINT:
		(*cur)->p = pcb.action->c;
		g_print("add point [%llx] (%f %f)\n", (*cur)->layers,
		    (*cur)->p.x, (*cur)->p.y);
		break;
	case PCB_LINE:
		if (((*cur)->l.point[0] =
		    find_point(pcb.action->c, pcb.action->layers)) &&
		    ((*cur)->l.point[1] =
		    find_point(pcb.action->l, pcb.action->layers))) {
			g_print("add line [%llx] (%f %f) (%f %f)\n",
			    (*cur)->layers,
			    (*cur)->l.point[0]->p.x, (*cur)->l.point[0]->p.y,
			    (*cur)->l.point[1]->p.x, (*cur)->l.point[1]->p.y);
			break;
		}
		g_warning("invalid line: %f %f %f %f\n", pcb.action->c.x,
		    pcb.action->c.y, pcb.action->l.x, pcb.action->l.y);
		/* FAlLTHRU */
	default:
		g_free(*cur);
		*cur = NULL;
		return 0;
	}
	show_item(*cur);
	return 1;
}
示例#2
0
文件: euler.c 项目: Botrix/mycode
void walk(int point, struct stack_t* stk, struct ALGraph* _alg)
{
	int idx = -1;
	struct point_t* next_point = NULL;

	struct ALGraph* alg = NULL;
	alg = (struct ALGraph*)malloc(sizeof(struct ALGraph));
	init_ALGraph(alg);
	cpy_ALGraph(alg, _alg);

	//fprintf(stdout, "\n--------------------\n");

	//print_ALGraph(alg);

	stack_push(stk, point);

	//print_stack(stk);

	/*是否全部走完*/
	if (if_walk_through(alg))
	{
		print_stack(stk);

		return;
	}
	
	idx = find_point(alg, point);

	if (-1 == idx)
	{
		fprintf(stderr, "find_point THIS CAN NOT HAPPEN!\n");
		exit(-1);
	}

	next_point = alg->point_array[idx].next;

	while (next_point != NULL)
	{
		idx = find_edge(alg, point, next_point->val);

		if (-1 == idx)
		{
			fprintf(stderr, "find_edge THIS CAN NOT HAPPEN!\n");
			exit(-1);
		}

		if (!alg->edge_array[idx].walk)
		{
			alg->edge_array[idx].walk = 1;

			walk(next_point->val, stk, alg);
			stack_pop(stk);
			alg->edge_array[idx].walk = 0;
		}

		next_point = next_point->next;
	}

	return;
}
示例#3
0
文件: trans.c 项目: yestab123/Tetris
//====================================================================
int go()
{

//	printf_full();
	find_point();
	if(!cannot_trans())
		trans();
	return 0;
}
示例#4
0
//立方体
mesh_cube::mesh_cube(ID3D11Device *device_need,ID3D11DeviceContext *contex_need):Geometry(device_need,contex_need)
{
	pancy_point *vertex_need = new pancy_point[100];
	UINT   *index_need = new UINT[100];  //索引数据
	find_point(vertex_need, index_need,all_vertex,all_index);
	init_point(vertex_need, index_need);
	delete[] vertex_need;
	delete[] index_need;
}
示例#5
0
//心形
mesh_heart::mesh_heart(ID3D11Device *device_need,ID3D11DeviceContext *contex_need,int circle_num_need,int vertex_percircle_need):Geometry(device_need,contex_need)
{
	heart_divide = circle_num_need;
	line_percircle = vertex_percircle_need;
	pancy_point *vertex_need = new pancy_point[circle_num_need*vertex_percircle_need + 100];
	UINT   *index_need = new UINT[circle_num_need*vertex_percircle_need * 6 + 100];  //索引数据
	find_point(vertex_need, index_need,all_vertex,all_index);
	init_point(vertex_need, index_need);
	delete[] vertex_need;
}
示例#6
0
/** \brief Move to given destination, check the map to detect any faulty path
  * \param nextPoint Next point coordinates
  * \param path Shortest path
  * \param current Pointer to current coordinates (will be updated)
  * \return 0 if we reached destination; 1 if the drone get lost; 2 if the drone drifted */
int move_to_next_point(struct coordinates_ nextPoint, const node_t **path, struct coordinates_ *current)
{
    int findy_lost = 0;
    float lastDistance, distance;

    distance = sqrt(pow(nextPoint.x-current->x,2) + pow(nextPoint.y-current->y,2));
    lastDistance = distance;
    while((distance > ERROR_COORD) && !findy_lost)
    {
        printf("Going to (%f, %f), currently at (%f, %f)\n", nextPoint.x, nextPoint.y, current->x, current->y);
        
        int j = 0;
        while (j < 6000) {
            pitch_move = FRONT;
            pitch_power = 0.15;
            SWITCH_DRONE_COMMANDE(4);
            j++;
        }
        break_drone();
        sleep(5);
        read_data_bluetooth(&(current->x),&(current->y));

        printf("Going to (%f, %f), currently at (%f, %f)\n", nextPoint.x, nextPoint.y, current->x, current->y);

        // Check if we're going in the wrong direction
        lastDistance = distance;
        distance = sqrt(pow(nextPoint.x-current->x,2) + pow(nextPoint.y-current->y,2));
        printf("Last distance: %f - Current distance: %f\n", lastDistance, distance);

        if (distance > lastDistance)
        {
            printf("We are farther than before...\n");

            // Check if we have drifted to another point
            if(find_point(graph, current->x, current->y) == -1)
            {
                //if((check = find_point_in_path(path, current.x, current.y)) == -1)
                printf("(%f, %f) is not accessible on the map\n", current->x, current->y);
                findy_lost = 1;
            }
            else
                findy_lost = 2;
        }
    }

    return findy_lost;
}
示例#7
0
int main( void )
{
	int i, j, c;
	unsigned long step;
	char matrix[MAX][MAX];
	char class;

	/*初始化随机数发生器、matrix矩阵、Site结构体*/
	srand((unsigned)time(0));
	Point Site;
	
	/*初始化矩阵matrix,填入顺序的数字标号*/
	printf("I'm initializing...\n");

	for(i = 0, class = CLASS; i < MAX; i++)
		for(j = 0; j < MAX; j++, class++)
			matrix[i][j]= class;
	matrix[MAX - 1][MAX - 1] = ' ';

	/*随机移动矩阵matrix中的数字标号,作为游戏起始状态*/
	for(step = 0; step < MAX_STEP; step++) {
		c = roll(4);
		Site = find_point( &matrix[0] );
		switch(c) {
		case 0:
			if((Site.y + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1];
				matrix[Site.x][Site.y + 1] = ' ';
			} break;
		case 1:
			if((Site.y - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1];
				matrix[Site.x][Site.y - 1] = ' ';
			} break;
		case 2:
			if((Site.x - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y];
				matrix[Site.x - 1][Site.y] = ' ';
			} break;
		case 3:
			if((Site.x + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y];
				matrix[Site.x + 1][Site.y] = ' ';
			} break;
		}		
	}
	printf_matrix( &matrix[0] );
	printf("I'm working...\n");

	/*游戏部分,从键盘接收命令,执行相应的动作,并打印出矩阵*/
	for(step = 0; is_win(&matrix[0]) != 1; step++) {
		c = my_getch();
		Site = find_point( &matrix[0] );
		switch(c) {
		case 'a':
			if((Site.y + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1];
				matrix[Site.x][Site.y + 1] = ' ';
			} break;
		case 'd':
			if((Site.y - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1];
				matrix[Site.x][Site.y - 1] = ' ';
			} break;
		case 's':
			if((Site.x - 1) >= 0) {
				matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y];
				matrix[Site.x - 1][Site.y] = ' ';
			} break;
		case 'w':
			if((Site.x + 1) < MAX) {
				matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y];
				matrix[Site.x + 1][Site.y] = ' ';
			} break;
		case 'Q':
			printf("Used %lu steps.\n", step);
			exit(0);
		}		
        system("clear");
		printf_matrix( &matrix[0] );
	    printf("Used %lu steps.\n", step);
	}

	/*打印出总共执行的步数step*/
    system("clear");
	printf_matrix( &matrix[0] );
	printf("I win!\n");
	printf("Used %lu steps.\n", step);
	return 0;
}
示例#8
0
void calcul_mission()
{
    struct coordinates_ C_blue; //coordinates of drone
    struct coordinates_ nextPoint;
    struct coordinates_ startPoint;
    mission_state_t state;
    int indice = 0;
    int index = 0;
    int bad_move = 0;
    float angle_actuel, calcul_x, calcul_y, angle_desire;
    node_t **path = NULL;

    C_blue.x = 0.0;
    C_blue.y = 0.0;


    // First of all, check if the given destination is valid
    if(find_point(graph, destination.x, destination.y) == -1)
    {
        printf("Invalid destination: (%.2f, %.2f)\n", destination.x, destination.y);
        state = FINISHED;
    }
    else
        state = INIT;

    while(state != FINISHED)
    {
        if(inC.flag_control_s == STATE_MANUAL)
        {
            state = FINISHED;
        }
        switch(state)
        {
            case INIT:

                printf("[Mission state: INIT]\n");

                // Wait for the data to stabilize
                sleep(5);
                read_data_bluetooth(&C_blue.x,&C_blue.y);

                printf("BT location: X = %f, Y = %f\n", C_blue.x, C_blue.y);
                printf("Destination point: (%f, %f)\n", destination.x, destination.y);

                path = dijkstra(C_blue.x, C_blue.y, destination.x, destination.y, graph);

                if (path == NULL)
                    state = LOST;
                else
                {
                    printf("Path to follow\n");
                    int i;
                    for(i = 0 ; path[i] != NULL ; i++)
                    {
                        indice = i;
                        printf("%s (%f,%f)\n", path[i]->name, path[i]->x, path[i]->y);
                    }
                    state = RUNNING;
                }

            break;

            case LOST:

                printf("[Mission state: LOST]\n");

                sleep(3);
                read_data_bluetooth(&C_blue.x,&C_blue.y);

                index = find_closest_node(graph, C_blue.x, C_blue.y);
                startPoint.x = graph->nodes[index].x;
                startPoint.y = graph->nodes[index].y;

                Main_Nav = return_navdata();

                angle_actuel = Main_Nav.magneto.heading_fusion_unwrapped;
                angle_desire = computeDesiredAngle(C_blue, startPoint);

                computeOffsetMag(&angle_desire, nav_prec, nav_suiv);
                yaw_power = computeDirection(angle_actuel, angle_desire, 0.3, &yaw_move);

                if(rotate_to_desired_angle(angle_desire) == -1)
                {
                    fprintf(stderr, "[%s:%d] Error: Rotation to desired angle failed\n", __FILE__, __LINE__);
                    break_drone();
                    stop_mission();
                    free(graph);
                    return;
                }

                // Let the drone stabilize itself
                sleep(1);

                bad_move = move_to(startPoint, &C_blue);
                break_drone();

                // If we find a start point, we can switch to state RUNNING
                if(!bad_move)
                {

                    printf("BT location: X = %f, Y = %f\n", C_blue.x, C_blue.y);
                    printf("Destination point: (%f, %f)\n", destination.x, destination.y);

                    path = dijkstra(C_blue.x, C_blue.y, destination.x, destination.y, graph);

                    if(path == NULL)
                    {
                        fprintf(stderr, "[%s:%d] Error: Bluetooth Data too unstable, aborting\n", __FILE__, __LINE__);
                        break_drone();
                        stop_mission();
                        free(graph);
                        return;
                    }

                    printf("Path to follow\n");
                    int i;
                    for(i = 0 ; path[i] != NULL ; i++)
                    {
                        indice = i;
                        printf("%s (%f,%f)\n", path[i]->name, path[i]->x, path[i]->y);
                    }
                    state = RUNNING;
                }

            break;

            case DRIFTED:

                printf("[Mission state: DRIFTED]\n");

                // Pretty much the same that LOST, at the difference that we think that we know where we are
                sleep(3);
                read_data_bluetooth(&C_blue.x,&C_blue.y);

                printf("BT location: X = %f, Y = %f\n", C_blue.x, C_blue.y);
                printf("Destination point: (%f, %f)\n", destination.x, destination.y);

                path = dijkstra(C_blue.x, C_blue.y, destination.x, destination.y, graph);

                if (path == NULL)
                {
                    // Mmh, Bluetooth data unstable, going to LOST
                    state = LOST;
                }
                else
                {
                    printf("New path to follow\n");
                    int i;
                    for(i = 0 ; path[i] != NULL ; i++)
                    {
                        indice = i;
                        printf("%s (%f,%f)\n", path[i]->name, path[i]->x, path[i]->y);
                    }
                    state = RUNNING;
                }


            break;

            case RUNNING:

                printf("[Mission state: RUNNING]\n");

                sleep(3);
                read_data_bluetooth(&C_blue.x,&C_blue.y);

                printf("BT location: X = %f, Y = %f\n", C_blue.x, C_blue.y);
                printf("Destination point: (%f, %f)\n", destination.x, destination.y);

                // Let's start
                indice--;

                if (indice <= 0)
                {
                    state = ARRIVED;
                }
                else
                {
                    // FIXME: Cast path[indice] from node_t to coordinates_ since we don't use the samee structure...
                    nextPoint.x = path[indice]->x;
                    nextPoint.y = path[indice]->y;

                    Main_Nav = return_navdata();
                    angle_actuel = Main_Nav.magneto.heading_fusion_unwrapped;
                    angle_desire = computeDesiredAngle(C_blue, nextPoint);

                    /*
                     * angle désiré
                     */

                    //calcul the new angle_desire
                    computeOffsetMag(&angle_desire, nav_prec, nav_suiv);

                    //printf("Calcul angle desire final v1 : %2.f\n", angle_desire);

                    //calculating the direction of rotation of the Z-axis for optimum positioning.
                    yaw_power = computeDirection(angle_actuel, angle_desire, 0.3, &yaw_move);
                    //printf("Valeur de la puissance mise : %1.f, Valeur de l'angle souhaité = %2.f, valeur de l'angle actuel = %2.f sens de rotation = %d\n", yaw_power, angle_desire, angle_actuel, yaw_move);

                    if (rotate_to_desired_angle(angle_desire) == -1)
                    {
                        break_drone();
                        stop_mission();
                        free(graph);
                        return;
                    }

                    // Let the drone stabilize itself
                    sleep(1);

                    // Let's move to next point
                    bad_move = move_to_next_point(nextPoint, (const node_t **) path, &C_blue);

                    // Stop the drone movement
                    break_drone();

                    if(bad_move == 1)
                        state = LOST;
                    else if(bad_move == 2)
                        state = DRIFTED;
                    else
                        printf("One step made\n");
                }

            break;

            case ARRIVED:
                printf("[Mission state: ARRIVED]\n");
                printf("You're arrived to destination\n");
                state = FINISHED;
            break;

            case FINISHED:
                // if stop mission called
            break;

            default:
                fprintf(stderr, "[%s:%d] Error: Unknown mission state\n", __FILE__, __LINE__);
                break_drone();
                stop_mission();
                free(graph);
                return;
            break;
        }
    }

    printf("[Mission state: FINISHED]\n");

    stop_mission();
    printf("fin mission\n");
    free(path);
}
示例#9
0
文件: extend.c 项目: Cavewhere/survex
int
main(int argc, char **argv)
{
   const char *fnm_in, *fnm_out;
   char *desc;
   img_point pt;
   int result;
   point *fr = NULL, *to;
   double zMax = -DBL_MAX;
   point *p;
   const char *survey = NULL;
   const char *specfile = NULL;
   img *pimg;
   int have_xsect = 0;

   msg_init(argv);

   /* TRANSLATORS: Part of extend --help */
   cmdline_set_syntax_message(/*INPUT_3D_FILE [OUTPUT_3D_FILE]*/267, 0, NULL);
   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
   while (1) {
      int opt = cmdline_getopt();
      if (opt == EOF) break;
      if (opt == 's') survey = optarg;
      if (opt == 'p') specfile = optarg;
   }
   fnm_in = argv[optind++];
   if (argv[optind]) {
      fnm_out = argv[optind];
   } else {
      char * base_in = base_from_fnm(fnm_in);
      char * base_out = osmalloc(strlen(base_in) + 8);
      strcpy(base_out, base_in);
      strcat(base_out, "_extend");
      fnm_out = add_ext(base_out, EXT_SVX_3D);
      osfree(base_in);
      osfree(base_out);
   }

   /* try to open image file, and check it has correct header */
   pimg = img_open_survey(fnm_in, survey);
   if (pimg == NULL) fatalerror(img_error2msg(img_error()), fnm_in);

   putnl();
   puts(msg(/*Reading in data - please wait…*/105));

   htab = osmalloc(ossizeof(pfx*) * HTAB_SIZE);
   {
       int i;
       for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
   }

   do {
      result = img_read_item(pimg, &pt);
      switch (result) {
      case img_MOVE:
	 fr = find_point(&pt);
	 break;
      case img_LINE:
	 if (!fr) {
	    result = img_BAD;
	    break;
	 }
	 to = find_point(&pt);
	 if (!(pimg->flags & (img_FLAG_SURFACE|img_FLAG_SPLAY)))
	    add_leg(fr, to, pimg->label, pimg->flags);
	 fr = to;
	 break;
      case img_LABEL:
	 to = find_point(&pt);
	 add_label(to, pimg->label, pimg->flags);
	 break;
      case img_BAD:
	 (void)img_close(pimg);
	 fatalerror(img_error2msg(img_error()), fnm_in);
	 break;
      case img_XSECT:
         have_xsect = 1;
         break;
      }
   } while (result != img_STOP);

   desc = osstrdup(pimg->title);

   if (specfile) {
      FILE *fs = NULL;
      char *fnm_used;
      /* TRANSLATORS: for extend: */
      printf(msg(/*Applying specfile: “%s”*/521), specfile);
      putnl();
      fs = fopenWithPthAndExt("", specfile, NULL, "r", &fnm_used);
      if (fs == NULL) fatalerror(/*Couldn’t open file “%s”*/93, specfile);
      while (!feof(fs)) {
	 char *lbuf = getline_alloc(fs, 32);
	 lineno++;
	 if (!lbuf)
	    fatalerror_in_file(fnm_used, lineno, /*Error reading file*/18);
	 parseconfigline(fnm_used, lbuf);
	 osfree(lbuf);
      }
      osfree(fnm_used);
   }

   if (start == NULL) { /* i.e. start wasn't specified in specfile */

      /* start at the highest entrance with some legs attached */
      for (p = headpoint.next; p != NULL; p = p->next) {
	 if (p->order > 0 && p->p.z > zMax) {
	    const stn *s;
	    for (s = p->stns; s; s = s->next) {
	       if (s->flags & img_SFLAG_ENTRANCE) {
		  start = p;
		  zMax = p->p.z;
		  break;
	       }
	    }
	 }
      }
      if (start == NULL) {
	 /* if no entrances with legs, start at the highest 1-node */
	 for (p = headpoint.next; p != NULL; p = p->next) {
	    if (p->order == 1 && p->p.z > zMax) {
	       start = p;
	       zMax = p->p.z;
	    }
	 }
	 /* of course we may have no 1-nodes... */
	 if (start == NULL) {
	    for (p = headpoint.next; p != NULL; p = p->next) {
	       if (p->order != 0 && p->p.z > zMax) {
		  start = p;
		  zMax = p->p.z;
	       }
	    }
	    if (start == NULL) {
	       /* There are no legs - just pick the highest station... */
	       for (p = headpoint.next; p != NULL; p = p->next) {
		  if (p->p.z > zMax) {
		     start = p;
		     zMax = p->p.z;
		  }
	       }
	       if (!start) fatalerror(/*No survey data*/43);
	    }
	 }
      }
   }

   /* TRANSLATORS: for extend:
    * Used to tell the user that a file is being written - %s is the filename
    */
   printf(msg(/*Writing %s…*/522), fnm_out);
   putnl();
   pimg_out = img_open_write(fnm_out, desc, img_FFLAG_EXTENDED);

   /* Only does single connected component currently. */
   do_stn(start, 0.0, NULL, ERIGHT, 0);

   if (have_xsect) {
      img_rewind(pimg);
      /* Read ahead on pimg before writing pimg_out so we find out if an
       * img_XSECT_END comes next. */
      char * label = NULL;
      int flags = 0;
      do {
	 result = img_read_item(pimg, &pt);
	 if (result == img_XSECT || result == img_XSECT_END) {
	    if (label) {
	       if (result == img_XSECT_END)
		  flags |= img_XFLAG_END;
	       img_write_item(pimg_out, img_XSECT, flags, label, 0, 0, 0);
	       osfree(label);
	       label = NULL;
	    }
	 }
	 if (result == img_XSECT) {
	    label = osstrdup(pimg->label);
	    flags = pimg->flags;
	    pimg_out->l = pimg->l;
	    pimg_out->r = pimg->r;
	    pimg_out->u = pimg->u;
	    pimg_out->d = pimg->d;
	 }
      } while (result != img_STOP);
   }

   (void)img_close(pimg);

   if (!img_close(pimg_out)) {
      (void)remove(fnm_out);
      fatalerror(img_error2msg(img_error()), fnm_out);
   }

   return EXIT_SUCCESS;
}
示例#10
0
文件: euler.c 项目: Botrix/mycode
static void create_ALGraph(struct ALGraph* alg, struct edge_t* edge_array, int edge_num)
{
	int i = 0;

	alg->point_array = (struct point_t*)malloc(edge_num * 2 * sizeof(struct point_t));
	if (NULL == alg->point_array)
	{
		fprintf(stdout, "malloc failed!\n");
	}

	alg->edge_array = (struct my_edge_t*)malloc(edge_num * sizeof(struct my_edge_t));
	if (NULL == alg->edge_array)
	{
		fprintf(stdout, "malloc failed!\n");
	}

	alg->point_num = 0;
	alg->edge_num = edge_num;
	
	/* 先搞定数组 */
	for (i = 0; i < edge_num; i++)
	{
		int left = edge_array[i].left;
		int right = edge_array[i].right;

		alg->edge_array[i].edge.left = edge_array[i].left;
		alg->edge_array[i].edge.right = edge_array[i].right;
		alg->edge_array[i].walk = 0;

		if (-1 == find_point(alg, left))
		{
			alg->point_array[alg->point_num].val = left;
			alg->point_array[alg->point_num].next = NULL;
			alg->point_num++;
		}

		if (-1 == find_point(alg, right))
		{
			alg->point_array[alg->point_num].val = right;
			alg->point_array[alg->point_num].next = NULL;
			alg->point_num++;
		}
	}

	/*从小到大排序*/
	qsort(alg->point_array, alg->point_num, sizeof(struct point_t), cmp_point);

	/*再搞定链表*/
	for (i = 0; i < edge_num; i++)
	{
		int left = edge_array[i].left;
		int right = edge_array[i].right;

		/*left*/
		int idx = find_point(alg, left);

		if (-1 == idx)
		{
			fprintf(stderr, "find_point failed!\n");
		}

		list_add_tail(alg->point_array + idx, right);

		/*right*/
		idx = find_point(alg, right);

		if (-1 == idx)
		{
			fprintf(stderr, "find_point failed!\n");
		}

		list_add_tail(alg->point_array + idx, left);
	}
}
示例#11
0
文件: aztecc.cpp 项目: tedher/aztecc
int main(int argc, char *argv[])
{
	timer t;

	t.set_print_mode(HMS_MODE);

	bencrypt=false;
	bdecrypt=false;
	bgen_pk=false;
	bfind_point=false;
	btest_point=false;
	bwipe_file=false;
	bhelp=false;
	bfile=false;
	buser=false;
	bversion=false;
	bks=false;

	for(int i=1;i<argc;i++)
	{
  		if((strcmp(argv[i],"-e")==0) || (strcmp(argv[i],"--encrypt")==0))
    	{
	      if (i+1 < argc)
				if(!is_option(argv[i+1]))
		  		{
		    		bencrypt=true;
		    		filename=argv[i+1];
				   bfile=true;
		    		i++;
		    		continue;
		  		}
			banner();
	      cerr<<"\nAZTECC ERROR : No filename given."<<endl;
	      exit(1);
	    }
	  
	  if((strcmp(argv[i],"-d")==0) || (strcmp(argv[i],"--decrypt")==0))
	  {
	  		if (i+1 < argc)
				if(!is_option(argv[i+1]))
		  		{
		    		bdecrypt=true;
		    		filename=argv[i+1];
		    		bfile=true;
		    		i++;
		    		continue;
		  		}
	      banner();
	      cerr<<"\nAZTECC ERROR : No filename given."<<endl;
	      exit(1);
    }
	  
	  if((strcmp(argv[i],"-g")==0) || (strcmp(argv[i],"--genkey")==0))
	    	bgen_pk=true;
	  
	  if((strcmp(argv[i],"-k")==0) || (strcmp(argv[i],"--keysize")==0))
	  {
	  		if (i+1 < argc)
				if(!is_option(argv[i+1]))
		  		{
		    		bks=true;
		    		keysize=argv[i+1];
		    		i++;
		    		continue;
		  		}
			banner();
	      cerr<<"\nAZTECC ERROR : No valid keysize given."<<endl;
	      exit(1);
	    }
	  
	  if((strcmp(argv[i],"-u")==0) || (strcmp(argv[i],"--user")==0))
     {
	  		if (i+1 < argc)
				if(!is_option(argv[i+1]))
		  		{
		    		username=argv[i+1];
		    		buser=true;
		    		i++;
		    		continue;
		  		}
			banner();
	      cerr<<"\nAZTECC ERROR : No valid username given."<<endl;
	      exit(1);
	  }
	  
	  if((strcmp(argv[i],"-pk")==0) || (strcmp(argv[i],"--pubkey")==0))
	  {
	  		if(!is_option(argv[i+1]))
			{
		  		pubkey=argv[i+1];
		  		i++;
		  		continue;
			}
			banner();
	      cerr<<"\nAZTECC ERROR : No public key filename given."<<endl;
	      exit(1);
	  }

		// find and test point
	  if((strcmp(argv[i],"-f")==0) || (strcmp(argv[i],"--find")==0))
	    bfind_point=true;
	  if((strcmp(argv[i],"-t")==0) || (strcmp(argv[i],"--test")==0))
	    btest_point=true;
	  
	  if((strcmp(argv[i],"-h")==0) || (strcmp(argv[i],"--help")==0))
	    bhelp=true;
	  if((strcmp(argv[i],"-v")==0) || (strcmp(argv[i],"--version")==0))
	    bversion=true;
	  if((strcmp(argv[i],"-w")==0) || (strcmp(argv[i],"--wipe")==0))
	  {
	  		if (i+1 < argc)
				if(!is_option(argv[i+1]))
		  		{
		    		filename=argv[i+1];
		    		bwipe_file=true;
		    		bfile=true;
		    		i++;
		    		continue;
		  		}
			banner();
	      cerr<<"\nAZTECC ERROR : No filename given."<<endl;
	      exit(1);
	    }
	}

	if((bencrypt) && (buser) && (bfile) && (!bdecrypt) && 
	   (!bgen_pk) && (!bfind_point) && (!btest_point) && 
	   (!bhelp) && (!bwipe_file) && (!bversion) && (!bks))
	{
		 t.start_timer();
	    encrypt_file(pubkey,username,filename);
		 t.stop_timer();
		 cout<<"\nElapsed time : "<<t<<endl;
	    exit(0);
	}
	
	if((bdecrypt) && (bfile) && (!buser) && (!bencrypt) && 
		(!bgen_pk) && (!bfind_point) && (!btest_point) &&
 		(!bhelp) && (!bwipe_file) && (!bversion) && (!bks))
	{
		 t.start_timer();
	    decrypt_file(pubkey,filename);
		 t.stop_timer();
		 cout<<"\nElapsed time : "<<t<<endl;
	    exit(0);
	}
	
	if((bgen_pk) && (bks) && (!buser) && (!bencrypt) && (!bdecrypt) && 
	   (!bfind_point) && (!btest_point) && (!bhelp) && (!bwipe_file) && (!bversion))
	{
	    if(strcmp(keysize,"160")==0)
	      blocksize=20;
	    else if(strcmp(keysize,"192")==0)
	      blocksize=24;
	    else
	    {
			banner();
			cerr<<"\nAZTECC ERROR : Keysize not valid.\n";
			exit(1);
	    }
	    
	    gen_pubkey(pubkey,blocksize);
	    exit(0);
	}
	
	if((bfind_point) && (!bencrypt) && (!bdecrypt) && (!bgen_pk) && 
	   (!btest_point) && (!bhelp) && (!bwipe_file) && (!bversion) &&
   	(!buser) && (!bks))
	{
			find_point();
			exit(0);
	}
	
	if((btest_point) && (!bencrypt) && (!bdecrypt) && (!bgen_pk) && 
		(!bfind_point) && (!bhelp) && (!bwipe_file) && (!bversion) &&
		(!buser) && (!bks))
	{
			test_point();
			exit(0);
	}

	if((bwipe_file) && (bfile) && (!bencrypt) && (!bdecrypt) && (!bgen_pk) && 
		(!bfind_point) && (!btest_point) && (!bhelp) && (!bversion) &&
     	(!buser) && (!bks))
	{
			wipe_file(filename);
			exit(0);
	}


	if((bversion) && (!bencrypt) && (!bdecrypt) && (!bgen_pk) && 
		(!bfind_point) && (!btest_point) && (!bhelp) && (!bwipe_file) &&
		(!buser) && (!bks))
	{
			version();
			exit(0);
	}

	if(bhelp)
	{
		usage();
		exit(0);
	}
  
  else
    usage();			
  return 0;
}