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

Access violation when trying to loadsettings

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

Molotov member
Posts
49
Threads
21
Location
Behind Keyboard

Access violation when trying to loadsettings

So what im trying to do is a way to load settings from a txt doc with the syntax

Language=en
LogActions=1
ConfirmExit=1
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.:)

"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

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

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:

uses reg1 reg2 reg3;

second problem is

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 :)

4th problem:

  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

[section]
setting1=text
setting2=text
Colin admin
Posts
928
Threads
49
Location
Bulgaria

here is a sample of using ini file

settings.ini:

[settings]
name=Admin
gender=male

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.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

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.

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