Example #1
0
static void psystem_read_source_file2(const char * filename) {
    FILE * f = fopen(filename, "r");
    if(f == NULL) {
        ERROR("failed to open %s", filename);
    }
    int NR = 0;
    int NF = 0;
    char * line = NULL;
    size_t len = 0;
    intptr_t isrc = 0;
    int stage = 0;
    size_t Nsamples;
    intptr_t isample;
    double x, y, z, dx, dy, dz, radius, L, time;
    char spec[128];
    char type[128];

    char timefmtstr[128];
    char locationfmtstr[128] = "absolute";
    int timefmt = 0;
    int locationfmt = 0;
    size_t nsrcs_file;

    while(0 <= getline(&line, &len, f)) {
        if(line[0] == '#' || line[0] == '\n') {
            NR++;
            continue;
        }
        switch(stage) {
        case 0:
            if(1 == sscanf(line, "%ld %80s %80s", &nsrcs_file, timefmtstr, locationfmtstr)) {
                fclose(f);
                free(line);
                /* maybe this is a verion 1 file? */
                psystem_read_source_file(filename);
                return;
            } else {
                if(!strcmp(timefmtstr, "time_q"))
                    timefmt = 0;
                else if(!strcmp(timefmtstr, "a"))
                    timefmt = 0;
                else if(!strcmp(timefmtstr, "redshift"))
                    timefmt = 1;
                else if(!strcmp(timefmtstr, "z"))
                    timefmt = 1;
                else if(!strcmp(timefmtstr, "myr"))
                    timefmt = 2;
                else if(!strcmp(timefmtstr, "tick"))
                    timefmt = 3;
                else if(!strcmp(locationfmtstr, "absolute")) {
                    locationfmt = 0;
                } else {
                    ERROR("time fmt str %s unknown", timefmtstr);
                }
                if(!strcmp(locationfmtstr, "relative")) {
                    locationfmt = 1;
                }
                isrc = psys.nsrcs;
                psys.nsrcs += nsrcs_file;
                psys.srcs = realloc(psys.srcs, sizeof(Source) * psys.nsrcs);
                stage ++;
            }
            break;
        case 1:
            NF = sscanf(line, "%ld %80s %80s %lf", &Nsamples, spec, type, &radius);
            if(NF == 3 && !strcmp(type, "plane")) {
                ERROR("%s format error at %d, needs radius for plane source", filename, NR);
            }
            psys.srcs[isrc].ticks = NULL;
            psys.srcs[isrc].Ngamma_dots = NULL;
            psys.srcs[isrc].lastemit = 0;
            ARRAY_RESIZE(psys.srcs[isrc].ticks, intptr_t, Nsamples);
            ARRAY_RESIZE(psys.srcs[isrc].Ngamma_dots, double, Nsamples);
            psys.srcs[isrc].cursor = 0;
            psys.srcs[isrc].specid = spec_get(spec);
            if(!strcmp(type, "plane")) {
                psys.srcs[isrc].type = PSYS_SRC_PLANE;
                psys.srcs[isrc].radius = radius;
            } else {
                psys.srcs[isrc].type = PSYS_SRC_POINT;

            }
            stage++;
            isample = 0;
            break;
        case 2:
            NF = sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
                        &time, &x, &y, &z, &dx, &dy, &dz, &L );
            if(NF != 8) {
                ERROR("%s format error at %d", filename, NR);
            }
            intptr_t tick;

            switch(timefmt) {
            case 0:
                time = z2t(1 / (1 + time));
                tick = (time * U_MYR - psys.epoch->age) / psys.tick_time;
            case 1:
                time = z2t(time);
                tick = (time * U_MYR - psys.epoch->age) / psys.tick_time;
                break;
            case 2:
                tick = (time * U_MYR) / psys.tick_time;
                break;
            case 3:
                tick = time;
                break;
            }
            psys.srcs[isrc].ticks[isample] = tick;

            switch(locationfmt) {
            case 0:
                break;
            case 1:
                x = psys.boxsize * x;
                y = psys.boxsize * y;
                z = psys.boxsize * z;
                break;
            }

            if(psys.srcs[isrc].type == PSYS_SRC_PLANE) {
                psys.srcs[isrc].Ngamma_dots[isample] = L * M_PI * radius * radius / (U_CM * U_CM * U_SEC);
                solve_u_v(psys.srcs[isrc].dir, psys.srcs[isrc].a, psys.srcs[isrc].b);
            }
            if(psys.srcs[isrc].type == PSYS_SRC_POINT) {
                if(L < 0) {
                    L = spec_N_from_lum(psys.srcs[isrc].specid, -L * C_SOLAR_LUM) * U_SEC / 1e50;
                }
                psys.srcs[isrc].Ngamma_dots[isample] = L * 1e50 / U_SEC;
            }
            psys.srcs[isrc].Ngamma_dots[isample] *= CFG_BOOST_SOURCE_FACTOR; /* boost the photon luminosity according to cfg file */

            psys.srcs[isrc].pos[0] = x;
            psys.srcs[isrc].pos[1] = y;
            psys.srcs[isrc].pos[2] = z;
            psys.srcs[isrc].dir[0] = dx;
            psys.srcs[isrc].dir[1] = dy;
            psys.srcs[isrc].dir[2] = dz;
            isample ++;
            if(isample == Nsamples) {
                if(isrc == psys.nsrcs) {
                    stage ++;
                } else {
                    stage --;
                    isrc++;
                }
            }
            break;
        case 3:
            break;
        }
        NR++;
        if(stage == 3) break;
    }
    if(isrc != psys.nsrcs) {
        ERROR("%s expecting %lu, but has %lu sources, %lu", filename, psys.nsrcs, isrc, NR);
    }
    free(line);
    fclose(f);

}
/** 
	Responsible for recursing into folders, and finding files, 
	if the object is a file, the file will be added to the first available threadpool's thread,
	or a new worker thread created until MAX_THREADS reached.
*/
void FileSearchThread::processFileOrFolder(LPWSTR fullpath)
{
	//Sleep(1);
	//Used to enable CANCEL!
	if (stop) 
	{
		return;
	}

	if (GetFileAttributes(fullpath) != FILE_ATTRIBUTE_DIRECTORY)
	{
		if (debugFlag)
		{
			swprintf_s(tbuffer, L"FileSearchThread::processFileOrFolder -- fullpath: '%s'\n\0", fullpath);
			logger->debug(tbuffer);
		}


		if (fileSearchForTextThreadCount < MAX_FILE_SEARCH_THREADS)
		{
			FileSearchForTextThread* tmpX = new FileSearchForTextThread(this, fullpath, logger);
			ARRAY_RESIZE(FileSearchForTextThread, fileSearchForTextThreads, fileSearchForTextThreadCount, tmpX);
			tmpX->start();

			if (debugFlag)
			{
				swprintf_s(tbuffer, L"FileSearchThread::processFileOrFolder -- ThreadCount: %i\n", fileSearchForTextThreadCount);
				logger->debug(tbuffer);
			}
		}
		else
		{
			bool found = false;
			while (!found)
			{
				//Find / Wait for the first available thread.
				for (int i=0; i<fileSearchForTextThreadCount; i++)
				{
					if (fileSearchForTextThreads[i] && fileSearchForTextThreads[i]->started && fileSearchForTextThreads[i]->completed)
					{
						//PROCESS RESULTS....
						if (fileSearchForTextThreads[i]->fileSearchResults && fileSearchForTextThreads[i]->fileSearchResultCount > 0)
						{
							processFoundResults(fileSearchForTextThreads[i]);
						}

						delete fileSearchForTextThreads[i];
						fileSearchForTextThreads[i] = NULL;
						fileSearchForTextThreads[i] = new FileSearchForTextThread(this, fullpath, logger);
						fileSearchForTextThreads[i]->start();

						found = true;

						if (debugFlag)
						{
							swprintf_s(tbuffer, L"FileSearchThread::processFileOrFolder -- ReuseThread: #%i\n", i);
							logger->debug(tbuffer);
						}

						break;
					}
				}
			}

		}



		totalFileCount += 1;

		//Update msg.
		swprintf_s(tbuffer, L"Searching files.. %i\0", totalFileCount);
		Static_SetText(m_form->getHWND(FSM_LBL_STATIC_MSG), tbuffer);

	}
	else
	{
		//List all files/folders within this directory.
		size_t len = wcslen(fullpath) + 4;
		LPWSTR findString = new wchar_t[len];

		ZeroMemory(findString, sizeof(wchar_t) * (len));
		wcscpy_s(findString, len, fullpath);
		wcscat_s(findString, len, L"\\*\0");

		WIN32_FIND_DATA find;
		HANDLE findResult = NULL;

		ZeroMemory(&find, sizeof(WIN32_FIND_DATA));
		findResult = FindFirstFile(findString, &find);
		BOOL found = true;

		while (findResult && found)
		{

			if (wcscmp(find.cFileName, L".") != 0 && wcscmp(find.cFileName, L"..") != 0)
			{
				size_t len = wcslen(fullpath) + wcslen(find.cFileName) + 2;
				LPWSTR fullpathTemp = new wchar_t[len];

				//Build the filename.
				ZeroMemory(fullpathTemp, sizeof(wchar_t) * (len));
			
				//Overlapping issue.
				for (UINT i=0; i<wcslen(fullpath); i++)
					fullpathTemp[i] = fullpath[i];

				wcscat_s(fullpathTemp, len, L"\\\0");
				wcscat_s(fullpathTemp, len, find.cFileName);
			
				processFileOrFolder(fullpathTemp);

				delete [] fullpathTemp;
			}
			found = FindNextFile(findResult, &find);
		}

		FindClose(findResult);
		delete [] findString;
	}
}
Example #3
0
static void psystem_read_source_file(const char * filename) {
    FILE * f = fopen(filename, "r");
    if(f == NULL) {
        ERROR("failed to open %s", filename);
    }
    MESSAGE("reading a version 1 source file");
    int NR = 0;
    int NF = 0;
    char * line = NULL;
    size_t len = 0;
    intptr_t isrc = 0;
    int stage = 0;
    double x, y, z, dx, dy, dz, radius, L;
    char spec[128];
    char type[128];

    size_t nsrcs_file;
    while(0 <= getline(&line, &len, f)) {
        if(line[0] == '#' || line[0] == '\n') {
            NR++;
            continue;
        }
        switch(stage) {
        case 0:
            if(1 != sscanf(line, "%ld", &nsrcs_file)) {
                ERROR("%s format error at %d", filename, NR);
            } else {
                isrc = psys.nsrcs;
                psys.nsrcs += nsrcs_file;
                psys.srcs = realloc(psys.srcs, sizeof(Source) * (psys.nsrcs));
                stage ++;
            }
            break;
        case 1:
            NF = sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %80s %80s %lf",
                        &x, &y, &z, &dx, &dy, &dz,
                        &L, spec, type, &radius);
            if(NF != 9 && NF != 10) {
                ERROR("%s format error at %d", filename, NR);
            }
            psys.srcs[isrc].pos[0] = x;
            psys.srcs[isrc].pos[1] = y;
            psys.srcs[isrc].pos[2] = z;
            psys.srcs[isrc].dir[0] = dx;
            psys.srcs[isrc].dir[1] = dy;
            psys.srcs[isrc].dir[2] = dz;
            psys.srcs[isrc].specid = spec_get(spec);
            psys.srcs[isrc].ticks = NULL;
            psys.srcs[isrc].Ngamma_dots = NULL;
            psys.srcs[isrc].lastemit = 0;
            ARRAY_RESIZE(psys.srcs[isrc].ticks, intptr_t, 1);
            ARRAY_RESIZE(psys.srcs[isrc].Ngamma_dots, double, 1);
            psys.srcs[isrc].cursor = 0;
            if(!strcmp(type, "plane")) {
                if(NF != 10) {
                    ERROR("%s format error at %d, needs 10 fields", filename, NR);
                }
                psys.srcs[isrc].type = PSYS_SRC_PLANE;
                psys.srcs[isrc].radius = radius;
                psys.srcs[isrc].ticks[0] = 0;
                psys.srcs[isrc].Ngamma_dots[0] = L * M_PI * radius * radius / (U_CM * U_CM * U_SEC);
                solve_u_v(psys.srcs[isrc].dir, psys.srcs[isrc].a, psys.srcs[isrc].b);
            } else {
                if(L < 0) {
                    L = spec_N_from_lum(psys.srcs[isrc].specid, -L * C_SOLAR_LUM) * U_SEC / 1e50;
                }
                psys.srcs[isrc].type = PSYS_SRC_POINT;
                psys.srcs[isrc].ticks[0] = 0;
                psys.srcs[isrc].Ngamma_dots[0] = L * 1e50 / U_SEC;
            }
            psys.srcs[isrc].Ngamma_dots[0] *= CFG_BOOST_SOURCE_FACTOR; /* boost the photon luminosity according to cfg file */
            isrc ++;
            if(isrc == psys.nsrcs) {
                stage ++;
            }
            break;
        case 2:
            break;
        }
        NR++;
        if(stage == 2) break;
    }
    if(isrc != psys.nsrcs) {
        ERROR("%s expecting %lu, but has %lu sources, %lu", filename, psys.nsrcs, isrc, NR);
    }
    free(line);
    fclose(f);
}