--attrib2.exw--
--start code--

include win32lib.ew
without warning

sequence file_name
file_name="attrib2.exw"
atom junk, file_id_ptr, file_handle

constant
str1 = "file size = ",
str2 = "current attributes = ",
str3 = "long filename  = ",
str4 = "short filename = ",
str5 = "filedate (m/d/y ) = ",
str6 = "filetime (last modified) = ",

zOPEN_EXISTING = 3,
zGENERIC_WRITE =  #40000000,
zMAX_PATH = 260,
zFILE_ATTRIBUTE_READONLY = 1,
zFILE_ATTRIBUTE_HIDDEN = 2,
zFILE_ATTRIBUTE_ARCHIVE = 32,
zFILE_ATTRIBUTE_NORMAL = 128,

Win     = create(Window,"File Attributes 2",0,0,0,285,365,0),
Label0  = create(LText,"filename:",Win,10,12,60,20,0),
Label1  = create(LText,"",Win,10,40,160,20,0),
Label2  = create(LText,"",Win,10,65,160,20,0),
Label3  = create(LText,"",Win,10,200,260,20,0),
Label4  = create(LText,"",Win,10,225,260,20,0),
Label5  = create(LText,"",Win,10,250,260,20,0),
Label6  = create(LText,"",Win,10,275,260,20,0),
Check1  = create(CheckBox," Read-Only (1)",Win,10,90,170,20,0),
Check2  = create(CheckBox," Hidden    (2)",Win,10,115,170,20,0),
Check3  = create(CheckBox," Archive   (32)",Win,10,140,170,20,0),
Button1 = create(PushButton,"Change Attributes",Win,10,165,130,25,0),
Button2 = create(PushButton,"Update FileTime",Win,10,300,130,25,0),
Entry   = create(EditText,"",Win,75,10,190,20,0),
--**

zGetFileAttributes=registerw32Function( kernel32, "GetFileAttributesA", {C_POINTER}, C_LONG ),
zSetFileAttributes=registerw32Function( kernel32, "SetFileAttributesA", {C_POINTER, C_LONG}, C_LONG ),
zFindFirstFile = registerw32Function( kernel32, "FindFirstFileA", {C_POINTER, C_POINTER}, C_LONG ),
zFileTimeToLocalFileTime = registerw32Function( kernel32, "FileTimeToLocalFileTime", {C_POINTER, C_POINTER}, C_LONG ),
zFileTimeToSystemTime = registerw32Function( kernel32, "FileTimeToSystemTime", {C_POINTER, C_POINTER}, C_LONG ),
zGetSystemTimeAsFileTime = registerw32Procedure( kernel32, "GetSystemTimeAsFileTime", {C_POINTER} ),
zSetFileTime = registerw32Function( kernel32, "SetFileTime", {C_LONG, C_POINTER, C_POINTER, C_POINTER}, C_LONG ),
zCreateFile = registerw32Function( kernel32, "CreateFileA", {C_POINTER, C_LONG, C_LONG, C_LONG, C_LONG, C_LONG, C_LONG}, C_LONG ),
zCloseHandle = registerw32Function( kernel32, "CloseHandle", {C_LONG}, C_LONG ),


-- LastWriteTemp structure
 size_of_LastWriteTemp = 8,
 Local=acquire_mem(0,size_of_LastWriteTemp),
 LowLocalTime = Local + 0,
 HighLocalTime = Local + 4,

-- WIN32_FIND_DATA structure
 size_of_WIN32_FIND_DATA = 318,
 file_data=acquire_mem(0,size_of_WIN32_FIND_DATA),
 dwFileAttributes  = file_data + 0,
 ftCreationTime    = file_data + 4,
 ftLastAccessTime  = file_data + 12,
 ftLastWriteTime   = file_data + 20,
 nFileSizeHigh     = file_data + 28,
 nFileSizeLow      = file_data + 32,
 dwReserved0       = file_data + 36,
 dwReserved1       = file_data + 40,
 cFileName         = file_data + 44,
 cAlternateFileName = cFileName + zMAX_PATH, 

-- SystemTime structure
 size_of_SystemTime = 16,
 Time=acquire_mem(0,size_of_SystemTime),
 wYear = Time + 0,
 wMonth = Time + 2,
 wDayOfWeek = Time + 4,
 wDay = Time + 6,
 wHour= Time + 8,
 wMinute = Time + 10,
 wSecond = Time + 12,
 wMilliseconds = Time + 14

