Register | Login
Forum Index > Samples > function getFileExt
Author Message
Pages: 1
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[64] function getFileExt - posted: 2011-09-16 19:41:59
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.

Code:
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.

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


output: Code:
exe


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

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[67] - posted: 2011-09-17 18:03:42
changed a little so if there is no "." eax will point to the end null char meaning a blank smile

Code:
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.

Code:
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++;
  }
}


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1248] - posted: 2012-12-06 20:49:49
The latest version of this sample function is as follows:

Code:
//
// 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 smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1255] - posted: 2012-12-09 22:12:13
yet another update:

Code:
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.

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1331] - posted: 2013-02-17 01:31:38
1 thing i missed that was pointed out to me is a filename that starts with the .

here is the correction smile

Code:
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;
}


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 8 user(s) online. 0 member(s) and 8 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd