示例#1
0
void A_saveScript(IScriptEngine* engine, const char* name)
{
    IScriptWriter *writer = engine->createScriptWriter();
    ADM_ScriptGenerator generator(video_body, writer);
    std::stringstream stream(std::stringstream::in | std::stringstream::out);
	std::string fileName = name;

    generator.generateScript(stream);
    delete writer;

	if (fileName.rfind(".") == std::string::npos)
	{
		fileName += "." + engine->defaultFileExtension();
	}

    FILE *file = ADM_fopen(fileName.c_str(), "wt");
    string script = stream.str();

    ADM_fwrite(script.c_str(), script.length(), 1, file);
    ADM_fclose(file);
}
void FileSel_ReadWrite(SELFILE_CB *cb, int rw, const char *name, const char *actual_workbench_file)
{
	if(name)
	{
		if(cb)
		{
			FILE *fd;
			fd=ADM_fopen(name,"rb");
			if(rw==0) // read
			{
				// try to open it..
				if(!fd)
				{
					GUI_Error_HIG(QT_TRANSLATE_NOOP("filesel","File error"), QT_TRANSLATE_NOOP("filesel","Cannot open \"%s\"."), name);
					return;
				}
			}
			else // write
			{
				if(fd){
					struct stat buf;
					int fdino;
					ADM_fclose(fd);

					char msg[300];

					snprintf(msg, 300, QT_TRANSLATE_NOOP("filesel","%s already exists.\n\nDo you want to replace it?"), ADM_GetFileName(name));

					if(!GUI_Question(msg))
						return;
					/*
					** JSC Fri Feb 10 00:07:30 CET 2006
					** compare existing output file inode against each current open files inode
					** i'm ignoring st_dev, so we may get false positives
					** i'm testing until fd=1024, should be MAXFD computed by configure
					** keep in mind:
					** you can overwrite .idx files, they are loaded into memory and closed soon
					** you cannot overwrite segment data files, all files are kept open and
					** are detected here
					*/
#ifndef _WIN32
					if( stat(name,&buf) == -1 ){
						fprintf(stderr,"stat(%s) failed\n",name);
						return;
					}
#endif
					fdino = buf.st_ino;
					for(int i=0;i<1024;i++){
						if( fstat(i,&buf) != -1 ){
							if( buf.st_ino == fdino ){
								char str[512];
								snprintf(str,512,"File \"%s\" exists and is opened by Avidemux",name);
								GUI_Error_HIG(str,
									QT_TRANSLATE_NOOP("filesel","It is possible that you are trying to overwrite an input file!"));
								return;
							}
						}
					}
					/*
					** compare output file against actual EMCAscript file
					** need to stat() to avoid symlink (/home/x.js) vs. real file (/export/home/x.js) case
					*/
					if( actual_workbench_file ){
						if( stat(actual_workbench_file,&buf) != -1 ){
							if( buf.st_ino == fdino ){
								char str[512];
								snprintf(str,512,"File \"%s\" exists and is the actual ECMAscript file",name);
								GUI_Error_HIG(str,QT_TRANSLATE_NOOP("filesel","It is possible that you are trying to overwrite an input file!"));
								return;
							}
						}
					}
				}

				// check we have right access to it
				fd=ADM_fopen(name,"wb");
				if(!fd)
				{
					GUI_Error_HIG(QT_TRANSLATE_NOOP("filesel","Cannot write the file"),QT_TRANSLATE_NOOP("filesel", "No write access to \"%s\"."), name);
					return;
				}
			}
			ADM_fclose(fd);
			cb(name);
		} // no callback -> return value
	}
}
ADM_audioAccessFile::~ADM_audioAccessFile()
{
        if(_fd) ADM_fclose(_fd);
        _fd=NULL;
}