Пример #1
0
void filecontainer_doopen(t_filecontainer *x, t_symbol *arg)
{
    t_atom			a[4];
    int				err = 0;
    char			filename[256];
    short			path;
    t_fourcc		outtype = 0;
    t_fourcc		type = 'cO0p';
#ifdef MAC_VERSION
    char			*temppath;
    FSRef			ref;
    Boolean			isDir;
    FSCatalogInfo	catalogInfo;
#else // WIN_VERSION
    char			temppath[512];
#endif
    char			fullpath[512];
    t_object		*result = NULL;
    t_object		*result2 = NULL;
    char			**record = NULL;		// sqlite records
    char			**record2 = NULL;		// sqlite records
    t_filehandle	file_handle;
    t_ptr_size		len = 0;
    char			*blob;
    char			sql[512];

    if(!arg || !arg->s_name[0]) {
        if(open_dialog(filename, &path, &outtype, NULL, -1))			// Returns 0 if successful
            return;
    }
    else {
        t_fourcc typelist[1];
        typelist[0] = 'cO0p';
        strcpy(filename, arg->s_name);
        path = 0;
        locatefile_extended(filename, &path, &type, typelist, 0);
    }
    path_topotentialname(path, filename, fullpath, 0);
#ifdef MAC_VERSION
    temppath = strchr(fullpath, ':');
    temppath += 1;
#else // WIN_VERSION
    path_nameconform(fullpath, temppath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE);
#endif
    x->name = gensym(temppath);

    // Create our temp folder for extracted files
    filecontainer_gettemppath(x);

    // Create the SQLite instance
    atom_setsym(&a[0], gensym("@rambased"));
    atom_setlong(&a[1], 0);
    atom_setsym(&a[2], gensym("@db"));
    atom_setsym(&a[3], x->name);
    x->sqlite = object_new_typed(CLASS_NOBOX, _sym_sqlite, 4, a);

    // Operate on the open DB
    if(x->sqlite) {
        object_method(x->sqlite, ps_starttransaction);
        object_method(x->sqlite, _sym_execstring, TABLEDEF_FILES, NULL);
        object_method(x->sqlite, _sym_execstring, TABLEDEF_ATTRS, NULL);

        object_method(x->sqlite, _sym_execstring, "UPDATE files SET valid = 1", NULL);
        object_method(x->sqlite, _sym_execstring, "SELECT file_id, filename, moddate FROM files", &result);
        while(record = (char **)object_method(result, _sym_nextrecord)) {
            // Here we check for the optional 'platform' attr for this file.
            // If a flag exists for the other platform, but not for the current platform, then we ignore this file.
#ifdef MAC_VERSION
            sprintf(sql, "SELECT file_id_ext FROM attrs \
							WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]);
            object_method(x->sqlite, _sym_execstring, sql, &result2);
            record2 = (char **)object_method(result2, _sym_nextrecord);
            if(record2) {
                sprintf(sql, "SELECT file_id_ext FROM attrs \
								WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]);
                object_method(x->sqlite, _sym_execstring, sql, &result2);
                record2 = (char **)object_method(result2, _sym_nextrecord);
                if(!record2) {
                    sprintf(sql, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]);
                    object_method(x->sqlite, _sym_execstring, sql, NULL);
                    continue;
                }
            }
#else // WIN_VERSION
            snprintf(sql, 512, "SELECT file_id_ext FROM attrs \
							WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]);
            object_method(x->sqlite, _sym_execstring, sql, &result2);
            record2 = (char **)object_method(result2, _sym_nextrecord);
            if(record2) {
                snprintf(sql, 512, "SELECT file_id_ext FROM attrs \
								WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]);
                object_method(x->sqlite, _sym_execstring, sql, &result2);
                record2 = (char **)object_method(result2, _sym_nextrecord);
                if(!record2) {
                    snprintf(sql, 512, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]);
                    object_method(x->sqlite, _sym_execstring, sql, NULL);
                    continue;
                }
            }
#endif
            // At this point we have a file (record[0]), and we have determined that it is indeed a file we want to cache
            // So cache it to a new file in our temp path
            err = path_createsysfile(record[1], x->temp_path, type, &file_handle);
            if(err) {																// Handle any errors that occur
                object_error((t_object *)x, "%s - error %d creating file", filename, err);
            }
            else {
                snprintf(sql, 512, "SELECT content FROM files WHERE file_id = %s", record[0]);
                object_method(x->sqlite, ps_getblob, sql, &blob, &len);
                err = sysfile_write(file_handle, &len, blob);
                if(err) {
                    object_error((t_object *)x, "sysfile_write error (%d)", err);
                }
            }
            err = sysfile_seteof(file_handle, len);
            if(err) {
                object_error((t_object *)x, "%s - error %d setting EOF", filename, err);
            }
            sysfile_close(file_handle);		// close file reference
            sysmem_freeptr(blob);
            blob = NULL;

            // Set the moddate
#ifdef MAC_VERSION
//			FSCatalogInfo		catalogInfo;
//			Boolean             status;
            CFGregorianDate     gdate;
            CFAbsoluteTime      abstime;
            CFTimeZoneRef		tz;
            UTCDateTime			utc;

            sscanf(record[2], "%4ld-%02hd-%02hd %02hd:%02hd:%02lf", &gdate.year, (signed short*)&gdate.month, (signed short*)&gdate.day,
                   (signed short*)&gdate.hour, (signed short*)&gdate.minute, &gdate.second);
            tz = CFTimeZoneCopySystem();
            abstime = CFGregorianDateGetAbsoluteTime(gdate, tz);
            UCConvertCFAbsoluteTimeToUTCDateTime(abstime, &utc);
            catalogInfo.contentModDate = utc;

            strcpy(s_tempstr, x->temp_fullpath->s_name);
            temppath = strchr(s_tempstr, ':');
            temppath++;
            strcat(temppath, "/");
            strcat(temppath, record[1]);
            FSPathMakeRef((UInt8*)temppath, &ref, &isDir);
            err = FSSetCatalogInfo(&ref, kFSCatInfoContentMod, &catalogInfo);

#else // WIN_VERSION
            char				winpath[512];
            HANDLE				hFile;
            FILETIME			fileTime;
            SYSTEMTIME			systemTime;

            sscanf(record[2], "%4lu-%02lu-%02lu %02lu:%02lu:%02lu", &systemTime.wYear, &systemTime.wMonth, &systemTime.wDay,
                   &systemTime.wHour, &systemTime.wMinute, &systemTime.wSecond);
            err = SystemTimeToFileTime(&systemTime, &fileTime);

            strcpy(s_tempstr, x->temp_fullpath->s_name);
            path_nameconform(s_tempstr, winpath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE);
            strcat(winpath, "\\");
            strcat(winpath, record[1]);
            hFile = CreateFile((LPCSTR)winpath , GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
            if(hFile == INVALID_HANDLE_VALUE) {
                object_error((t_object *)x, "invalid handle value");
                goto out;
            }
            err = SetFileTime(hFile, &fileTime, &fileTime, &fileTime);
            if(err == 0) {
                err = GetLastError();
                object_error((t_object *)x, "Error setting date: %i", err);
            }
            CloseHandle(hFile);
out:
            ;
#endif
        }
        object_method(x->sqlite, ps_endtransaction);
    }
Пример #2
0
void *jsusfx_new(t_symbol *notused, long argc, t_atom *argv) {
	if ( argc < 1 || atom_gettype(argv) != A_SYM ) {
		error("jsusfx~: missing script name");
		return NULL;
	}
    
	t_jsusfx *x = reinterpret_cast<t_jsusfx *>(object_alloc(jsusfx_class));
	t_symbol *s = atom_getsym(argv);
    t_fourcc filetype = 'TEXT', outtype;
    short path;
    char filename[MAX_PATH_CHARS];
    
    strcpy(filename, s->s_name);
    
    if (locatefile_extended(filename, &path, &outtype, &filetype, 1)) {
        t_object *mypatcher;
        object_obex_lookup(x, gensym("#P"), &mypatcher);
        t_symbol *checkExists = object_attr_getsym(mypatcher, gensym("filepath"));
        
        if ( checkExists->s_name[0] == 0 ) {
            error("jsusfx~: patch needs to be saved in order to create new jsusfx script file");
            return NULL;
        }
        
        path = path_getdefault();
                
        t_fourcc type = 'TEXT';
        t_filehandle ref;

        if ( path_createsysfile(filename, path, type, &ref) ) {
            error("jsusfx~: unable to create file");
            return NULL;
        }

        char initText[] = "@sample\nspl0=1\nspl1=-1\n";
        t_handle h = sysmem_newhandle(0);
        sysmem_ptrandhand(initText,h,strlen(initText));
        
        if ( sysfile_writetextfile(ref, h, TEXT_LB_NATIVE) ) {
            error("jsusfx~: unable to write file");
            return NULL;
        }
        
        sysfile_close(ref);
        sysmem_freehandle(h);
    }
    
    strcpy(x->scriptname, filename);
    x->path = path;
    
    char fullpath[MAX_PATH_CHARS];
    path_toabsolutesystempath(path, filename, fullpath);
    std::ifstream is(fullpath);
	if ( ! is.is_open() ) {
		error("jsusfx~: error opening file %s", fullpath);
		return NULL;
	}
    
    x->bypass = false;
    dsp_setup((t_pxobject *)x, 2);
    x->outlet1 = outlet_new((t_object *)x, NULL);
	outlet_new((t_object *)x, "signal");
    outlet_new((t_object *)x, "signal");
    
	critical_new(&(x->critical));
	x->m_editor = NULL;
    JsusFxMax *fx = new JsusFxMax();
    fx->compile(is);
    x->fx = fx;

    /*if ( argc >= 2 && atom_gettype(argv+1) == A_LONG ) {
		x->fx->normalizeSliders = atom_getlong(argv+1);
	} else {
        x->fx->normalizeSliders = 1;
    }
    
    post("normalizer sl %x", x->fx->normalizeSliders);*/
    
	return (x);
}