Source file: /~heha/hs/random.zip/src/random.cpp

#include <windows.h>
#include <windowsx.h>
#include <shlwapi.h>
#include <shlobj.h>
#define elemof(x) (sizeof(x)/sizeof(*(x)))
#define T(x) TEXT(x)
#define nobreak

HINSTANCE hInst;

/*****************************
 * Verzeichnisauswahl-Dialog *
 *****************************/

int CALLBACK BrowseCallbackProc(HWND Wnd,UINT Msg,LPARAM,LPARAM) {
 switch (Msg) {
  case BFFM_INITIALIZED: {
   WCHAR CurDir[MAX_PATH];
   GetCurrentDirectoryW(elemof(CurDir),CurDir);
   SendMessage(Wnd,BFFM_SETSELECTION,TRUE,(LPARAM)CurDir);
  }break;
 }
 return 0;
}

bool ChooseDirectory(HWND Parent, LPTSTR DirName) {
 TCHAR s[256];
 LoadString(hInst,14,s,elemof(s));
 BROWSEINFO bi={Parent,NULL,NULL,s,
   BIF_NEWDIALOGSTYLE|BIF_RETURNONLYFSDIRS,
   BrowseCallbackProc,0,0};
 LPITEMIDLIST pidl;
 CoInitialize(NULL);
 pidl=SHBrowseForFolder(&bi);
 if (!pidl) return false;
 SHGetPathFromIDList(pidl,DirName);
 CoTaskMemFree(pidl);
 return true;
}

/*******************************
 * Editfenster mit Hoch/Runter *
 *******************************/
WNDPROC DefEditProc;
INT_PTR CALLBACK EditSubclassProc(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
 int Delta=0;
 switch (Msg) {
  case WM_VSCROLL: switch (LOWORD(wParam)) {
   case SB_LINEUP:   Delta=  1; break;
   case SB_LINEDOWN: Delta= -1; break;
   case SB_PAGEUP:   Delta= 10; break;
   case SB_PAGEDOWN: Delta=-10; break;
  }break;
  case WM_KEYDOWN: switch (wParam) {
   case VK_UP:    Delta=  1; break;
   case VK_DOWN:  Delta= -1; break;
   case VK_PRIOR: Delta= 10; break;
   case VK_NEXT:  Delta=-10; break;
  }break;
 }
 if (Delta) {
  TCHAR s[32];
  int n,nn;
  GetWindowText(Wnd,s,elemof(s));
  n=StrToInt(s);
  nn=n+Delta;
  if (nn<1) nn=1;
  if (nn>100000) nn=100000;
  if (nn==n) MessageBeep(MB_ICONHAND);
  else{
   wnsprintf(s,elemof(s),T("%d"),nn);
   SetWindowText(Wnd,s);
  }
 }
 return CallWindowProc(DefEditProc,Wnd,Msg,wParam,lParam);
}

/*******************************************
 * Größenangabe mit Einheitenpräfix parsen *
 *******************************************/
DWORD ParseSize(LPCTSTR s) {
 DWORD l=StrToInt(s);
 DWORD d=0;
 LPCTSTR k=StrChr(s,',');
 if (!k) k=StrChr(s,'.');
 if (k) d=k[1]-'0';
 s=StrChr(s,' ');
 if (!s) return l;
 TCHAR c=s[1];
 if (s[2]=='i') c&=~' '; else c|=' ';
 switch (s[1]) {
  case 'k': l*=1000; l+=d*100; break;
  case 'K': l*=1024; l+=d*102; break;
  case 'm': l*=1000*1000; l+=d*100000; break;
  case 'M': l*=1024*1024; l+=d*104858; break;
  case 'g': l*=1000*1000*1000; l+=d*100000000; break;
  case 'G': l*=1024*1024*1024; l+=d*107374182; break;
 }
 return l;
}

void WriteRandomFiles(DWORD len, UINT cnt, LPCTSTR pref, LPCTSTR path) {
 srand(GetTickCount());
 for (UINT i=0; i<cnt; i++) {
  TCHAR name[MAX_PATH];
  GetTempFileName(path,pref,0,name);
  HMMIO h=mmioOpen(name,NULL,MMIO_ALLOCBUF|MMIO_CREATE|MMIO_DENYNONE|MMIO_WRITE);
  static char buf[4096];
  for (DWORD j=0; j<len;) {
   int ll=len-j;
   if (ll>sizeof(buf)) ll=sizeof(buf);
   for (int k=0; k<ll; k++) buf[k]=rand();
   mmioWrite(h,buf,ll);
   j+=ll;
  }
  mmioClose(h,0);
 }
}

INT_PTR CALLBACK MainDlgProc(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
 switch (Msg) {
  case WM_INITDIALOG: {
   DefEditProc=SubclassWindow(GetDlgItem(Wnd,11),EditSubclassProc);
   TCHAR s[MAX_PATH];
   GetCurrentDirectory(elemof(s),s);
   SetDlgItemText(Wnd,13,s);
  }return TRUE;
  
  case WM_COMMAND: switch (LOWORD(wParam)) {
   case IDOK:{
    TCHAR s[32];
    HWND w=GetDlgItem(Wnd,10);
    GetWindowText(w,s,elemof(s));
    DWORD len=ParseSize(s);
    if (!len) {
     SetFocus(w);
     Edit_SetSel(w,0,-1);
     break;
    }
    UINT cnt=GetDlgItemInt(Wnd,11,NULL,FALSE);
    if (!cnt) {
     w=GetDlgItem(Wnd,11);
     SetFocus(w);
     Edit_SetSel(w,0,-1);
     break;
    }
    TCHAR pref[4];
    GetDlgItemText(Wnd,12,pref,elemof(pref));
    TCHAR path[MAX_PATH];
    GetDlgItemText(Wnd,13,path,elemof(path));
    HCURSOR curs=SetCursor(LoadCursor(0,IDC_WAIT));
    WriteRandomFiles(len,cnt,pref,path);
    SetCursor(curs);
   }nobreak;
   case IDCANCEL: EndDialog(Wnd,wParam); break;
   case 14: {	// Verzeichnis-Auswahl „…“
    TCHAR s[MAX_PATH];
    GetDlgItemText(Wnd,13,s,elemof(s));
    if (ChooseDirectory(Wnd,s)) {
     SetDlgItemText(Wnd,13,s);
    }
   }break;
  }
 }
 return FALSE;
}

void CALLBACK WinMainCRTStartup() {
 hInst=GetModuleHandle(NULL);
 ExitProcess((UINT)DialogBox(hInst,MAKEINTRESOURCE(100),0,MainDlgProc));
}
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded