TEIMSI
Developer's platform of programming text codes
Home|Utilities|Forum|Documentation

Table of contents -> Chapter 2 - Basic examples of using Teimsi

Any of the following "Scripts" examples can be saved into a .TSI file, then edit the file with the TEIMSI editor and compile as ".EXE" program file. Examples can be seen positioned delimited in two rows with separator lines (which are strings that begin with "// ################# ..."), on the editor is just enough to press the F9 button to insert one where the text cursor is located.

List of examples:

Example 1 (program to transform any file in decimal db notation)
Example 2 (Trigonometry Table Builder)
Example 3 (Numerator of lines in text files)
Example 4 (Bitwise Symmetric Encryption)




Example 1

A program to transform any file in decimal db notation


; //########################################################################################################

	//	In this example, the contents of the input file must be limited to text-readable characters. The "Bytes" of the File "file1.txt" will
	//	 be converted into ASCII code numbers in the range of values from 0 to 255, the generated list will be written in the "file2.txt" file.

	var file1="file1.txt"
	var file2="file2.txt"
	
	alert("This program will load the file " + file1 + ", and it will save into the file " + file2 + ", the content of the first one in a decimal data notation.")
	
	if(fileexists(file1)==false){
		alert("Unable to locate the file  "+file1);
		sys.quit()
	}
	if(filesize(file1)>100000){
		alert("The file can not be loaded because it has more than 100 kb in size.");
		sys.quit()
	}
	
	var thedata=binary_read(file1)
	if(thedata==false){
		alert("Error loading "+file1);
		sys.quit()
	}
	var length=len(thedata), pointer=0, bytes_per_line=50, output_text="";
	
	while(pointer<length){
	
		var amount_bytes=min((length-pointer),bytes_per_line)
		output_text+="	db "
		for(var c=0;c<amount_bytes;c++){
			if(c>0){
				output_text+=",";
			}
			output_text+=ascat(thedata, pointer+c);
		}
		output_text+=_nl;		//	Observation: _nl = chr(13)+chr(10)
		pointer+=bytes_per_line
	}
	
	alert("Result of writing file2.txt = " + binary_write(file2, output_text)+_nl+"The task is complete !.")
	
	
@eof_file


; //########################################################################################################


Go to top




Example 2

Trigonometry table maker


; //########################################################################################################

	// For this example the "roundn" function that rounds a decimal number is written. The generated table is stored into a file and then the associated program for
	//  ".txt" files is called (typically notepad).

	alert_ex("A Trigonometry Table will be created.", "Information", _mb_iconinformation)

	var cp_index=0, cp_index_max=90, cp_index_inc=1

	var list="Angle Radians	Sen	Cos	Tan"+_nl+_nl
	
	for(var cp_index=0;cp_index<=cp_index_max;cp_index+=cp_index_inc){

		var cp_rads = cp_index * _PI / 180;

		list+= roundn(cp_index,3)+_c9+ roundn(cp_rads,3)+_c9+roundn(sin(cp_rads),3)+_c9+roundn(cos(cp_rads),3)+_c9+roundn(tan(cp_rads),3)+_nl

	}

	binary_write("table.txt", list)

	openapp("table.txt")


	function roundn(rx_elnum, rx_lascif0){

		var rx_lascif=parseint(rx_lascif0)

		if(rx_lascif<=0){return ron(rx_elnum)}

		if(rx_lascif>14){return rx_elnum}

		var rx_tmp10ec=(10 ** rx_lascif)		//	finds 10 powered to rx_lascif

		return ron(rx_elnum*rx_tmp10ec)/rx_tmp10ec
	}



@eof_file


; //########################################################################################################


Go to top




Example 3

Numerator of lines in text files


; //########################################################################################################

	//	The following program tries to amend three text files, adding a numbering to their lines.

	var arr_files=array("text1.txt","text2.txt","text3.txt")

	for(var t=0;t<count(arr_files);t++){

		var file1=arr_files[t]

		if(fileexists(file1)==false){
			alert_ex("Unable to locate the file " + file1, "Information", _mb_iconinformation);
			continue;
		}
		if(filesize(file1)>10000000){
			alert("The file " + file1 + " has more than 10 million bytes and will not be processed by limiting the used amount of physical memory.")
			continue;
		}
		var arr_lines=explode(_nl,binary_read(file1));

		for(var t2=0;t2<count(arr_lines);t2++){
			arr_lines[t2]=(t2+1) + "- " + arr_lines[t2]
		}

		if(-binary_write(file1, implode(_nl,arr_lines))){
			alert_ex("Unable to write into the file " + file1+ _nl+ "Description of error: "+err_description(err_number())  , "Information", _mb_iconerror);
		}
	}

	alert_ex("Completed the text processing of the files: " + implode(",", arr_files), "Information", _mb_iconinformation)