for i=Check1 to Check3 do
setFont( i,"Courier New",10,1)
end for

for i=Win to Check3 do
setWindowBackColor(i,rgb(255,255,0))
end for

procedure show_file_time()
sequence t_string
junk=w32Func(zFileTimeToLocalFileTime,{ftLastWriteTime,Local})
junk=w32Func(zFileTimeToSystemTime,{Local,Time})
t_string=sprintf("%d",peek(wMonth)) & "/"
       & sprintf("%d",peek(wDay)) & "/"
       & sprintf("%d",peek(wYear)+(peek(wYear+1)*256))
setText(Label5,str5 & sprintf("%s",{t_string}))
t_string=sprintf("%2d",peek(wHour)) & ":"
       & sprintf("%02d",peek(wMinute))
setText(Label6,str6 & sprintf("%s",{t_string}))
end procedure

procedure set_file_time(atom self, atom event, sequence params)
file_handle=w32Func(zCreateFile,
            {file_id_ptr,zGENERIC_WRITE,0,0,zOPEN_EXISTING,0,0})
w32Proc(zGetSystemTimeAsFileTime,{Local})
junk=w32Func(zSetFileTime,{file_handle,NULL,NULL,Local})
junk=w32Func(zCloseHandle,{file_handle})
junk=w32Func(zFindFirstFile,{file_id_ptr,file_data})
show_file_time()
end procedure

procedure do_it(atom self, atom event, sequence params)
setText(Entry,file_name)
file_id_ptr=acquire_mem(0,file_name)
file_handle=w32Func(zCreateFile,
            {file_id_ptr,0,0,0,zOPEN_EXISTING,0,0})
if file_handle != -1 then
junk=w32Func(zFindFirstFile,{file_id_ptr,file_data})
setText(Label1,str1 & sprintf("%d",peek4u(nFileSizeLow)))
junk=peek4u(dwFileAttributes)
setText(Label2,str2 & sprintf("%d",junk))
if junk < zFILE_ATTRIBUTE_NORMAL then
   if junk >= zFILE_ATTRIBUTE_ARCHIVE then
     setCheck(Check3,1) junk=junk-zFILE_ATTRIBUTE_ARCHIVE
   end if
   if junk >= zFILE_ATTRIBUTE_HIDDEN then
     setCheck(Check2,1) junk=junk-zFILE_ATTRIBUTE_HIDDEN
   end if
   if junk = zFILE_ATTRIBUTE_READONLY then
     setCheck(Check1,1)
   end if
end if
setText(Label3,str3 & sprintf("%s",{peek_string(cFileName)}))
setText(Label4,str4 & sprintf("%s",{peek_string(cAlternateFileName)})) 
show_file_time()
else
setText(Label1,str1) setText(Label2,str2) setText(Label3,str3)  
setText(Label4,str4) setText(Label5,str5) setText(Label6,str6) 
  junk = message_box( "NO SUCH FILE",
  "ERROR !",#2000)
end if
junk=w32Func(zCloseHandle,{file_handle})
end procedure

procedure set_attribs(atom self, atom event, sequence params)
atom attribs
attribs=0
if isChecked(Check1) then attribs=attribs+1 end if
if isChecked(Check2) then attribs=attribs+2 end if
if isChecked(Check3) then attribs=attribs+32 end if
junk=w32Func(zSetFileAttributes,{file_id_ptr,attribs})
junk=w32Func(zGetFileAttributes,{file_id_ptr})
setText(Label2,str2 & sprintf("%d",junk))
end procedure

--remove leading spaces
function trim_left(sequence x)
  for i=1 to length(x) do
    if x[i] != ' ' then return x[i..length(x)] end if
  end for
  return {}
end function

procedure new_name()
setCheck(Check1,0)  setCheck(Check2,0)  setCheck(Check3,0)
file_name=getText(Entry)
file_name=trim_left(file_name)
do_it(1,2,"")	-- arbitrary params
end procedure

procedure onkey_entry(atom self, atom event, sequence params)	--(integer key, integer shift)
  atom key = params[1]
  if key=13 then new_name() end if
end procedure

setHandler( Win, w32HOpen, routine_id("do_it") )
setHandler( Button1, w32HClick, routine_id("set_attribs") )
setHandler( Button2, w32HClick, routine_id("set_file_time") )
setHandler( Entry, w32HKeyDown, routine_id("onkey_entry") )


WinMain( Win, Normal )
--end code--


