The MASM Forum

General => The Campus => Topic started by: ragdog on November 04, 2012, 10:25:20 PM

Title: Count Ini entries
Post by: ragdog on November 04, 2012, 10:25:20 PM
Hi

I need for my project a Ini entry counter to tell my source how many entry is in this section

LOCAL hToolName[1024]:DWORD
LOCAL buffer   [256]:BYTE
invoke     GetPrivateProfileString,CTEXT ("test"), 0, 0,addr hToolName, sizeof hToolName, addr hCurrentDir
    xor        ecx,ecx
    mov        ecx, 2
    cdq
    idiv       ecx
    invoke wsprintf ,addr buffer ,CTEXT("%d"),eax
    invoke MessageBox,0,addr buffer,0,MB_OK
ret


my Ini


[Test]
1=string
2=string
3=string
4=string
5=string
6=string
7=string
8=string
9=string
10=string


to 10 entries works fine hav i more as 10  have i not the correct result

have an any idea?
Title: Re: Count Ini entries
Post by: Tedd on November 05, 2012, 12:23:03 AM
The return value is the number of characters copied to your buffer; each string may be a different length, so you can't just divide and hope for the best.

If you only need to know how many entries there are, you can just count null-terminators; the list ends with an extra terminator (giving a zero length string.)

If you need to know how many there are so you can parse them, just parse them normally and stop when there's a zero length string.
Title: Re: Count Ini entries
Post by: ragdog on November 05, 2012, 12:37:39 AM
QuoteIf you only need to know how many entries there are, you can just count null-terminators; the list ends with an extra terminator (giving a zero length string.)

From lpReturnedString ?
Title: Re: Count Ini entries
Post by: Tedd on November 05, 2012, 12:50:40 AM
Yes..

QuoteIf lpKeyName is NULL, the function copies all key names in the specified section to the supplied buffer. ... each string is followed by a null character and the final string is followed by a second null character.
Title: Re: Count Ini entries
Post by: ragdog on November 05, 2012, 01:06:41 AM
Yes this solution works with lpReturnedString  contain now but is not sure (if 10 entries)
1-31 from ini this ini entries is unknow

lpReturnedString  =

;CPU Dump
;Address   Hex dump                                         ASCII
;0012E398  31 00 32 00|33 00 34 00|35 00 36 00|37 00 38 00| ?..?.?.?.?..?.
;0012E3A8  39 00 31 30|00 31 31 00|31 32 00 31|33 00 31 34| ?.?.??.?.??.??
;0012E3B8  00 31 35 00|31 36 00 31|37 00 31 38|00 31 39 00| .??.??.?.??.??.
;0012E3C8  32 30 00 32|31 00 32 32|00 32 33 00|32 34 00 32| .?..?.?.
;0012E3D8  35 00 32 36|00 32 37 00|32 38 00 32|39 00 33 30| ?.?..?.?.?
;0012E3E8  00 33 31 00|00 00 00 00|                         .??.....



LOCAL hToolName[256]:DWORD
LOCAL buffer   [256]:BYTE
invoke     GetPrivateProfileString,CTEXT ("test"),0, 0,addr hToolName, sizeof hToolName, addr hCurrentDir
lea         esi,hToolName
xor ecx,ecx
xor ebx,ebx
mov ebx,2
    .while      dword ptr [esi] != 0
           
                push ecx
               
               .if ecx==10
      add ebx,1
        add esi,1
    .endif
                invoke MessageBox,0,esi,0,MB_OK
                pop ecx


    add esi,ebx
            inc ecx
    .endw


Thanks
Title: Re: Count Ini entries
Post by: Tedd on November 05, 2012, 02:54:26 AM
Well that code does what you intend, though I'd consider it a little hacky - I'd still parse strings instead of relying on all entries to be present and in exact order.

I'm not sure why your hexdump is appearing like that, but the ascii should be..

;CPU Dump
;Address   Hex dump                                         ASCII
;0012E398  31 00 32 00|33 00 34 00|35 00 36 00|37 00 38 00| 1.2.3.4.5.6.7.8.
;0012E3A8  39 00 31 30|00 31 31 00|31 32 00 31|33 00 31 34| 9.10.11.12.13.14
;0012E3B8  00 31 35 00|31 36 00 31|37 00 31 38|00 31 39 00| .15.16.17.18.19.
;0012E3C8  32 30 00 32|31 00 32 32|00 32 33 00|32 34 00 32| 20.21.22.23.24.2
;0012E3D8  35 00 32 36|00 32 37 00|32 38 00 32|39 00 33 30| 5.26.27.28.29.30
;0012E3E8  00 33 31 00|00 00 00 00|                         .31.....

30h = "0", 31h = "1", 32h = "2", ... 39h = "9"
Title: Re: Count Ini entries
Post by: jj2007 on November 05, 2012, 03:59:57 AM
Keep it simple...

include \masm32\include\masm32rt.inc
.data?
buffer db 4096 dup(?)

.code
start: mov edi, offset buffer
fn GetPrivateProfileString, "ProductNames", 0, "x", edi, sizeof buffer, "C:\globdata.ini"
xor ebx, ebx
@@: inc ebx
print edi, 13, 10
xor eax, eax
or ecx, -1
repne scasb
cmp byte ptr [edi+1], 0
jne @B
inkey str$(ebx), " entries found"
exit
end start