Exemple #1
0
bool	BVHParser::parse( const char * path )
{
	release();
	
	fileName = getFilenameFromPath(path);
	
	Buffer buf;
	CM_TEST( buf.fromFile(path) );
	
	std::string str;
	buf.getToken(str);
	
	// parse hierarchy
	
	if( str!="HIERARCHY" )
	{
		return false;
	}
	
	buf.getToken(str); // root node
	_root = parseNode(buf,0);
	
	if( _root == 0 )
		return false;
		
	TEST_TRUE( parseMotion( buf ) );
	
	return true;
}
Exemple #2
0
int
get_file( IOBuf * aws_buf, char *name ) {
  char * filename;
  filename = getFilenameFromPath(name);

  int rv = s3_get(aws_buf, filename);
  if(rv == 0 && aws_buf->code == 200 && aws_buf->len != 0) {
    FILE  * fp;
    char writebuf[BUF_SIZE];
    
    // Write out the downloaded file
    // Check if file exists
    if( (fp = fopen(name, "rb")) != NULL) {
      fprintf(stdout, "WARNING: The specified file already exist. \n"
                      "Refuse to overwrite. \n");
      fclose(fp);
      return -1;
    }
    // File doesn't exist yet. Okay to write :)
    else {
      if( (fp = fopen(name, "w+")) == NULL) {
        fprintf(stdout, "ERROR: Unable to create the specified file. \n");
        return -1;
      }
      int n, sz;
      while( ( sz = aws_iobuf_getdata ( aws_buf, writebuf, sizeof(writebuf))) != 0 ) {
        n = fwrite(writebuf, sizeof(unsigned char), sz, fp);
        if(n != sz) {
            fprintf(stdout, "ERROR: Error writing to file. \n");
            return -1;
          }
      }
      fclose(fp);
    }
  }
  return rv;
}
Exemple #3
0
int
put_file( IOBuf * aws_buf, char *name ) {
  FILE  * fp;
  char readbuf[BUF_SIZE];
  char * filename;
  
  // Read in file to upload
  if( (fp = fopen(name, "rb")) == NULL) {
      fprintf(stdout, "ERROR: The specified file doesn't exist. \n");
      return -1;
    }
  else {
    int n;
    while( !feof(fp) ) {
      n = fread(readbuf, sizeof(unsigned char), BUF_SIZE, fp);
      if(n != BUF_SIZE) {
        if(feof(fp)) {
          ; // Do Nothing
        }
        else {
          fprintf(stdout, "ERROR: Error reading from file. \n");
          return -1;
        }
      }
      aws_iobuf_append ( aws_buf, readbuf, n);
    }
    fclose(fp);
  }
  
  // Strip the full path from the name and replace spaces with %20
  filename = getFilenameFromPath(name);
  
  int rv = s3_put(aws_buf, filename);
  
  free(filename);
  return rv;
}
Exemple #4
0
/*
	FileOpen: Handles a request to open a file. (Either by menu or generated by the app.)

	Parameters:
	HWND sheet: Handle to the property sheet.
	bool ask:	Should AOKTS ask the user which file?
	int recent:	Optionally open from one of the recent file entries. (-1 to disable.)
*/
void FileOpen(HWND sheet, bool ask, int recent)
{
	OPENFILENAME ofn;
	struct RecentFile *file = NULL;	//the file info will be stored here one way or another
	char titleBuffer[100];
	const char *filename;
	Game version = scen.game;

	HWND page = PropSheet_GetCurrentPageHwnd(sheet);

	//save the scenario if changes have been made (NOT FUNCTIONAL)
	if (scen.needsave())
	{
		int sel = MessageBox(sheet, askSaveChanges, "Save", MB_YESNOCANCEL);

		if (sel == IDYES)
			FileSave(sheet, false, true);

		else if (sel == IDCANCEL)
			return;	//stop closing
	}

    // Hint about whether to open as AOC or SGWB
	if (setts.recent_first) {
	     scen.game = (Game)setts.recent_first->game;
	}

	/* Using a recent file... */
	if (recent >= 0)
	{
		ofn.Flags = 0;	//make sure no random flags set
		file = setts.recent_first;

		/* Parse the linked list to find the one we want */
		while (recent--)
		{
			if (file)
				file = file->next;
			else
			{
				MessageBox(sheet,
					"Warning: Recent File open failed.",
					"Open Warning", MB_ICONWARNING);
			}
		}

		strcpy(setts.ScenPath, file->path);
		version = (Game)file->game;
	}
	/* Prompt the user for a filename. */
	else if (ask)
	{
		struct RecentFile *r_parse;
		char dir[_MAX_PATH];
		strcpy(dir, setts.BasePath);
		strcat(dir, "Scenario");

		ofn.lStructSize =	sizeof(OPENFILENAME);
		ofn.hwndOwner =		sheet;
		//ofn.hInstance unused
		ofn.lpstrFilter =	extOpen;
		ofn.lpstrCustomFilter = NULL;	//user should not set custom filters
		ofn.lpstrFile =		setts.ScenPath;
		ofn.nMaxFile =		_MAX_PATH;
		ofn.lpstrFileTitle = NULL;
		ofn.lpstrInitialDir = dir;
		ofn.lpstrTitle =	NULL;
		ofn.Flags =			OFN_FILEMUSTEXIST | OFN_NONETWORKBUTTON | OFN_NOCHANGEDIR;

		if (scen.header.header_type == HT_AOE2SCENARIO) {
		    ofn.nFilterIndex =	1;
		    ofn.lpstrDefExt =	"aoe2scenario";
		} else {
		    switch (scen.game) {
		    case AOK:
		    case AOC:
		    case AOHD:
		    case AOF:
		    case AOHD4:
		    case AOF4:
		    case AOHD6:
		    case AOF6:
		        ofn.nFilterIndex =	1;
		        ofn.lpstrDefExt =	"scx";
		        break;
		    case SWGB:
		    case SWGBCC:
		        ofn.nFilterIndex =	2;
		        ofn.lpstrDefExt =	"scx";
		        break;
		    }
		}

		if (!GetOpenFileName(&ofn))
			return;

		switch (ofn.nFilterIndex) {
		case 1:
		    version = AOC;
		    printf_log("Selected %d, AOE 2.\n", ofn.nFilterIndex);
		    break;
		case 2:
		    version = SWGB;
		    printf_log("Selected %d, Star Wars.\n", ofn.nFilterIndex);
		    break;
		}

		/* Now check if selected file is already on recent list. */
		r_parse = setts.recent_first;
		while (r_parse)
		{
			if (!strcmp(r_parse->path, setts.ScenPath))
			{
				file = r_parse;
				break;
			}
			r_parse = r_parse->next;
		}
	}

	/* Open it! */
	SendMessage(page, AOKTS_Closing, 0, 0);
	// set hourglass, might take more than 1ms
	HCURSOR previous = SetCursor(LoadCursor(NULL, IDC_WAIT));
	scen.reset();
	try
	{
		version = scen.open(setts.ScenPath, setts.TempPath, version);

	    /* Handle recent file stuff */
	    if (!file)
	    {
		    file = setts.recent_getnext();
		    strcpy(file->path, setts.ScenPath);
		    strcpy(file->display, PathFindFileName(setts.ScenPath));
		    file->game = (int)version;
	    }
	    setts.recent_push(file);
	    UpdateRecentMenu(propdata.menu);

		SetCursor(previous);
		// for some reason this is always read only. on Wine at least
		//SetSaveState(sheet, ofn.Flags & OFN_READONLY ? MF_GRAYED : MF_ENABLED);
		SetSaveState(sheet, ofn.Flags & OFN_READONLY ? MF_ENABLED : MF_ENABLED);

		// set status bar text
		SetWindowTextW(propdata.statusbar, L"Scenario loaded successfully.");

	    SendMessage(page, AOKTS_Loading, 0, 0);
        SendMessageW(propdata.mapview, MAP_Recreate, 0, 0);
	    MapView_Reset(propdata.mapview, true);

	    filename = getFilenameFromPath(setts.ScenPath);

	    _snprintf(titleBuffer, sizeof(titleBuffer),
		        "%s - %s", szTitle, filename);

	    SetWindowText(sheet, titleBuffer);
	}
	catch (std::exception &ex)
	{
		// TODO: better atomic cursor handling?
		SetCursor(previous);

		// set status bar text
		SetWindowText(propdata.statusbar, ex.what());

		// report error to user
		std::string desc = "Failed to open as ";

        desc.append(gameName(scen.game));
		desc.append(" scenario file.\n");

		switch (scen.game) {
		case AOC:
		    desc.append("Try opening as a SWGB scx file instead\nusing File->Open\n");
		    break;
		case SWGB:
		    desc.append("Try opening as a Conquerors scx file instead\nusing File->Open\n");
		    break;
		}

		desc.append("\nThe problem: ");
		desc.append(ex.what());

		desc.append("\n\nIf the game is able to open the scenario,\nplease report this error. Thanks.");
		printf_log("User message: %s\n", desc.c_str());
		MessageBox(sheet, desc.c_str(), "Scenario Load Error", MB_ICONWARNING);

		// unless it's a debug build, clear the bad data
	#ifndef _DEBUG
		scen.reset();
		SendMessage(page, AOKTS_Closing, 0, 0);
	#endif

	    /* Updates*/
	    SendMessage(page, AOKTS_Loading, 0, 0);

	    _snprintf(titleBuffer, sizeof(titleBuffer),
		        "%s", szTitle);

	    SetWindowText(sheet, titleBuffer);
	}

	//report errors to logfile
	fflush(stdout);
}
Exemple #5
0
int main(int argc, char **argv)
{
	struct gengetopt_args_info args_info;

	char outputDir[PATH_MAX] = ""; // the output directory where files are to be saved
	char filename[PATH_MAX] = ""; // the name of the files (without extension) 
	char capitalFilename[PATH_MAX] = ""; // the name of the files (without extension) in capital letters
	char fullPath[PATH_MAX] = ""; // the full path of the file
	char fileType[4] = ""; // the extension of the file

	char mainTemplateName[PATH_MAX] = ""; // the path to the main template file (.c or .cu)
	char headerTemplateName[PATH_MAX] = ""; // the path to the header template file (.h)
	char makefileTemplateName[PATH_MAX] = ""; // the path to the makefile template file

	char fileVarMainTemplateName[PATH_MAX] = "";// the path to the main template variables file 
	char fileVarHeaderTemplateName[PATH_MAX] = ""; // the path to the header template variables file
	char fileVarMakefileTemplateName[PATH_MAX] = ""; // the path to the makefile template variables file
	char handleErrorTemplateName[PATH_MAX] = ""; // the path to the HandleError.h
	

	HASHTABLE_T *systemVarsTable; // an hashtable containing program genereted variables
	HASHTABLE_T *fileVarsTable;
	LISTA_GENERICA_T *varsIgnoreList; // an hashtable containing file fetched variables	
	
	char userName[PATH_MAX] = "your name"; // the name of the user
	char *fileVars = NULL; // a string with the vars fetched from a variables file


	char *template; // a string with the content of a template file
	unsigned int i = 0; // a utility index
	char *currentDate = NULL; // a string representing the current date
		
	struct passwd *passwd;
	passwd=getpwuid(getuid());
	char * homedir = passwd->pw_dir;
	if(homedir[strlen(homedir) - 1] == '/'){
		homedir[strlen(homedir) - 1] = 0;
	}
	
	
	// parse input parameters
	if (cmdline_parser(argc, argv, &args_info) != 0)
		exit(1);
	
	currentDate = getDateTime();

	//creates an hashtable with system generated template variables
	systemVarsTable = tabela_criar(11, (LIBERTAR_FUNC) free);

	
	varsIgnoreList = lista_criar((LIBERTAR_FUNC) free);
	
	tabela_inserir(systemVarsTable, "$!C_DATE!$", string_clone(currentDate));	
	tabela_inserir(systemVarsTable, "$!USER_NAME!$", string_clone(userName));

	
	// --about
	if (args_info.about_given) {
		return 0;
	} 
	
	// --student
	
	for(i = 0; i < args_info.student_given; i ++){
		lista_inserir(varsIgnoreList, string_clone(args_info.student_arg[i]));
	}
	
	// --proto
	if (args_info.proto_given) {
		tabela_inserir(systemVarsTable, "$!KERNEL_PROTO!$", string_clone(args_info.proto_arg));	
	} else {
		tabela_inserir(systemVarsTable, "$!KERNEL_PROTO!$", string_clone(""));	
	}
	
	// --kernel
	if (args_info.kernel_given) {
		tabela_inserir(systemVarsTable, "$!KERNEL_NAME!$", string_clone(args_info.kernel_arg));	
	} else {
		tabela_inserir(systemVarsTable, "$!KERNEL_NAME!$", string_clone("Kernel"));	
	}
	
	// --blocks
	store_grid_geometry(systemVarsTable, &args_info);
	// --threads
	store_blocks_geometry(systemVarsTable, &args_info);
	
	// --dir
	// get filename from path (the name of the last directory)
	getFilenameFromPath(args_info.dir_arg, filename);

	// the filename in capital letters
	for (i = 0; i < strlen(filename); i++) {
		capitalFilename[i] = toupper(filename[i]);
	}
	capitalFilename[i] = 0;
	
	tabela_inserir(systemVarsTable, "$!FILENAME!$", string_clone(filename));
	tabela_inserir(systemVarsTable, "$!CAPITAL_FILENAME!$", string_clone(capitalFilename));
	

	// removes the / character
	if (args_info.dir_arg[strlen(args_info.dir_arg) - 1] == '/') {
		args_info.dir_arg[strlen(args_info.dir_arg) - 1] = 0;
	}
	//creates the output directory
	if (!createDirectory(args_info.dir_arg)) {

		if (args_info.Force_given) {
			// removes the existing directoy        
			remove_directory(args_info.dir_arg);
			sprintf(outputDir, "%s", args_info.dir_arg);
		} else {
			// adds a date to the directory name
			sprintf(outputDir, "%s%s", args_info.dir_arg,
				currentDate);
		}

		createDirectory(outputDir);
	} else {
		sprintf(outputDir, "%s", args_info.dir_arg);
	}

	sprintf(outputDir, "%s/", outputDir);


	if (!args_info.measure_given) {
		char *var = malloc(strlen("MEASURE") + 1);
		strcpy(var, "MEASURE");
		lista_inserir(varsIgnoreList, var);

	}
	

	/* defines which templates to use */

	// cuda with prototype
	if (args_info.proto_given) {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, CU_PROTO_TEMPLATE);

		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, CU_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, CU_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, CU_PROTO_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, CU_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, CU_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".cu");

		// regular c template   
	} else if (args_info.regular_code_given) {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, C_MAIN_TEMPLATE);
		
		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, C_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, C_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, C_MAIN_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, C_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, C_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".c");

		// cuda default
	} else {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, CU_MAIN_TEMPLATE);

		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, CU_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, CU_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, CU_MAIN_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, CU_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, CU_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".cu");

	}
	
	// create HANDLE_ERROR_H file
	if (strcmp(fileType, ".cu") == 0) {
		// reads from source file
		strcpy(handleErrorTemplateName, homedir);
		strcat(handleErrorTemplateName, HANDLE_ERROR_H);