Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

function getFileExt

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

function getFileExt

Whilst working on a small project I needed a function to get file extension so i've written a quick small function, i thought i'd share it in case someone else needs it for now, i will probably add this to the library for the next release.

function getFileExt(char* path) {
	eax = path;		
	while (char[eax] != 0) {
		eax++;
	}
	while (char[eax] != ord('.')) {
		eax--;
	}
	eax++;
}

the usage is pretty simple, the function will take the string input find the .ext and return a pointer to the string with the offset, so bare in mind it still is pointing to the string you pass.

const myPath = 'C:/some folder/another folder/file.exe';
eax = getFileExt(myPath);
println(eax);

output:

exe

this can and will be improved when i see the need to or unless someone else beats me to it :P :)

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

changed a little so if there is no "." eax will point to the end null char meaning a blank :)

function getFileExt(char* path) {
	eax = path;
	ecx = xor;
	while (char[eax] != 0) {
		if (char[eax] == ".") {
			ecx = eax;
		}
		eax++;
	}
	
	if (ecx) {
		eax = ecx;
		eax++;
	}
}

note you may also add the uses ecx clause.

function getFileExt(char* path) {
	uses ecx;
	eax = path;
	ecx = xor;
	while (char[eax] != 0) {
		if (char[eax] == ".") {
			ecx = eax;
		}
		eax++;
	}
	
	if (ecx) {
		eax = ecx;
		eax++;
	}
}
Colin admin
Posts
928
Threads
49
Location
Bulgaria

The latest version of this sample function is as follows:

//
// Copyright (c) OverHertz Ltd.
//
function getFileExt(char* path) {
	edx = path;
	ecx = edx;

	al = [edx];
	edx += 1;
	
	if (al != 0) {
		repeat {
			cmp al, "."
			cmovz ecx, edx

			al = [edx];
			edx += 1;
		} until (al == 0);
	}

	eax = ecx;
}

This is already included in the include library, but posted for sample sake :)

Colin admin
Posts
928
Threads
49
Location
Bulgaria

yet another update:

function getFileExt(char* path) {
	edx = path;
	al = [edx];
	
	if (al != 0) {
		ecx = edx;
		al = [edx+1];
		edx += 2;

		repeat {
			cmp al, "."
			cmovz ecx, edx

			al = [edx];
			edx += 1;
		} until (al == 0);

		return ecx;
	}

	eax = edx;
}

this one is even faster.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

1 thing i missed that was pointed out to me is a filename that starts with the .

here is the correction :)

function getFileExt(char* path) {
  edx = path;
  al = [edx];
  
  if (al != 0) {
    ecx = edx;
    edx += 1;

    repeat {
      cmp al, "."
      cmovz ecx, edx

      al = [edx];
      edx += 1;
    } until (al == 0);

    return ecx;
  }

  eax = edx;
}