int
main
	(
	int argc,
	char** argv
	)
{
	JInitCore();

	JChooseSaveFile* csf = JGetChooseSaveFile();

	JString resultStr;

	if (csf->ChooseFile("Name of file:", NULL, &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	if (csf->ChooseFile("Name of file:", "Please select a file...", &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	if (csf->ChooseRPath("", "Please select a directory...", NULL, &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	if (csf->ChooseRWPath("", "Please select a writable directory:", NULL, &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	if (csf->SaveFile("Save file as:", NULL, "junk", &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	if (csf->SaveFile("Save file as:", "Please save the file...", "more junk", &resultStr))
		{
		std::cout << resultStr << std::endl;
		JWaitForReturn();
		}

	return 0;
}
int
main
	(
	int argc,
	char** argv
	)
{
	JInitCore();

	JString fileName;
	if (!(JGetChooseSaveFile())->SaveFile("Save file as:", NULL, "junk", &fileName))
		{
		return 0;
		}

	ofstream file0(fileName);
	file0 << "0123456789";
	file0.close();		// force write to disk
	fstream file1(fileName, kJTextFile);
	cout << "Length of file (10): " << JGetFStreamLength(file1) << endl;

	fstream* file2 = JSetFStreamLength(fileName, file1, 20, kJTextFile);
	cout << "Old fstream open? (0) " << (file1.rdbuf())->is_open() << endl;
	cout << "Length of file (20): " << JGetFStreamLength(*file2) << endl;

	fstream* file3 = JSetFStreamLength(fileName, *file2, 5, kJTextFile);
	cout << "Old fstream open? (0) " << (file2->rdbuf())->is_open() << endl;
	cout << "Length of file (5): " << JGetFStreamLength(*file3) << endl;

	file3->close();

	ofstream file4(fileName);
	file4.close();

	file1.open(fileName, kJTextFile);
	cout << "default open of ofstream should erase file" << endl;
	cout << "Length of file (0): " << JGetFStreamLength(file1) << endl;
	file1.close();

	remove(fileName);

	return 0;
}
int
main
	(
	int		argc,
	char*	argv[]
	)
{
	if (argc == 1)
		{
		char buffer[BUFSIZ];
		while (1)
			{
			ssize_t count = read(0, buffer, sizeof(buffer));
			if (count < 0 && jerrno() == EINTR)
				{
				// keep going
				}
			else if (count <= 0)
				{
				break;
				}
			else
				{
//				cerr << count << endl;
				cout.write(buffer, count);
				JWait(0.01);
				}
			}
		return 0;
		}

	JProcess* p;
	int fromFD;
	const JError err = JProcess::Create(&p, argv[1],
										kJIgnoreConnection, NULL,
										kJForceNonblockingPipe, &fromFD,
										kJAttachToFromFD, NULL);
	if (err.OK())
		{
		char buffer[BUFSIZ];
		while (1)
			{
			ssize_t count = read(fromFD, buffer, sizeof(buffer));
			if (count < 0 && jerrno() == EINTR)
				{
				// keep going
				}
			else if (count <= 0)
				{
				break;
				}
			else
				{
//				cerr << count << endl;
				cout.write(buffer, count);
				JWait(0.02);
				}
			}
/*
		ProcessLink* link = new ProcessLink(fromFD);
		assert( link != NULL );

		ACE_Time_Value delta(20);
		ACE_Reactor::instance()->run_reactor_event_loop(delta);

		link->Dump();
		delete link;
*/
		return 0;
		}
	else
		{
		JInitCore();
		err.ReportIfError();
		return 1;
		}
}
JError
CBSearchDocument::Create
	(
	const JPtrArray<JString>&	fileList,
	const JPtrArray<JString>&	nameList,
	const JCharacter*			searchStr,
	const JBoolean				onlyListFiles,
	const JBoolean				listFilesWithoutMatch
	)
{
	assert( !fileList.IsEmpty() );
	assert( fileList.GetElementCount() == nameList.GetElementCount() );

	int fd[2];
	JError err = JCreatePipe(fd);
	if (!err.OK())
		{
		return err;
		}

	pid_t pid;
	err = JThisProcess::Fork(&pid);
	if (!err.OK())
		{
		return err;
		}

	// child

	else if (pid == 0)
		{
		close(fd[0]);

		// get rid of JXCreatePG, since we must not use X connection
		// (binary files may trigger it)
		JInitCore();

		CBSearchTE te;
		JOutPipeStream output(fd[1], kJTrue);
		te.SearchFiles(fileList, nameList,
					   onlyListFiles, listFilesWithoutMatch,
					   output);
		output.close();
		exit(0);
		}

	// parent

	else
		{
		close(fd[1]);

		JProcess* process = jnew JProcess(pid);
		assert( process != NULL );

		const JCharacter* map[] =
			{
			"s", searchStr
			};
		const JString windowTitle = JGetString("SearchTitle::CBSearchDocument", map, sizeof(map));

		CBSearchDocument* doc =
			jnew CBSearchDocument(kJFalse, JI2B(onlyListFiles || listFilesWithoutMatch),
								 fileList.GetElementCount(),
								 process, fd[0], windowTitle);
		assert( doc != NULL );
		doc->Activate();

		RecordLink* link;
		const JBoolean ok = doc->GetRecordLink(&link);
		assert( ok );
		CBSearchTE::SetProtocol(link);
		}

	return JNoError();
}