Register | Login
Forum Index > Source Help > Access violation when trying to loadsettings
Author Message
Pages: 1
Molotov
Member
(send private message)

Posts: 49
Topics: 21

Location:
Behind Keyboard
[819] Access violation when trying to loadsettings - posted: 2011-11-24 07:36:17
So what im trying to do is a way to load settings from a txt doc with the syntax


Code:
Language=en
LogActions=1
ConfirmExit=1



Code:
inline procedure LoadSettings() {
  uses ebx;
  uses edi;
  uses ecx;
  uses eax;
  // Open file
  ebx = loadfs('Settings.dat');
  
  // Process data
  
  // find \n
  pointer myBuf1,myBuf2;
  myBuf1 = new 30;
  myBuf2 = new 20;
  
  ecx = strOffset(ebx,'\n');
  while(ecx != 0) {
    
    // extract row
    strCopyLen(eax,ebx,ecx);
  
    // Find =
    edi = strIndexOf(eax,'=');
    
    // Extract before and after =
    strCopyLen(myBuf1,eax,edi);

    add eax, edi;
    edi = strLen(eax);
    strCopyLen(myBuf2,eax,edi);
    
    edi = strCmp(myBuf1,'ConfirmExit');
    if(edi) {
      strCopyLen(@zAppConfirmExit,myBuf2,strLen(myBuf2));
    } else {
      edi = strCmp(myBuf1,'Language');
      if(edi) {
        strCopyLen(@zAppLanguage,myBuf2,strLen(myBuf2));
      } else {
        edi = strCmp(myBuf1,'LogActions');
        if(edi) {
          strCopyLen(@zAppLogActions,myBuf2,strLen(myBuf2));
        }
      }
    }
    
    eax = xor;
    ecx++;
    add ebx, ecx;
    ecx = strIndexOf(ebx,'\n');
  }
  delete myBuf1;
  delete myBuf2;
}


It compiles ok but when i try to run it i get Access Violation error at address: 4212015

all help i can get is greatly appriciated.smile

"Lerning new things is what life is all about, if you are always doing the same old things day in and day out you might aswell just kill yourself." - ME d^_^b
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[820] - posted: 2011-11-24 10:32:02
first problem i see is that because it is an inline function the "uses" may cause you problems if you call it in a global scope.. and also that uses should be at only the top of a function....

remember that "inline functions" are macros, anything written inside is output where they are called, since such things as LoadSettings() are a 1 time only call i would suggest to make it an actual function by removing "inline", any reason why you are restoring eax aswell?

also btw you can do:

Code:
uses reg1 reg2 reg3;


second problem is

Code:
ebx = loadfs('Settings.dat');


loadfs only loads a file on assembly time, meaning that your settings file would be static, you would need to use fileread to read it dynamically.

3rd problem is alot of usage of EAX but it would point no where... you are trying to write into strange areas of memory smile

4th problem:

Code:
  ecx = strOffset(ebx,'\n'); //ECX does not equal length but points to the offset
  while(ecx != 0) {
    
    // extract row
    strCopyLen(@lineBuf, ebx, ecx); //this would be very bad


Last but not least, why don't you just use the INI include file and make your settings file an ini? it is almost same format, just needs a section

Code:
[section]
setting1=text
setting2=text


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
[821] - posted: 2011-11-24 10:44:13
here is a sample of using ini file

settings.ini:
Code:
[settings]
name=Admin
gender=male


code:
Code:
#include 'inifiles.zir';

char buf[1024];
Ini_ReadString('settings.ini', @buf, 'settings', 'name', 'default', 1024);

//buf now holds your string

print('buf = ', @buf, '\r\n');


a quick explanation of Ini_ReadString.

Parameters:
1 - Location/Filename of your settings file
2 - A pointer to a buffer to hold the string that is read.
3 - The section inside the INI file to search.
4 - The identifier of the setting to get from the section.
5 - The default string to return if ident(4) can not be found.
6 - The maximum buffer size (the size of memory allocated for reading your setting)

Hope this helps, let me know.

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
[822] - posted: 2011-11-24 11:33:16
I've just put together a new function for you that does not rely on ini files, plus a test app, I've not test it much but you should be able to customize it to your needs.

Code:
program WIN32CUI 'Test';

#include 'ch.zir';

////////////////////

//you need to dynamically load your settings file (this can be done inside the LoadSettings Function
const mySettings = loadfs('Settings.dat');


function LoadSettings() {
      uses edi esi ecx;
      
      char setBuf[64], valBuf[64];

      // Open file (temporary)
      esi = @mySettings;
      ecx = strLen(esi);
      
      ecx += esi;
  
      while (esi < ecx) {      
            //remove white spaces
            while (char[esi] == " ") {
                  esi ++;
            }
      
            //remove empty lines
            while (word[esi] == "\r\n") {
                  esi += 2;
            }
            
            //destination
            edi = @setBuf;            
            
            lodsb //load byte into al from esi      
            while (al <> "=") {
                  if (al == 0) {
                        break;
                  }
                  char[edi] = al;
                  edi ++;
                  
                  lodsb
            }
            char[edi] = 0;
            
            if (al == 0) {
                  //unexpected end of file
                  print('Unexpected EOF line: ', @setBuf, '\r\n');
                  return false;
            }
            
            edi = @valBuf;
                  
            lodsb
            while (al <> "\n") {
                  if (al == 0) {
                        break;
                  }
                  char[edi] = al;
                  edi ++;
                        
                  lodsb
            }
            char[edi] = 0;            
            
            
            strLower(@setBuf); //make lowercase for faster compare.          
            
            //a small macro to allow if-based comparison.
            inline function Compare(s1, s2) {
                  eax = strCmp($s1, $s2);
                  $return eax;
            }
            
            if (Compare(@setBuf, 'confirmexit') == true) {                 
                  print('found confirm exit: ', @setBuf, '=', @valBuf, '\r\n');
            } else {
                  print('Unknown setting: ', @setBuf, '=', @valBuf, '\r\n');
            }
      };
}


LoadSettings();

wait_key(nil);
ExitProcess(0);


i have added some comments here and there to help you understand what exactly is going on, if you need explanation of some of the code, just let me know. smile

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 5 user(s) online. 0 member(s) and 5 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