@eof_file

; //########################################################################################################


Go to top





Example 4

Bitwise Symmetric Encryption


; //########################################################################################################

/*
	The following example is an advanced program that beginners can skip reading.

	Is widely used the symmetric encryption Byte to Byte using the xor instruction. The following simple example works at Bits level, which generally increases the security of any encryption algorithm. (A byte consists of eight bits: a Bit (Binary digit) can only be zero or one).
*/


includec(engine\internal\simple_rsrc.asm)

main()
	
	function main(){

		if(alert_ex("Successively a symmetric encryption will be applied to a file"+_nl+"You must specify the password, the file to load and file to save"+_nl+"continue?", "Information", _mb_iconinformation+_mb_yesno)!=_mbret_yes){
			return
		}

		var pass=inputdlgx("Password?", "Required information:","1234")
	
		var filename1 = opfopen(".","","all files *.*"+chr(0)+"*.*");
		if(len(filename1)==0){ return }
	
		var filename2 = opfsave(".","","all files *.*"+chr(0)+"*.*");
		if(len(filename2)==0){ return }
	
		if(-fileexists(filename1)){alert("Unable to locate the file  "+filename1);return}

		if(fileexists(filename2)){
			if(alert_ex("The file " + filename2 + "Already exists, overwrite it?", "Information", _mb_iconinformation+_mb_yesno)!=_mbret_yes){
				return
			}
		}

		if(filesize(filename1)>10000000){alert("The file " + filename1 + " has over 10 million bytes and will not be processed due to its size.");return}

		var cont=binary_read(filename1)

		if(len(cont)==0){
			alert("The file " + filename1 + " is empty.")
			return
		}
		var hash1=hashver(pass)			//	hashver returns a "hash", a unique long integer as function of a string.

		hash1=(hash1 ^ hashver(parsestr(hash1)))		//	Will perform the calculation of a single integer as a function of the password using the Xor operator used for long integers.

		var bitstr=binstrpack(cont)		//	The binstrpack function converts any string into another eight times its size whose bytes are zeros and ones 
							//	 that represent the original string bits.
		_direct{
			mov ebx, [hash1+reg.vo]	;	It's loaded the unique integer determined according to the password into "ebx".
			mov eax, [bitstr+reg.vo]	;	The string's handler is a long integer (index) placed in the "reg.vo" offset which is 8.
			NWPOS_eax			;	The NWPOS_eax macro loads into "esi" the pointer in memory to the start of the string.

			mov edx, [bitstr+reg.nu]	;	It loads into edx the string's length, which in this case is multiple of 8 and not zero.
			lea edi, [esi+edx-1]		;	Loading into the edi pointer the last byte of the string.
			shr edx, 1			;	Divide "edx" half.
			enc_sample1:
				mov ah, [esi]
				mov al, [edi]

				mov cl, bl		;	Puts into the low bit "cl" a bit used to encrypt.
				shr cl, 3
				xor cl, bl

				xor al, cl		;	The lowest bit in memory is encrypted (the other bits are ignored by the "binstrunpack" function).
				xor ah, cl

				test bl, bl		;	If the count of 1 bits is odd, then exchange the current bit and the symmetric respect the center of the string.
				jp @f
					xchg al, ah
				@@:

				mov [esi], ah
				mov [edi], al

				mov cl, bl		;	In these instructions a task is performed , whose importance lies (for our simple example of encryption)
				xor cl, dl		;	 in to modify the ebx register "rotating its bits" to the left as a function of the position of the bit to encrypt and the same register.
				rol ebx, cl		;		and cl, 31		;	Is a redundant instruction that doesn't alter the behavior of "rol".
				inc ebx		;	Secures a little bit more the algorithm by increasing the "ebx" register.
			inc esi
			dec edi
			dec edx
			jnz enc_sample1
		}
		var cont2=binstrunpack(bitstr)	//	The "binstrunpack" function performs the inverse task "binstrpack", takes a string with bytes zero and one (although all are allowed 
							//	for only uses the lowest bit value) of multiple length of 8 bytes and creates a string whose bits 
							//	value what the original string bytes do.
		
		if(-binary_write(filename2, cont2)){
			alert_ex("Unable to write to the file " + filename2+ _nl+ "Description of error: "+err_description(err_number())  , "Information", _mb_iconerror);
		}else{
			alert_ex("It was successfully created the processed file as " + filename2, "Information", _mb_iconinformation)
		}

	
	}


@eof_file

; //########################################################################################################


Go to top