void
OpenJournal (void)
{
    DWORD start_seed;

#if CREATE_JOURNAL
    if (create_journal)
    {
        if (journal_fh = copen (journal_buf, MEMORY_STREAM, STREAM_WRITE))
        {
            start_seed = SeedRandomNumbers ();
            cwrite ((PBYTE)&start_seed, sizeof (start_seed), 1, journal_fh);
        }
    }
    else
#endif /* CREATE_JOURNAL */
    {
        uio_Stream *fp;

        if (fp = res_OpenResFile ("starcon.jnl", "rb"))
        {
            ReadResFile (journal_buf, 1, sizeof (journal_buf), fp);
            res_CloseResFile (fp);

            if (journal_fh = copen (journal_buf, MEMORY_STREAM, STREAM_READ))
            {
                OldArrowInput = ArrowInput;
                ArrowInput = DemoInput;
                PlayerInput[0] = PlayerInput[1] = DemoInput;

                FlushInput ();

                cread ((PBYTE)&start_seed, sizeof (start_seed), 1, journal_fh);
                TFB_SeedRandom (start_seed);
            }
        }
    }
}
예제 #2
0
void Lexer::lex() {
    // Use the read to fetch the values.
    // loop on each char, and add the found
    // symbols to the end of queue.
    char c = _stream->read();
    switch (c) {
        case '#':
            lcomment();
            break;
        case ' ':
        case '\t':
        case '\n':
        case '\r':
        case '\f':
        case '\b':
            space();
            break;

            //string
        case '"':
        case '`':
        case '\'':
            string();
            break;

        case '~':
            character();
            break;

            //compound.
        case '[':
        case '(':
        case '{':
            copen();
            break;

        case ']':
        case ')':
        case '}':
            cclose();
            break;
        case 0:
            _has = false;
            break;
        default: // word fetch until the next space.
            if (c >= 32 && c <= 126)
                word();
    }
}
예제 #3
0
bool SIFLoader::LoadHeader(const char *filename)
{
CFILE *fp;
uint32_t magick;

	ClearIndex();
	
	if (fFP) cclose(fFP);
	fp = fFP = copen(filename, "rb");
	
	if (!fp)
	{
		NX_ERR("SIFLoader::LoadHeader: failed to open file '%s'\n", filename);
		return 1;
	}
	
	if ((magick = cgetl(fp)) != SIF_MAGICK)
	{
		NX_ERR("SIFLoader::LoadHeader: magick check failed--this isn't a SIF file or is wrong version?\n");
		NX_ERR(" (expected %08x, got %08x)\n", SIF_MAGICK, magick);
		return 1;
	}
	
	int nsections = cgetc(fp);
	NX_LOG("SIFLoader::LoadHeader: read index of %d sections\n", nsections);
	
	for(int i=0;i<nsections;i++)
	{
		SIFIndexEntry *entry = new SIFIndexEntry;
		
		entry->type = cgetc(fp);		// section type
		entry->foffset = cgetl(fp);		// absolute offset in file
		entry->length = cgetl(fp);		// length of section data
		entry->data = NULL;				// we won't load it until asked
		
		fIndex.AddItem(entry);
		//NX_LOG("  - Sect%02d @ %04x\n", entry->type, entry->foffset);
	}
	
	// ..leave file handle open, its ok
	return 0;
}
예제 #4
0
static void
run(svsem_t *sem, int n, int verbose)
{
	int i, rst;
	char buf[255];

	for (i = 0; i < n; i++) {
		sprintf(buf, "/tmp/file.%d", i);
		svsem_wait(sem);

		if (copen(buf, O_CREAT, 0600, &rst) == -1) {
			MMSG("");
		} else if (verbose) {
printf("%d %d: %d %s\n", getpid(), i, rst, buf);
		}

		usleep(100);
	}
	
}
예제 #5
0
파일: open.c 프로젝트: andreiw/polaris
int
openat32(int fd, char *path, int fmode, int cmode)
{
	return (copen(fd, path, OPENMODE32(fmode), cmode));
}
예제 #6
0
파일: open.c 프로젝트: andreiw/polaris
int
creat32(char *fname, int cmode)
{
	return (copen(AT_FDCWD, fname, CREATMODE32, cmode));
}
예제 #7
0
파일: open.c 프로젝트: andreiw/polaris
/*
 * Open and Creat for 32-bit compatibility on 64-bit kernel
 */
