CLWProgram CLWProgram::CreateFromFile(char const* filename,
                                      char const** headernames,
                                      int numheaders,
                                      char const* buildopts,
                                      CLWContext context)
{
    std::vector<char> sourcecode;
    load_file_contents(filename, sourcecode, false);
    
    std::vector<std::vector<char> > headers;
    std::vector<char const*> headerstrs;
    std::vector<size_t> headerssizes;

    if (numheaders > 0)
    {
        for (int i = 0; i < numheaders; ++i)
        {
            std::vector<char> headersource;
            load_file_contents(headernames[i], headersource, false);
            headerssizes.push_back(headersource.size());
            headers.push_back(std::move(headersource));
            headerstrs.push_back(&headers[i][0]);
        }

        return CreateFromSource(&sourcecode[0], sourcecode.size(), &headerstrs[0], headernames, &headerssizes[0], numheaders, buildopts, context);
    }
    else
    {
        return CreateFromSource(&sourcecode[0], sourcecode.size(), buildopts, context);
    }
}
int
main (int argc, char **argv)
{
    /* Setup locale */
    setlocale (LC_ALL, "");

    /* Setup context */
    if (!setup_context (argc, argv)) {
        g_printerr ("Context setup failed... exiting\n");
        return -1;
    }

    /* Either text or file must be given */
    if (filename == NULL &&
            text == NULL) {
        g_printerr ("Either 'file' or 'text' options should be used\n"
                    "Run '%s --help' to see a full list of available "
                    "command line options.\n",
                    argv[0]);
        return -2;
    }

    /* If required, load file contents */
    if (filename != NULL &&
            !load_file_contents ()) {
        g_printerr ("Loading file '%s' contents failed... exiting\n",
                    filename);
        return -3;
    }

    /* Run the parsing! */
    if (!run_parsing ()) {
        g_printerr ("Parsing operation failed... exiting\n");
        return -4;
    }

    /* Clean exit */
    if (filename)
        g_free (text);
    return 0;
}
int main(int argc, char **argv)
{
	int result;
	int upload_count;
	int rand_num;
	int file_index;
	char file_id[64];
	char storage_ip[IP_ADDRESS_SIZE];
	int count_sums[FILE_TYPE_COUNT];
	int i;
	struct timeval tv_start;
	struct timeval tv_end;
	int time_used;

	if (argc < 2)
	{
		printf("Usage: %s <process_index>\n", argv[0]);
		return EINVAL;
	}

	log_init();
	proccess_index = atoi(argv[1]);
	if (proccess_index < 0 || proccess_index >= PROCESS_COUNT)
	{
		printf("Invalid proccess index: %d\n", proccess_index);
		return EINVAL;
	}

	if ((result = load_file_contents()) != 0)
	{
		return result;
	}

	if ((result=test_init()) != 0)
	{
		return result;
	}

	if ((result=dfs_init(proccess_index)) != 0)
	{
		return result;
	}

	if (daemon(1, 1) != 0)
	{
		return errno != 0 ? errno : EFAULT;
	}

	memset(&storages, 0, sizeof(storages));
	upload_count = 0;
	for (i=0; i<FILE_TYPE_COUNT; i++)
	{
		upload_count += files[i].count;
		count_sums[i] = upload_count;
	}

	if (upload_count == 0)
	{
		return EINVAL;
	}

	memset(file_id, 0, sizeof(file_id));
	memset(storage_ip, 0, sizeof(storage_ip));

	start_time = time(NULL);
	srand(SRAND_SEED);
	result = 0;
	total_count = 0;
	success_count = 0;
	while (total_count < upload_count)
	{
		rand_num = (int)(upload_count * ((double)rand() / RAND_MAX));
		for (file_index=0; file_index<FILE_TYPE_COUNT; file_index++)
		{
			if (rand_num < count_sums[file_index])
			{
				break;
			}
		}

		if (files[file_index].upload_count >= files[file_index].count)
		{
			continue;
		}

		files[file_index].upload_count++;
		total_count++;

		gettimeofday(&tv_start, NULL);
		*storage_ip = '\0';

		result = upload_file(files[file_index].file_buff, files[file_index].bytes, file_id, storage_ip);
		gettimeofday(&tv_end, NULL);
		time_used = TIME_SUB_MS(tv_end, tv_start);
		files[file_index].time_used += time_used;

		add_to_storage_stat(storage_ip, result, time_used);
		if (result == 0) //success
		{
			success_count++;
			files[file_index].success_count++;

			fprintf(fpSuccess, "%d %d %s %s %d\n", 
				(int)tv_end.tv_sec, files[file_index].bytes, 
				file_id, storage_ip, time_used);
		}
		else //fail
		{
			fprintf(fpFail, "%d %d %d %d\n", (int)tv_end.tv_sec, 
				files[file_index].bytes, result, time_used);
			fflush(fpFail);
		}

		if (total_count % 100 == 0)
		{
			if ((result=save_stats_by_overall()) != 0)
			{
				break;
			}
			if ((result=save_stats_by_file_type()) != 0)
			{
				break;
			}

			if ((result=save_stats_by_storage_ip()) != 0)
			{
				break;
			}
		}

	}

	save_stats_by_overall();
	save_stats_by_file_type();
	save_stats_by_storage_ip();

	fclose(fpSuccess);
	fclose(fpFail);

	dfs_destroy();

	printf("proccess %d, time used: %ds\n", proccess_index, (int)(time(NULL) - start_time));
	return result;
}
int TestUpload::Run(UINT32 (__stdcall * func_UploadFileByID)(const BYTE *pbyFileBuff, UINT32 nFileSize, const BYTE *pbyFileExtName, BYTE *pbyFileID),
					UINT32 (__stdcall * func_UploadSlaveFileByID)(const BYTE *pbyFileBuff, UINT32 nFileSize,
						const BYTE *pbyMasterGroupName, const BYTE *pbyMasterFileName,
						const BYTE *pbyPrefixName, const BYTE *pbyFileExtName,
						BYTE *pbyFileID))
{
	int result;
	int upload_count;
	int rand_num;
	int file_index;
	char file_id[256];
	int i;
	int time_used;
	SYSTEMTIME tStart;
	SYSTEMTIME tEnd;


	if ((result = load_file_contents()) != 0)
	{
		return result;
	}

	if ((result=test_init()) != 0)
	{
		return result;
	}

	upload_count = testFile.count;

	if (upload_count == 0)
	{
		return EINVAL;
	}

	memset(file_id, 0, sizeof(file_id));

	start_time = time(NULL);
	srand(0);
	result = 0;
	total_count = 0;
	success_count = 0;
	while (total_count < upload_count)
	{
		total_count++;
		GetLocalTime(&tStart);

		result = func_UploadFileByID((const BYTE*)testFile.file_buff, testFile.bytes, (const BYTE*)"jpg", (BYTE*)file_id);
	
		GetLocalTime(&tEnd);
		time_used = TIME_SUB_MS(tEnd, tStart);
		testFile.time_used += time_used;
		testFile.upload_count++;
		if (result == 0) //success
		{
			success_count++;
			testFile.success_count++;

			fprintf(fpSuccess, "%d %d %s %d\n", 
				(int)tEnd.wSecond, testFile.bytes, 
				file_id, time_used);
		}
		else //fail
		{
			fprintf(fpFail, "%d %d %d %d\n", (int)tEnd.wSecond, 
				testFile.bytes, result, time_used);
			fflush(fpFail);
		}


		if (total_count % 100 == 0)
		{
			if ((result=save_stats_by_overall()) != 0)
			{
				break;
			}
			if ((result=save_stats_by_file_type()) != 0)
			{
				break;
			}
		}

	}

	save_stats_by_overall();
	save_stats_by_file_type();

	fclose(fpSuccess);
	fclose(fpFail);

	return result;
}
CLWProgram CLWProgram::CreateFromFile(char const* filename, char const* buildopts, CLWContext context)
{
    std::vector<char> sourcecode;
    load_file_contents(filename, sourcecode, false);
    return CreateFromSource(&sourcecode[0], sourcecode.size(), buildopts, context);
}