Esempio n. 1
0
void short_path(int v, int u, int len, int hands)
{
    if (v == u)
    {
        if (len == min_path)
        {
            ++count;
            max_hands = max_hands > hands ? max_hands : hands;
        }
        else if (len < min_path)
        {
            count = 1;
            min_path = len;
            max_hands = hands;
        }
        return;
    }
    for (Node *p = map[v].first; p != NULL; p = p->next)
    {
        if (!mask[p->city])
        {
            mask[p->city] = true;
            short_path(p->city, u, len+p->len, hands+map[p->city].hands);
            mask[p->city] = false;
        }
    }
}
Esempio n. 2
0
 void set_target_tile(T p, T ratio,
         std::vector<std::vector<int>> terrain,
         std::vector< std::vector<int> > move_cost)
 {
     PathPt target = PathPt(round(p.x/ratio.x), round(p.y/ratio.y));
     short_path(terrain, move_cost, &target, ratio);
     // maybe I will allow this, but not right now
     assert(!path->empty());
     set_target_position(path->front()->get_as_position(ratio));
     final_position = p;
 }
Esempio n. 3
0
// Return short path name under W9x, else quoted or unquoted pathname
// if contains white space.
CString GetSafePathName(const CString &path, bool bQuoted)
{
    // Under Win9x, we need to rely on the short path name
    // else, autoexec.bat won't grok it. But what about WINME ?
    CString short_path(path);
    if (bQuoted) {
        if (strchr(path, ' ') && path[0] != '"') {
            short_path = CString("\"") + short_path + CString("\"");
        }
    }
    else if (!g_bIsWindowsNT) {
        char lpShortPathName[_MAX_PATH];
        if (GetShortPathName((LPCTSTR)path, lpShortPathName, sizeof(lpShortPathName)) != 0)
            short_path = lpShortPathName;
    }
    
    return short_path;
}
Esempio n. 4
0
int main()
{
    int c1, c2;
    int n, m;
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for (int i = 0; i < n; ++i)
        scanf("%d", &(map[i].hands));
    for (int i = 0; i < m; ++i)
    {
        int x, y, len;
        scanf("%d%d%d", &x, &y, &len);
        map[x].insert(y, len);
        map[y].insert(x, len);
    }
    
    short_path(c1, c2, 0, map[c1].hands);
    printf("%d %d\n", count, max_hands);
    return 0;
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
	AdjList *p;
	AdjMatrix *G;

	p = (AdjList *)malloc(sizeof(AdjList));

	p = Creat(p);

	show(p);

	G = init (G);

	G = trans(G, p);

	show_two(G);
//	show_result(p);

	short_path(G);

	return EXIT_SUCCESS;
}
Esempio n. 6
0
/************************************************************************
*									*
*  User has entered a filename.  If it is a directory, go into that     *
*  directory and list all directory files.  Otherwise, return to        *
*  the user function call.						*
*									*/
static void
file_execute_proc(void)
{
    static char markers[] = "/"; /* List of spurious symbols at end of names */
    char fullname[MAXPATHLEN];
    char filename[MAXPATHLEN];
    char *p;				/* Temp */
    char *q;
    char *uname;			/* user seleted filename */
    int i;
    struct stat statbuf;		/* status of a file */

    uname = (char *) xv_get(fhdl->filename, PANEL_VALUE);
    if (*uname == NULL){
	err_msg("No file name entered");
	return;
    }
    if (expand_filename(uname, filename) == NULL){
	err_msg("Can't expand filename");
	return;
    }

    // Strip off any trailing markers
    while ( (p=strpbrk(filename+strlen(filename)-1, markers))
	   && (p > filename))	// Don't remove everything
    {
	*p = 0;
    }

    if (*filename == '/'){
	// We have given a full path name
	strcpy(fullname, filename);
    }else{
	strcpy(fullname, dirname);
	strcat(fullname, "/");
	strcat(fullname, filename);
    }
    
    // Avoid uncontrolled pathname growth

    // Remove excess slashes ("//" is same as "/")
    while (p=strstr(fullname, "//")){
	// Move portion of string beyond first "/" down one place
	do{
	    *p = *(p+1);
	} while (*p++);
    }

    // Remove "/./" elements
    while (p=strstr(fullname, "/./")){
	// Move portion of string beyond "/." down two places
	do{
	    *p = *(p+2);
	} while (*p++);
    }

    // Remove trailing "/."
    i = strlen(fullname);
    if (strcmp(fullname+i-2, "/.") == 0){
	// Filename ends in "/."; cut it off
	fullname[i-2] = 0;
    }

    // Remove "/../" elements (along with preceding element)
    while (p=strstr(fullname, "/../")){
	q = p + 3;				// Point to following "/"
	while ( (*(--p) != '/') && (p > fullname) ); // Point to previous "/"
	do{
	    *p = *q++;			// Move following stuff down
	} while (*p++);
    }

    // Remove trailing "/.." (along with preceding element)
    i = strlen(fullname);
    if (strcmp(fullname+i-3, "/..") == 0){
	// Filename ends in "/.."; cut off last two elements
	for (i=0; (i<2) && (p=strrchr(fullname, '/')); i++){
	    *p = 0;
	}
    }

    // Make sure something is left of the name
    if ( *fullname == 0 ){
	strcpy(fullname, "/");
    }

    int staterr;
    if ((staterr=stat(fullname, &statbuf)) == 0
	&& (statbuf.st_mode & S_IFMT) == S_IFDIR)
    {
	strcpy(dirname, fullname);
	
	/* Get the files */
	if (file_listing(dirname) == NOT_OK){
	    err_msg("Error in file_listing");
	    return;
	}

	/* Update the directory message */
	show_dir_path(short_path(dirname));
	xv_set(fhdl->filename, PANEL_VALUE, "", NULL);

	/* Remember new default directory */
	if (curfinfo){
	    free(curfinfo->dirpath);
	    curfinfo->dirpath = strdup(dirname);
	}
    }else{
	if (curfinfo == NULL){
	    err_msg("No registered function");
	    return;
	}
	/* in this case we will only load/save one file */
	if (xv_get(fhdl->load_but, PANEL_SHOW_ITEM) == TRUE){
	    if (staterr){
		err_msg("Can't find file: %s", short_path(fullname));
		return;
	    }else if ((statbuf.st_mode & S_IFMT) != S_IFREG){
		err_msg("Error: Invalid file or directory.");
		return;
	    }else{
		if (curfinfo->loadfunc){
		    curfinfo->loadfunc(dirname, filename);
		}
	    }
	}else{
	    if (curfinfo->savefunc && (staterr||confirm_overwrite(filename))){
		curfinfo->savefunc(dirname, filename);
	    }
	}
    }
}
Esempio n. 7
0
/************************************************************************
*									*
*  User has entered part of the filename, and this routine tries to     *
*  complete a filename from the current directory.  If it finds more    *
*  than one filename to complete,  it will list them in the panel list
*  window.								*
*									*/
static void
file_complete_name(void)
{
    char fractname[128];		/* fraction user name */
    int fractnamlen;			/* fraction user name length */
    register struct dirent *dp; /* directory info pointer for each file */
    DIR *dirptr;			/* directory file pointer */
    struct stat statbuf;		/* status of a file */
    int nfile;				/* number of files */
    char *filename[MAX_FILE];		/* pointer to a filename */
    int i;				/* loop counter */

    (void) strcpy(fractname, (char *) xv_get(fhdl->filename, PANEL_VALUE));
    if ((fractnamlen = strlen(fractname)) == 0)
	return;

    /* open the directory */
    if ((dirptr = opendir(dirname)) == NULL){
	char dirbuf[MAXPATHLEN];

	err_msg("Cannot open %s", short_path(getcwd(dirbuf,MAXPATHLEN)));
	return;
    }
    nfile = 0;
    for (dp = readdir(dirptr); dp != NULL; dp = readdir(dirptr)){
	if (strncmp(dp->d_name, fractname, fractnamlen) == 0){
	    (void) stat(dp->d_name, &statbuf);
	    if ((statbuf.st_mode & S_IFMT) == S_IFDIR){
		if ((filename[nfile] = (char *) malloc(strlen(dp->d_name) + 2))
		    == NULL) {
		    PERROR("file_complte_name:malloc");
		    file_free(filename, nfile);
		    closedir(dirptr);
		    return;
		}
		(void) sprintf(filename[nfile], "%s/", dp->d_name);
	    }else{
		if ((filename[nfile] = strdup(dp->d_name)) == NULL){
		    PERROR("file_complte_name:strdup");
		    file_free(filename, nfile);
		    closedir(dirptr);
		    return;
		}
	    }
	    if (nfile++ == (MAX_FILE - 2)){
		err_msg("Only can list %d maximum files", MAX_FILE);
		break;
	    }
	}
    }

    if (nfile == 0){			/* NO such fractional filename */
	err_msg("No such fractional filename: %s", fractname);
	return;
    }
    if (nfile == 1){			/* We can complete filename */
	xv_set(fhdl->filename, PANEL_VALUE, filename[0], NULL);
    }else{				/* More than one filename found */
	char c0;			/* the first filename character */
	char ch;			/* the rest of the filename
					   character */

	/* Try to complete the fractional filename as many characters  */
	/* as possible.  The idea is to compare all the filenames      */
	/* starting at the position of the 'fractnamlen'.              */
	while (1){
	    /* A character to be checked (for the first filename) */
	    if ((c0 = *(filename[0] + fractnamlen)) == 0)
		break;

	    for (i = 1; i < nfile; i++){
		ch = *(filename[i] + fractnamlen);
		if ((ch == 0) || (ch != c0))
		    goto STOP_COMPLETE;
	    }

	    /* All filenames have the same character at 'fractnamlen' */
	    fractname[fractnamlen++] = c0;
	    fractname[fractnamlen] = 0;
	}

STOP_COMPLETE:

	/* Update the user fractional filename */
	xv_set(fhdl->filename, PANEL_VALUE, fractname, NULL);

	/* Update the panel list to all matching filenames */
	file_list_update(filename, nfile);
    }

    /* Free the filenames */
    file_free(filename, nfile);
}
Esempio n. 8
0
/************************************************************************
*									*
*  Pop up the filelist frame.						*
*  Note that "state" indicates the file-browser for loading or saving.	*
*  The "dir_path" indicates go to the specified directory.  If it is	*
*  NULL, use the default directory (which is kept in Fileinfo).		*
*  The title name will be shown in the title window. If it is NULL,	*
*  the title "File Browser" will be shown.				*
*									*/
void
filelist_win_show(long id,		/* which filelist */
		  Flist_state state,	/* FILELIST_LOAD, FILELIST_LOAD_ALL, */
					/* or FILELIST_SAVE */
		  char *dir_path,	/* destination directory */
		  char *title,		/* Title of file-browser */
		  char *button_name)	/* string to put in button */
{					
    Fileinfo *newcurfinfo;		/* new current curfinfo */
    char dest_dir[MAXPATHLEN];		/* destination directory to list */
    char temp[1024];			/* temporary buffer */

    /* Create filelist window if it is not created */
    if (fhdl->popup == NULL){
	file_create_win();
    }

    err_msg("");

    if (state == FILELIST_LOAD){
	xv_set(fhdl->load_but, PANEL_SHOW_ITEM, TRUE, NULL);
	xv_set(fhdl->load_all_but, PANEL_SHOW_ITEM, FALSE, NULL);
	xv_set(fhdl->save_but, PANEL_SHOW_ITEM, FALSE, NULL);
	xv_set(fhdl->filemsg, PANEL_LABEL_STRING, LOAD_FILE_MSG, NULL);
	/* Change the button name  */
	xv_set(fhdl->load_but, PANEL_LABEL_STRING,
	       button_name ? button_name : "Load", NULL);

    }else if (state == FILELIST_LOAD_ALL){
	xv_set(fhdl->load_but, PANEL_SHOW_ITEM, TRUE, NULL);
	xv_set(fhdl->load_all_but, PANEL_SHOW_ITEM, TRUE, NULL);
	xv_set(fhdl->save_but, PANEL_SHOW_ITEM, FALSE, NULL);
	xv_set(fhdl->filemsg, PANEL_LABEL_STRING, LOAD_FILE_MSG, NULL);
	strcpy(temp, button_name ? button_name : "Load");
	xv_set(fhdl->load_but, PANEL_LABEL_STRING, temp, NULL);
	xv_set(fhdl->load_all_but, PANEL_LABEL_STRING,
	       strcat(temp, " All"), NULL);
    }else if (state == FILELIST_SAVE){
	xv_set(fhdl->load_but, PANEL_SHOW_ITEM, FALSE, NULL);
	xv_set(fhdl->load_all_but, PANEL_SHOW_ITEM, FALSE, NULL);
	xv_set(fhdl->save_but, PANEL_SHOW_ITEM, TRUE, NULL);
	xv_set(fhdl->filemsg, PANEL_LABEL_STRING, SAVE_FILE_MSG, NULL);
	xv_set(fhdl->save_but, PANEL_LABEL_STRING,
	       button_name ? button_name : "Save", NULL);
    }else{
	STDERR_1("filelist_win_show:No known state: %d", state);
	return;
    }

    /* Change the title */
    xv_set(fhdl->popup, FRAME_LABEL, title ? title : "File Browser", NULL);

    /* Find the requested ID */
    for (newcurfinfo=finfoheader; newcurfinfo; newcurfinfo=newcurfinfo->next){
	if (newcurfinfo->id == id){
	    break;
	}
    }

    if (newcurfinfo == NULL){
	STDERR_1("filelist_win_show:No such ID registered: %d", id);
	return;
    }
    if (dir_path){
	(void) strcpy(dest_dir, dir_path);	/* Use requested directory */
    }else{
	(void) strcpy(dest_dir, newcurfinfo->dirpath);	/* Use default dir. */
    }

    strcpy(dirname, dest_dir);

    /* Listing the files */
    file_listing(dirname);

    /* Update the directory message */
    show_dir_path(short_path(dirname));

    /* Update the current curfinfo */
    curfinfo = newcurfinfo;

    xv_set(fhdl->popup,
	   FRAME_CMD_PUSHPIN_IN, TRUE,
	   XV_SHOW, TRUE,
	   NULL);
}
Esempio n. 9
0
/************************************************************************
*									*
*  Create filelist window.						*
*									*/
static
void file_create_win(void)
{
    int x_pos,
        y_pos;
    char *envhome;

    if (fhdl == NULL){
	STDERR("file_create_win:filelist handler has not been created");
	return;
    }
    (void) getcwd(dirname,MAXPATHLEN);

    /* Get the parent directory of user's home directory. Note that	 */
    /* it is a complete path name including the file-mount name.	 */
    /* The way we do this is to change directory to the login directory  */
    /* get the full-path name, and change it back to the current working */
    /* directory.							 */
    if (envhome = getenv("HOME")){
	int i;
	char temp[MAXPATHLEN];

	strcpy(temp, getenv("HOME"));
	/* Get rid of the login name.  Hence we have parent directory */
	/* of home directory.						*/
	i = strlen(temp);
	while (temp[i] != '/'){
	    i--;
	}
	temp[i] = 0;
	parent_login = strdup(temp);
    }else{
	parent_login = (char *)malloc(1);	/* allocate 1 memory */
	parent_login[0] = 0;
    }

    /* Set the default values */
    if (fhdl->filename_wd == 0){
	fhdl->filename_wd = 380;
    }
    if (fhdl->filename_num == 0){
	fhdl->filename_num = 10;
    }


    if ((fhdl->font = xv_find(fhdl->owner, FONT,
			      FONT_FAMILY, FONT_FAMILY_LUCIDA,
			      FONT_STYLE, FONT_STYLE_BOLD,
			      NULL))
	== NULL)
    {
	STDERR("file_create_win: FONT_FAMILY_LUCIDA");
    }

    /* Create pop-up frame */
    fhdl->popup = xv_create(fhdl->owner, FRAME_CMD,
			    FRAME_LABEL, "File Browser",
			    XV_X, fhdl->x,
			    XV_Y, fhdl->y,
			    XV_FONT, fhdl->font,
			    FRAME_SHOW_RESIZE_CORNER, TRUE,
			    NULL);

    /* Get a panel from pop-up frame */
    fhdl->panel = xv_get(fhdl->popup, FRAME_CMD_PANEL);

    /* Set the panel font */
    xv_set(fhdl->panel, XV_FONT, fhdl->font, NULL);

    x_pos = 12;
    y_pos = 6;
    fhdl->filemsg = xv_create(fhdl->panel, PANEL_MESSAGE,
			      XV_X, x_pos,
			      XV_Y, y_pos,
			      PANEL_LABEL_STRING, LOAD_FILE_MSG,
			      NULL);

    fhdl->load_but = xv_create(fhdl->panel, PANEL_BUTTON,
			       XV_X, x_pos + fhdl->filename_wd - 40,
			       XV_Y, y_pos,
			       PANEL_LABEL_STRING, "Load",
			       PANEL_CLIENT_DATA, FILELIST_LOAD,
			       PANEL_NOTIFY_PROC, file_action_proc,
			       NULL);

    fhdl->save_but = xv_create(fhdl->panel, PANEL_BUTTON,
			       XV_X, x_pos + fhdl->filename_wd - 40,
			       XV_Y, y_pos,
			       PANEL_LABEL_STRING, "Save",
			       PANEL_CLIENT_DATA, FILELIST_SAVE,
			       PANEL_NOTIFY_PROC, file_action_proc,
			       NULL);

    y_pos += (int) xv_get(fhdl->filemsg, XV_HEIGHT) + 8;
    fhdl->load_all_but = xv_create(fhdl->panel, PANEL_BUTTON,
				   XV_X, x_pos + fhdl->filename_wd - 40,
				   XV_Y, y_pos,
				   PANEL_LABEL_STRING, "Load All",
				   PANEL_CLIENT_DATA, FILELIST_LOAD_ALL,
				   PANEL_NOTIFY_PROC, file_action_proc,
				   NULL);

    y_pos += (int) xv_get(fhdl->filemsg, XV_HEIGHT) + 8;
    fhdl->filename = xv_create(fhdl->panel, PANEL_TEXT,
			       XV_X, x_pos,
			       XV_Y, y_pos,
			       PANEL_VALUE, "",
		PANEL_VALUE_DISPLAY_LENGTH, (int) (fhdl->filename_wd / 10),
			       PANEL_NOTIFY_LEVEL, PANEL_SPECIFIED,
			       PANEL_NOTIFY_STRING, "\t\r\t\033",
			       PANEL_NOTIFY_PROC, file_enter_proc,
			       NULL);

    y_pos += (int) xv_get(fhdl->filename, XV_HEIGHT) + 14;
    fhdl->dirmsg = xv_create(fhdl->panel, PANEL_MESSAGE,
			     XV_X, x_pos,
			     XV_Y, y_pos,
			     PANEL_LABEL_STRING, "",
			     NULL);
    show_dir_path(short_path(dirname));

    y_pos += (int) xv_get(fhdl->dirmsg, XV_HEIGHT) + 8;
    fhdl->filename_xpos = x_pos;
    fhdl->filename_ypos = y_pos;
    fhdl->dirlist = xv_create(fhdl->panel, PANEL_LIST,
			      XV_X, x_pos,
			      XV_Y, y_pos,
			      PANEL_LIST_WIDTH, fhdl->filename_wd,
			      PANEL_LIST_DISPLAY_ROWS, fhdl->filename_num,
			      PANEL_NOTIFY_PROC, file_select_proc,
			      NULL);

    y_pos += (int) xv_get(fhdl->dirlist, XV_HEIGHT) + 8;
    fhdl->msg = xv_create(fhdl->panel, PANEL_MESSAGE,
			  PANEL_LABEL_STRING, "Message",
			  XV_X, x_pos,
			  XV_Y, y_pos,
			  NULL);

    y_pos += (int) xv_get(fhdl->msg, XV_HEIGHT) + 8;
    fhdl->errmsg = xv_create(fhdl->panel, PANEL_MESSAGE,
			     PANEL_LABEL_STRING, "Error Message",
			     XV_X, x_pos,
			     XV_Y, y_pos,
			     NULL);

    window_fit(fhdl->panel);
    window_fit(fhdl->popup);
}
Esempio n. 10
0
/************************************************************************
*									*
*  Get all files from directory "dirpath" and insert them into panel	*
*  list.  All directory files will be appended by '/' at the end.	*
*  Return OK or NOT_OK.							*
*									*/
static int
file_listing(char *dirpath)
{
    register struct dirent *dp; /* directory info pointer for each file */
    DIR *dirptr;			/* directory file pointer */
    struct stat statbuf;		/* status of a file */
    int nfile;				/* number of files */
    char fullname[MAXPATHLEN];
    char *filename[MAX_FILE];		/* pointer to a filename */

    /* open the directory */
    if ((dirptr = opendir(dirpath)) == NULL){
	err_msg("Can't open %s", short_path(dirpath));
	return (NOT_OK);
    }
    /* Read files from directory.  Add '/' for a directory file */
    nfile = 0;
    for (dp = readdir(dirptr); dp != NULL; dp = readdir(dirptr)){
	/* Don't list "hidden" files */
	if (*dp->d_name != '.'){
	    strcpy(fullname, dirname);
	    strcat(fullname, "/");
	    strcat(fullname, dp->d_name);
	    (void) stat(fullname, &statbuf);
	    if ((statbuf.st_mode & S_IFMT) == S_IFDIR){
		if ((filename[nfile] = (char *) malloc(strlen(dp->d_name) + 2))
		    == NULL)
		{
		    PERROR("file_listing:malloc");
		    file_free(filename, nfile);
		    closedir(dirptr);
		    return (NOT_OK);
		}
		(void) sprintf(filename[nfile], "%s/", dp->d_name);
	    }else{
		if ((filename[nfile] = strdup(dp->d_name)) == NULL){
		    PERROR("file_listing:strdup");
		    file_free(filename, nfile);
		    closedir(dirptr);
		    return (NOT_OK);
		}
	    }

	    if (nfile++ == (MAX_FILE - 2)){
		err_msg("Only can list %d maximum files", MAX_FILE);
		break;
	    }
	}
    }

    closedir(dirptr);

    /* Add the "../" so that the user can move up a directory */
    if ((filename[nfile] = strdup("../")) == NULL){
	PERROR("file_listing:strdup");
	file_free(filename, nfile);
	closedir(dirptr);
	return (NOT_OK);
    }
    nfile++;

    file_list_update(filename, nfile);
    file_free(filename, nfile);

    return (OK);
}