int
open32(char *fname, int fmode, int cmode)
{
	return (copen(AT_FDCWD, fname, OPENMODE32(fmode), cmode));
}
예제 #8
0
파일: open.c 프로젝트: andreiw/polaris
int
creat64(char *fname, int cmode)
{
	return (copen(AT_FDCWD, fname, CREATMODE64, cmode));
}
예제 #9
0
int main(int argc, char **argv)
{
	cfile out_cfh, ref_cfh, ver_cfh;
	int out_fh;

	int optr;
	char  *src_file = NULL;
	char  *trg_file = NULL;
	char  *patch_name = NULL;
	unsigned long patch_id = 0;
	signed long encode_result=0;
	int err;
	unsigned long sample_rate=0;
	unsigned long seed_len = 0;
	unsigned long hash_size = 0;
//	unsigned int patch_compressor = 0;
	unsigned int patch_to_stdout = 0;

	#define DUMP_USAGE(exit_code)		\
		print_usage("differ", "src_file trg_file [patch_file|or to stdout]", help_opts, exit_code);

	while((optr = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
		switch(optr) {
		case OVERSION:
			print_version("differ");		exit(0);
		case OUSAGE:
		case OHELP:
			DUMP_USAGE(0);
		case OVERBOSE:
			global_verbosity++;				break;
		case OSAMPLE:
			sample_rate = atol(optarg);
			if(sample_rate == 0 || sample_rate > MAX_SAMPLE_RATE) DUMP_USAGE(EXIT_USAGE);
			break;
		case OSEED:
			seed_len = atol(optarg);
			if(seed_len == 0 || seed_len > MAX_SEED_LEN) DUMP_USAGE(EXIT_USAGE);
			break;
		case OHASH:
			hash_size = atol(optarg);
			if(hash_size == 0 || hash_size > MAX_HASH_SIZE) DUMP_USAGE(EXIT_USAGE);
			break;
		case OSTDOUT:
			patch_to_stdout = 1;		break;
		case 'f':
			patch_format = optarg;		break;
		default:
			v0printf("invalid arg- %s\n", argv[optind]);
			DUMP_USAGE(EXIT_USAGE);
		}
	}
	err = 0;
	if( ((src_file = (char *)get_next_arg(argc,argv)) == NULL) ||
		(err = copen(&ref_cfh, src_file, NO_COMPRESSOR, CFILE_RONLY)) != 0) {
		if(src_file) {
			if(err == MEM_ERROR) {
				v0printf("alloc failure for src_file\n");
			} else {
				v0printf("Must specify an existing source file.\n");
			}
			exit(EXIT_USAGE);
		}
		DUMP_USAGE(EXIT_USAGE);
	}
	err = 0;
	if( ((trg_file=(char *)get_next_arg(argc, argv)) == NULL) ||
		(err = copen(&ver_cfh, trg_file, NO_COMPRESSOR, CFILE_RONLY)) != 0) {
		if(trg_file) {
			if(err == MEM_ERROR) {
				v0printf("alloc failure for trg_file\n");
			} else {
				v0printf("Must specify an existing target file.\n");
			}
			exit(EXIT_USAGE);
		}
		DUMP_USAGE(EXIT_USAGE);
	}
	if(patch_to_stdout != 0) {
		out_fh = 1;
	} else {
		if((patch_name = (char *)get_next_arg(argc, argv)) == NULL)
			DUMP_USAGE(EXIT_USAGE);
		if((out_fh = open(patch_name, O_WRONLY | O_TRUNC | O_CREAT, 0644))==-1) {
			v0printf( "error creating patch file '%s' (open failed)\n", patch_name);
			exit(1);
		}
	}
	if (NULL!=get_next_arg(argc, argv)) {
		DUMP_USAGE(EXIT_USAGE);
	}

	if(patch_format==NULL) {
		patch_id = DEFAULT_PATCH_ID;
	} else {
		patch_id = check_for_format(patch_format, strlen(patch_format));
		if(patch_id==0) {
			v0printf( "Unknown format '%s'\n", patch_format);
			exit(EXIT_FAILURE);
		}
	}
	if(copen_dup_fd(&out_cfh, out_fh, 0, 0, NO_COMPRESSOR /* patch_compressor */, CFILE_WONLY)) {
		v0printf("error allocing needed memory for output, exiting\n");
		exit(EXIT_FAILURE);
	}

	v1printf("using patch format %lu\n", patch_id);
	v1printf("using seed_len(%lu), sample_rate(%lu), hash_size(%lu)\n", 
		seed_len, sample_rate, hash_size);
	v1printf("verbosity level(%u)\n", global_verbosity);
	v1printf("initializing Command Buffer...\n");

	encode_result = simple_difference(&ref_cfh, &ver_cfh, &out_cfh, patch_id, seed_len, sample_rate, hash_size);
	v1printf("flushing and closing out file\n");
	cclose(&out_cfh);
	close(out_fh);
	if(err) {
		if(! patch_to_stdout) {
			unlink(patch_name);
		}
	}
	v1printf("encode_result=%ld\n", encode_result);
	v1printf("exiting\n");
	v1printf("closing reference file\n");
	cclose(&ref_cfh);
	v1printf("closing version file\n");
	cclose(&ver_cfh);
	close(out_fh);
	check_return2(encode_result, "encoding result was nonzero")
	return 0;
}
예제 #10
0
파일: load.c 프로젝트: jurchik/project6014
BOOLEAN
LoadGame (COUNT which_game, SUMMARY_DESC *SummPtr)
{
	uio_Stream *in_fp;
	char file[PATH_MAX];
	char buf[256];
	SUMMARY_DESC loc_sd;
	GAME_STATE_FILE *fp;
	DECODE_REF fh;
	COUNT num_links;
	STAR_DESC SD;
	ACTIVITY Activity;

	sprintf (file, "starcon2.%02u", which_game);
	in_fp = res_OpenResFile (saveDir, file, "rb");
	if (!in_fp)
		return FALSE;

	if (!LoadSummary (&loc_sd, in_fp))
	{
		log_add (log_Error, "Warning: Savegame is corrupt");
		res_CloseResFile (in_fp);
		return FALSE;
	}

	if (!SummPtr)
	{
		SummPtr = &loc_sd;
	}
	else
	{	// only need summary for displaying to user
		memcpy (SummPtr, &loc_sd, sizeof (*SummPtr));
		res_CloseResFile (in_fp);
		return TRUE;
	}

	// Crude check for big-endian/little-endian incompatibilities.
	// year_index is suitable as it's a multi-byte value within
	// a specific recognisable range.
	if (SummPtr->year_index < START_YEAR ||
			SummPtr->year_index >= START_YEAR +
			YEARS_TO_KOHRAH_VICTORY + 1 /* Utwig intervention */ +
			1 /* time to destroy all races, plenty */ +
			25 /* for cheaters */)
	{
		log_add (log_Error, "Warning: Savegame corrupt or from "
				"an incompatible platform.");
		res_CloseResFile (in_fp);
		return FALSE;
	}

	GlobData.SIS_state = SummPtr->SS;

	if ((fh = copen (in_fp, FILE_STREAM, STREAM_READ)) == 0)
	{
		res_CloseResFile (in_fp);
		return FALSE;
	}

	ReinitQueue (&GLOBAL (GameClock.event_q));
	ReinitQueue (&GLOBAL (encounter_q));
	ReinitQueue (&GLOBAL (ip_group_q));
	ReinitQueue (&GLOBAL (npc_built_ship_q));
	ReinitQueue (&GLOBAL (built_ship_q));

	memset (&GLOBAL (GameState[0]), 0, sizeof (GLOBAL (GameState)));
	Activity = GLOBAL (CurrentActivity);
	LoadGameState (&GlobData.Game_state, fh);
	NextActivity = GLOBAL (CurrentActivity);
	GLOBAL (CurrentActivity) = Activity;

	LoadRaceQueue (fh, &GLOBAL (avail_race_q));
	// START_INTERPLANETARY is only set when saving from Homeworld
	//   encounter screen. When the game is loaded, the
	//   GenerateOrbitalFunction for the current star system will
	//   create the encounter anew and populate the npc queue.
	if (!(NextActivity & START_INTERPLANETARY))
	{
		if (NextActivity & START_ENCOUNTER)
			LoadShipQueue (fh, &GLOBAL (npc_built_ship_q));
		else if (LOBYTE (NextActivity) == IN_INTERPLANETARY)
			// XXX: Technically, this queue does not need to be
			//   saved/loaded at all. IP groups will be reloaded
			//   from group state files. But the original code did,
			//   and so will we until we can prove we do not need to.
			LoadGroupQueue (fh, &GLOBAL (ip_group_q));
		else
			// XXX: The empty queue read is only needed to maintain
			//   the savegame compatibility
			LoadEmptyQueue (fh);
	}
	LoadShipQueue (fh, &GLOBAL (built_ship_q));

	// Load the game events (compressed)
	cread_16 (fh, &num_links);
	{
#ifdef DEBUG_LOAD
		log_add (log_Debug, "EVENTS:");
#endif /* DEBUG_LOAD */
		while (num_links--)
		{
			HEVENT hEvent;
			EVENT *EventPtr;

			hEvent = AllocEvent ();
			LockEvent (hEvent, &EventPtr);

			LoadEvent (EventPtr, fh);

#ifdef DEBUG_LOAD
		log_add (log_Debug, "\t%u/%u/%u -- %u",
				EventPtr->month_index,
				EventPtr->day_index,
				EventPtr->year_index,
				EventPtr->func_index);
#endif /* DEBUG_LOAD */
			UnlockEvent (hEvent);
			PutEvent (hEvent);
		}
	}

	// Load the encounters (black globes in HS/QS (compressed))
	cread_16 (fh, &num_links);
	{
		while (num_links--)
		{
			HENCOUNTER hEncounter;
			ENCOUNTER *EncounterPtr;

			hEncounter = AllocEncounter ();
			LockEncounter (hEncounter, &EncounterPtr);

			LoadEncounter (EncounterPtr, fh);

			UnlockEncounter (hEncounter);
			PutEncounter (hEncounter);
		}
	}

	// Copy the star info file from the compressed stream
	fp = OpenStateFile (STARINFO_FILE, "wb");
	if (fp)
	{
		DWORD flen;

		cread_32 (fh, &flen);
		while (flen)
		{
			COUNT num_bytes;

			num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen;
			cread (buf, num_bytes, 1, fh);
			WriteStateFile (buf, num_bytes, 1, fp);

			flen -= num_bytes;
		}
		CloseStateFile (fp);
	}

	// Copy the defined groupinfo file from the compressed stream
	fp = OpenStateFile (DEFGRPINFO_FILE, "wb");
	if (fp)
	{
		DWORD flen;

		cread_32 (fh, &flen);
		while (flen)
		{
			COUNT num_bytes;

			num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen;
			cread (buf, num_bytes, 1, fh);
			WriteStateFile (buf, num_bytes, 1, fp);

			flen -= num_bytes;
		}
		CloseStateFile (fp);
	}

	// Copy the random groupinfo file from the compressed stream
	fp = OpenStateFile (RANDGRPINFO_FILE, "wb");
	if (fp)
	{
		DWORD flen;

		cread_32 (fh, &flen);
		while (flen)
		{
			COUNT num_bytes;

			num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen;
			cread (buf, num_bytes, 1, fh);
			WriteStateFile (buf, num_bytes, 1, fp);

			flen -= num_bytes;
		}
		CloseStateFile (fp);
	}

	LoadStarDesc (&SD, fh);

	cclose (fh);
	res_CloseResFile (in_fp);

	EncounterGroup = 0;
	EncounterRace = -1;

	ReinitQueue (&race_q[0]);
	ReinitQueue (&race_q[1]);
	CurStarDescPtr = FindStar (NULL, &SD.star_pt, 0, 0);
	if (!(NextActivity & START_ENCOUNTER)
			&& LOBYTE (NextActivity) == IN_INTERPLANETARY)
		NextActivity |= START_INTERPLANETARY;

	return TRUE;
}