⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sbpad.c

📁 smallbasic for linux
💻 C
📖 第 1 页 / 共 5 页
字号:
	int			recs, i, len;	ErrNonFatalDisplayIf(!curFile, "BuildSecList(): database is not opened");	recs = dbRecCount(curFile);	secList = tmp_alloc(sizeof(sec_t) * recs);	secListStr = tmp_alloc(sizeof(char_p) * recs);	for ( i = 0; i < recs; i ++ )	{		len = dbRecSize(curFile, i);		ptr = tmp_alloc(len);		dbRead(curFile, i, ptr, len);		if	( *ptr == 'S' )	{			MemMove(&secList[secCount], ptr, sizeof(sec_t));			secListStr[secCount] = secList[secCount].name;			secCount ++;			}		tmp_free(ptr);		}}/**/void destroySecList() SEC(IDE);void destroySecList(){	if	( secList )	{		tmp_free(secListStr);		tmp_free(secList);		secListStr = NULL;		secList = NULL;		secCount = 0;		}}/**	destroy the list of user-filenames*///static void destroyUserFileList() SEC(IDE);void	destroyUserFileList(){	int		i;	if	( ufstTable )	{		for ( i = 0; i < ufstCount; i ++ )			MemPtrFree(ufstTable[i]);		tmp_free(ufstTable);		ufstTable = NULL;		ufstCount = 0;		}}/**/static void buildUserFileList() SEC(IDE);static void buildUserFileList(){	int			dbCount, i;	dword		dbType, dbCreator;	LocalID		LID;	destroyUserFileList();	dbCount = DmNumDatabases(0);	ufstTable = tmp_alloc(dbCount * sizeof(char_p));	ufstCount = 0;	for ( i = 0; i < dbCount; i++ )	{		LID = DmGetDatabase(0, i);				if	( LID )	{			temp[0] = '\0';			if	( DmDatabaseInfo(0, LID,				temp,				NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,				&dbType, &dbCreator ) == 0 )	{				if ( dbCreator == SmBa && dbType == idUFST ) {					ufstTable[ufstCount] = (char *) MemPtrNew(StrLen(temp)+1);					ErrNonFatalDisplayIf(!ufstTable[ufstCount], "out of memory");					StrCopy(ufstTable[ufstCount], temp);					ufstCount ++;					} // dbType				} // DmDatabaseInfo			} // LID		} // for	if	( ufstCount == 0 )	{		ufstTable[ufstCount] = (char *) MemPtrNew(33);		ErrNonFatalDisplayIf(!ufstTable[ufstCount], "out of memory");		StrCopy(ufstTable[ufstCount], "NewFile");		ufstCount ++;		}}/**/mem_t	loadSection(word secIndex) SEC(IDE);mem_t	loadSection(word secIndex){	word		len, rec_n;	sec_t		*sec_p;	mem_t		h;	char		*ptr;	fatal(!curFile, "loadSection(): database is not opened");	fatal(secIndex > secCount, "loadSection(): page out of range");	rec_n = secIndex + 1;	sec_p = &secList[secIndex];	len = dbRecSize(curFile, rec_n);	h = mem_alloc(len - sizeof(sec_t));	ptr = mem_lock(h);	dbReadSeg(curFile, rec_n, ptr, sizeof(sec_t), len - sizeof(sec_t));	mem_unlock(h);	return h;}/**/mem_t	loadFSV(const char *name) SEC(IDE);mem_t	loadFSV(const char *name){	mem_t		h = NULL;	char		*ptr;	dword		len;	FileHand	f;	Err			ferr;	f = FileOpen(0, (char *)name, idUFST, SmBa, fileModeReadOnly, &ferr);	if	( ferr == 0 )	{		FileTell(f, &len, &ferr);		if	( len > 32767 )	{			len = 32767;			warning("File size > 32KB");			}		h = mem_alloc(len+1);		ptr = mem_lock(h);		FileRead(f, ptr, len, 1, &ferr);		ptr[len] = '\0';		FileClose(f);		mem_unlock(h);		}	return h;}/**/int		createNewFSV(const char *name) SEC(IDE);int		createNewFSV(const char *name){	FileHand	f;	Err			ferr;	f = FileOpen(0, (char *)name, idUFST, SmBa, fileModeAppend, &ferr);	if	( ferr == 0 )			FileClose(f);	return (ferr != 0);}/**/int		saveFSV(const char *name) SEC(IDE);int		saveFSV(const char *name){	char		*text;	FileHand	f;	Err			ferr;	FileDelete(0, (char *)name);	f = FileOpen(0, (char *)name, idUFST, SmBa, fileModeAppend, &ferr);	if	( ferr == 0 )	{		text = FldGetTextPtr(fld_ptr(fldTEXT));		FileWrite(curFile, text, StrLen(text), 1, &ferr);		FileClose(f);		}	return (ferr != 0);}/**/void	openDOC(const char *fileName) SEC(IDE);void	openDOC(const char *fileName){	if	( curFile )		dbClose(curFile);	modified = 0;	curFile = dbOpen(fileName);	// header	dbRead(curFile, 0, &curFileHeader, sizeof(curFileHeader));	// build the section list	destroySecList();	buildSecList();}/**/void	closeDOC() SEC(IDE);void	closeDOC(){	if	( curFile )	{		word	attr;			destroySecList();		dbClose(curFile);		if	( modified )	{			LocalID		lid;			lid = DmFindDatabase(0, (char *) curFileName);			if	( lid )	{				DmDatabaseInfo(0, lid, NULL, &attr, NULL, NULL,  NULL, NULL, NULL, NULL, NULL, NULL, NULL);				attr |= dmHdrAttrBackup;				DmSetDatabaseInfo(0, lid, NULL, &attr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);				modified = 0;				}			}		curFile = 0;		}}/**/void	closeFSV() SEC(IDE);void	closeFSV(){	destroyUserFileList();}/**	Creates an empty doc*/int		createNewDOC(const char *fileName) SEC(IDE);int		createNewDOC(const char *fileName){	file_t		f;	sec_t		sec;	char		*defText;	ULong		dts;	DateTimeType cur_date;	word		rec_n;	if	( dbExist(fileName) )	{		if	( FrmCustomAlert(ConfAlertID, "Overwrite '", (char *)fileName, "' ?") == 0 )				dbRemove(fileName);		else			return 0;		}	// default text 	defText = tmp_alloc(64);	dts = TimGetSeconds();	TimSecondsToDateTime(dts, &cur_date);	StrPrintF(defText, "' %s\n' %02d/%02d/%04d\n", fileName, (int) cur_date.day, (int) cur_date.month, cur_date.year);	// create & open	f = dbOpenAlw(fileName, SmBa /* idBASC */, idTEXT);	// write header	curFileHeader.sign = 'H';	curFileHeader.version = 3;	curFileHeader.category = (curCat == catUnfiledID) ? 0 : curCat;	dbWrite(f, 0xFFFF, &curFileHeader, sizeof(curFileHeader));	// write section	sec.sign = 'S';	sec.version = 3;	sec.flags = 1;	StrCopy(sec.name, "Main");	rec_n = dbWriteSeg(f, 0xFFFF, 0, &sec, sizeof(sec_t));	dbWriteSeg(f, rec_n, sizeof(sec_t), defText, StrLen(defText)+1);	// close	dbClose(f);	tmp_free(defText);	return 1;}/**	returns the document category*/dword	fastGetDocCat(LocalID lid){	file_t		f;	info_t		hdr;		// open	f = DmOpenDatabase(0, lid, dmModeReadWrite);	fatal(f == 0, "fastGetDocCat: can't open file");	if	( f )	{		dbRead(f, 0, &hdr, sizeof(info_t));		dbClose(f);		return hdr.category;		}	return 0;}/**/void 	updateDocCat(const char *file, dword category) SEC(IDE);void	updateDocCat(const char *file, dword category){	file_t		f;	info_t		hdr;	f = dbOpen(file);	if	( f )	{		dbRead(f, 0, &hdr, sizeof(info_t));		hdr.category = category;		dbWrite(f, 0, &hdr, sizeof(info_t));		dbClose(f);		}}/**/void	saveSection(word secIndex) SEC(IDE);void	saveSection(word secIndex){	char		*text;	fatal(!curFile, "saveSection(): database is not opened");	fatal(secIndex > secCount, "saveSection(): page out of range");	text = FldGetTextPtr(fld_ptr(fldTEXT));	dbWriteSeg(curFile, secIndex + 1, sizeof(sec_t), text, StrLen(text)+1);	modified = 1;	// needed to set the backup field}/**/void	saveSectionName(word secIndex, char *name) SEC(IDE);void	saveSectionName(word secIndex, char *name){	dbWriteSeg(curFile, secIndex + 1, sizeof(sec_t)-64, name, StrLen(name)+1);	modified = 1;	// needed to set the backup field}/**/void 	createNewSec(char *name) SEC(IDE);void	createNewSec(char *name){	word		rec_n;	sec_t		sec;	char		defText[64];	fatal(!curFile, "createNewSection(): database is not opened");	mkSmallTitle(name, defText, 55, stdFont);	StrCopy(sec.name, defText);	StrCopy(defText, "' ");	StrCat(defText, name);	// write section	sec.sign = 'S';	sec.flags = 0;	rec_n = dbWriteSeg(curFile, 0xFFFF, 0, &sec, sizeof(sec_t));	dbWriteSeg(curFile, rec_n, sizeof(sec_t), defText, StrLen(defText)+1);}/**/void	deleteSec(word idx) SEC(IDE);void	deleteSec(word idx){	fatal(!curFile, "deleteSec(): database is not opened");	if	( idx > 0 )			{ fatal( (DmRemoveRecord(curFile, idx+1) != 0), "deleteSec(): can't delete record"); }	else		FrmCustomAlert(InfoAlertID, "You cannot delete the main section", "", "");}void	checkSaveDOC() SEC(IDE);void	checkSaveDOC(){	if	( curFile )	{		if	( FldDirty(fld_ptr(fldTEXT)) )	{			if	( FrmCustomAlert(ConfAlertID, "The text has been modified. Save now ?", "", "") == 0 )	{				FldCompactText(fld_ptr(fldTEXT));				saveSection(curSection);				}			}		}}void	checkSaveFSV() SEC(IDE);void	checkSaveFSV(){	if	( FldDirty(fld_ptr(fldTEXT)) )	{		if	( FrmCustomAlert(ConfAlertID, "The text has been modified. Save now ?", "", "") == 0 )	{			FldCompactText(fld_ptr(fldTEXT));			saveFSV(curFSVName);			}		}}/**/int		dispSection(word secIndex) SEC(IDE);int		dispSection(word secIndex){	mem_t	new_h;	fatal(!curFile, "dispSection(): database is not opened");	if ( secIndex < secCount )	{		checkSaveDOC();		new_h = loadSection(secIndex);		curSection = secIndex;		setFieldHandle(fldTEXT, new_h, 1);		return 0;		}	return 1;}/**/int		dispFSV(const char *name) SEC(IDE);int		dispFSV(const char *name){	mem_t	new_h;	char	*ptr;	if	( dbExist(name) )	{		// checkSaveDOC();		new_h = loadFSV(name);//		StrCopy(curFSVName, name);		}	else	{		new_h = mem_alloc(128);		ptr = mem_lock(new_h);		StrPrintF(ptr, "File '%s' not found!", name);		mem_unlock(new_h);		}	setFieldHandle(fldTEXT, new_h, 1);	return 0;}void	setTextPos(word pos) SEC(IDE);void	setTextPos(word pos){	FieldPtr	fp;	fp = fld_ptr(fldTEXT);	FldSetScrollPosition(fp, pos);	FldSetInsPtPosition(fp, pos);//	FldSetInsertionPoint(fp, pos);	FldDrawField(fp);	FldGrabFocus(fp);}word	getTextPos() SEC(IDE);word	getTextPos(){	return FldGetInsPtPosition(fld_ptr(fldTEXT));}int 	copyDOC(const char *oldName, const char *newName) SEC(IDE);int		copyDOC(const char *oldName, const char *newName){	if	( !dbExist(oldName) )		return 0;    if	( !StrCompare(oldName,newName))	{		FrmCustomAlert(InfoAlertID, "New name '", (char *) newName, "' same as old name.  No action taken.");		return 0;		}	if	( dbExist(newName) )	{		if	( FrmCustomAlert(ConfAlertID, "Overwrite '", (char *) newName, "' ?") != 0 )			return 0;		}	return dbCopy(oldName, newName);}void	renameDOC(const char *oldName, const char *newName) SEC(IDE);void	renameDOC(const char *oldName, const char *newName){	if	( copyDOC(oldName, newName) )		DmDeleteDatabase(0, DmFindDatabase(0, (char *) oldName));}// 0 = failint		Exec(const char *prog) SEC(IDE);int		Exec(const char *prog){	LocalID	lid;	dword	progid;	word	card;	DmSearchStateType	state;	progid  = ((dword) prog[0] << 24) + ((dword) prog[1] << 16) + ((dword) prog[2] << 8) + (dword) prog[3];	if	( progid == idLNCH )	{		EventType	ev;		MemSet(&ev, sizeof(EventType), 0);		ev.eType = keyDownEvent;		ev.data.keyDown.modifiers = commandKeyMask;		ev.data.keyDown.chr = vchrLaunch;		EvtAddEventToQueue(&ev);		return 1;		}	else	{		if	( DmGetNextDatabaseByTypeCreator(true, &state, APPL, progid, true, &card, &lid) == 0 )			return (SysUIAppSwitch(card, lid, sysAppLaunchCmdNormalLaunch, NULL) == 0);		}	return 0;}/**	destroy the list of 'basic-source' filenames*///static void destroyDocList() SEC(IDE);void	destroyDocList(){	int		i;	if	( docTable )	{		for ( i = 0; i < docCount; i ++ )			MemPtrFree(docTable[i]);		tmp_free(docTable);		docTable = NULL;		docCount = 0;		}}/**	build the list of 'basic-source' filenames*///static void buildDocList() SEC(IDE);void	buildDocList(){	int			dbCount, i;	dword		dbType, dbCat, dbCreator;	LocalID		LID;	destroyDocList();	dbCount = DmNumDatabases(0);	docTable = tmp_alloc(dbCount * sizeof(char_p));	docCount = 0;	for ( i = 0; i < dbCount; i++ )	{		LID = DmGetDatabase(0, i);				if	( LID )	{			temp[0] = '\0';			if	( DmDatabaseInfo(0, LID,				temp,				NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,				&dbType, &dbCreator ) == 0 )	{				if ( (dbCreator == idBASC || dbCreator == SmBa) && dbType == idTEXT ) {					dbCat = fastGetDocCat(LID);					if	( curCat == 0 || dbCat == curCat || (dbCat == 0 && curCat == catUnfiledID) )	{						docTable[docCount] = (char *) MemPtrNew(StrLen(temp)+1);						ErrNonFatalDisplayIf(!docTable[docCount], "out of memory");						StrCopy(docTable[docCount], temp);						docCount ++;						}					} // dbType				} // DmDatabaseInfo			} // LID		} // for}void	gotoLine(int line) SEC(IDE);void	gotoLine(int line){	char	*p, *text;	int		c = 1, pos = 0, found = 0;	if	( line > 0 )	{		if	( line == 1 )			setTextPos(0);		text = FldGetTextPtr(fld_ptr(fldTEXT));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -