sobota, 29 czerwca 2019

info file (class C++ Builder)

class info_my_FILE
{
private:
VS_FIXEDFILEINFO *type_File;
UnicodeString name;
void *temp_info;
unsigned long temp_Long;
char *buff_File;

int size_Buff;
UnicodeString result_File;


public:

void read_File(UnicodeString name_File)
{

name=name_File;
unsigned int temp_value_Size;
size_Buff=GetFileVersionInfoSize(name.w_str(),&temp_Long);
buff_File=new char[size_Buff];
GetFileVersionInfo(name.w_str(),0,size_Buff,buff_File);
if(VerQueryValue(buff_File,L"\\",&temp_info,&temp_value_Size))
{
type_File=(VS_FIXEDFILEINFO*)temp_info;
if(type_File->dwFileType==VFT_DLL)
result_File="library DLL";
else if(type_File->dwFileType==VFT_APP)
result_File="application .exe";
else if(type_File->dwFileType==VFT_DRV)
result_File="DRV Type";
else if(type_File->dwFileType==VFT_STATIC_LIB)
result_File="static Library";
else if(type_File->dwFileType==VFT_FONT)
result_File="font type";
else if(type_File->dwFileType==VFT_VXD)
result_File="virtual";
else
  result_File="unknown type";
}


}

int write_size_Buff()
{
return size_Buff;
}
UnicodeString writeType()
{
return result_File;
}
~info_my_FILE()
{
delete buff_File;
}
};


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 UnicodeString files[3];
 files[0]=Application->ExeName;
 files[1]="C:\\BOOTSECT.BAK";
 files[2]="C:\\Windows\\PFRO.log";
 info_my_FILE *my_Files=new info_my_FILE[3];
 int i;
 for(i=0;i<3;i++)
 {
my_Files[i].read_File(files[i]);
 }
 for(i=0;i<3;i++)
 {
ListBox1->Items->Add(my_Files[i].writeType()+" - "+IntToStr(my_Files[i].write_size_Buff()));
 }


 delete my_Files;
}

wtorek, 25 czerwca 2019

getpwuid() and getgrgid(), (class C++ Linux)

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
using namespace std;

class UID_GID
{
private:
uid_t uid_value;
gid_t gid_value;
struct group* group_buff;
struct passwd* passwd_buff;
char* user_name;
char* group_name;
public:

UID_GID()
{
uid_value=geteuid();
gid_value=getegid();
passwd_buff=getpwuid(uid_value);
group_buff=getgrgid(gid_value);
user_name=strdup(passwd_buff->pw_name);
group_name=strdup(group_buff->gr_name);


}
char *write_User()
{
return user_name;
}
char *write_Group()
{
return group_name;
}

};

int main(int argc, char **argv)
{
UID_GID User_Group;
cout<<"User : "<<User_Group.write_User()<<endl;
cout<<"Group: "<<User_Group.write_Group()<<endl;
return 0;
}

niedziela, 23 czerwca 2019

Pipe and fork() - example gcc/Linux

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define size 1000
#define B 2048
int m_First[size];
double sqrt_First[size];
pid_t example_pid;
int temp[2];

int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
 ++sum;
if(sum==2)
 return 1;
else
 return 0;
}
void init_Arrays()
{
int i,j;
i=0;
j=2;
do
{
if(is_First(j))
{
m_First[i]=j;
sqrt_First[i]=sqrt(1.0*j);
++i;
}
++j;
}while(i<size);
}
void f_P(char *buff)
{
fputs(buff,stdout);
}
void read_File(FILE *str_buffer)
{
char buff[B];
while(!feof(str_buffer) && !ferror(str_buffer) && fgets(buff,sizeof(buff),str_buffer)!=NULL)
f_P(buff);
}
void write_Dates(int count, FILE *str1)
{
int i;
for(i=0;i<count;i++)
{
fprintf(str1,"%d  %f\n",m_First[i],sqrt_First[i]);
fflush(str1); 
}
}
int main(int argc, char **argv)
{
FILE *temp_stream;
int rand_value;
pipe(temp);
example_pid=fork();
srand(time(NULL));
init_Arrays();
rand_value=1+rand()%size;

if(example_pid==(pid_t) 0)
{
close(temp[1]);
temp_stream=fdopen(temp[0],"r");
read_File(temp_stream);
close(temp[0]);
}
else
{
   close(temp[0]);
   temp_stream=fdopen(temp[1],"w");
   write_Dates(rand_value,temp_stream);
   close(temp[1]);
}
return 0;
}

sobota, 15 czerwca 2019

Simulation of lottery draws (C-gcc)

I want to check 1000 number draws (5 different numbers [42 max value]) and make a camparison to the pattern. 


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 42
#define BETS 1000
#define VAL_BET 5


void sort_Bet(int *tab, int size)
{
int i,j,k;
for(i=1;i<size;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
int *tab_Bet;
int **tab_Game;

void init_Arrays()
{
 int i,j,k;
 int temp_tab_Bet[MAX];
 int temp_tab_Game[BETS][MAX];
 tab_Bet=(int*)malloc(VAL_BET*sizeof(int));
 tab_Game=(int**)malloc(BETS*sizeof(int*));
 for(i=0;i<BETS;i++)
  tab_Game[i]=(int*)malloc(VAL_BET*sizeof(int*)); 
 for(i=0;i<MAX;i++)
  temp_tab_Bet[i]=0;
 for(i=0;i<BETS;i++)
  for(j=0;j<MAX;j++)
  temp_tab_Game[i][j]=0;
 i=0;
 do
 {
  do
  {
k=rand()%MAX;
  }while(temp_tab_Bet[k]!=0);
  temp_tab_Bet[k]=1;
  tab_Bet[i]=k+1;
  i++;
  } while(i<VAL_BET);
  sort_Bet(tab_Bet,VAL_BET);
  i=0;
  do
  {
   j=0;  
   do
   {  
   do
   {
  k=rand()%MAX;
   }while(temp_tab_Game[i][k]!=0);
   temp_tab_Game[i][k]=1;
   tab_Game[i][j]=k+1;
   ++j;
   }while(j<VAL_BET);    
    ++i;  
  }while(i<BETS);
  for(i=0;i<BETS;i++)
   sort_Bet(tab_Game[i],VAL_BET);
}
void destroy_Arrays()
{
int i;
for(i=0;i<BETS;i++)
free(tab_Game[i]);
free(tab_Game);
free(tab_Bet);
}
int is_Five(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==5)
return 1;
else
return 0;

}
int is_Four(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==4)
return 1;
else
return 0;

}
int is_Three(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==3)
return 1;
else
return 0;

}
int is_Two(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==2)
return 1;
else
return 0;

}
int is_One(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==1)
return 1;
else
return 0;

}
int is_Zero(int *tab1, int *tab2, int size)
{
int i,j,sum;
sum=0;
i=0;
do
{
for(j=0;j<size;j++)
if(tab1[j]==tab2[i])
 ++sum;
++i;
}while(i<size);
if(sum==0)
return 1;
else
return 0;

}


int main(int argc, char **argv)
{
int i;
int win_0,win_1,win_2,win_3,win_4,win_5;
win_0=win_1=win_2=win_3=win_4=win_5=0;
srand(time(NULL));
init_Arrays();
for(i=0;i<BETS;i++)
{
if(is_Zero(tab_Game[i],tab_Bet,VAL_BET))
++win_0;
if(is_One(tab_Game[i],tab_Bet,VAL_BET))
++win_1;
if(is_Two(tab_Game[i],tab_Bet,VAL_BET))
++win_2;
if(is_Three(tab_Game[i],tab_Bet,VAL_BET))
++win_3;
if(is_Four(tab_Game[i],tab_Bet,VAL_BET))
++win_4;
if(is_Five(tab_Game[i],tab_Bet,VAL_BET))
++win_5;
}

printf("Your BET: (5 [42])");
printf("\n");
for(i=0;i<VAL_BET;i++)
printf("%d  ",tab_Bet[i]);
printf("\n----------------------------------------------\n");
printf("For 1000 draws: \n\n");
printf(" 5 - %d\n",win_5);
printf(" 4 - %d\n",win_4);
printf(" 3 - %d\n",win_3);
printf(" 2 - %d\n",win_2);
printf(" 1 - %d\n",win_1);
printf(" 0 - %d\n",win_0);


destroy_Arrays();

return 0;
}






piątek, 7 czerwca 2019

Bit flags (class C++ Builder)

class BYTE_MASK
{
private:
UnicodeString result;
int byt_value;
public:
BYTE_MASK()
{
        result="";
    }
void Read(char *base, int size, int value)
{


int i;
for(i=size-1;i>=0;i--)
{
char temp=char(value);
byt_value=(base[i] & temp);
if(byt_value!=0)
result+="1";
else
result+="0";
}
}
UnicodeString Write()
{
return result;
}


};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int i;
  char temp_values[10]={1,2,4,8,16,32,64,128,256,512};
  int date[10]={20,111,290,330,440};
  BYTE_MASK *a_Mask=new BYTE_MASK[5];
  for(i=0;i<5;i++)
  {
   a_Mask[i].Read(temp_values,10,date[i]);
  }
  Label1->Caption=a_Mask[0].Write();
  Label2->Caption=a_Mask[1].Write();
  Label3->Caption=a_Mask[2].Write();
  Label4->Caption=a_Mask[3].Write();
  Label5->Caption=a_Mask[4].Write();

}

środa, 5 czerwca 2019

Data from a CPU to a file (class C++ - Linux)

#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;


class file_CPU_INFO
{
private:
mode_t mode_value;
char *file_name;
float cpu_Values(char *str_Argument)
{
char *directory="/proc/cpuinfo";
char buf[512],end='\0';
FILE *m_file;
char *find;
size_t byt;
float result;
m_file=fopen(directory,"r");
byt=fread(buf,1,sizeof(buf),m_file);
fclose(m_file);
buf[byt]=end;
find=strstr(buf,str_Argument);
if(find==NULL)
 return 0;
char *temp;
strcpy(temp,str_Argument);
strcat(temp," : %f");
sscanf(find,temp,&result);
return result;
 
 
}
public:
file_CPU_INFO(char *file)
{
int f1;
mode_value=S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
file_name=file; 
f1=open(file_name, O_WRONLY | O_EXCL | O_CREAT,mode_value);
close(f1);
 
 
 
}
void Write(char *cpu_Argument)
{
float cpu_Value;
cpu_Value=cpu_Values(cpu_Argument);
fstream file(file_name,ios::out);
file<<cpu_Value;
file.flush();
file.close();
 
 
 
}
};
int main(int argc, char **argv)
{
int i;
string cpu_values[5]={"bogomips","cache_alignment","model","stepping","processor"};

file_CPU_INFO my_CPU("cpu_file");
for(i=0;i<5;i++)
{
my_CPU.Write(strdup(cpu_values[i].c_str()));
}

return 0;
}

wtorek, 4 czerwca 2019

Information about the project (class C++ Builder)

class info_Project
{
 private:
ofstream new_file;
UnicodeString name_File;
TStrings text[4];
UnicodeString name_Project;
 public:
info_Project(UnicodeString name)
{
name_File=name;
name_Project=Application->ExeName;
text[0].Text=name_Project;
text[1].Text=ExtractFileDir(name_Project);
text[2].Text=ExtractFileExt(name_Project);
text[3].Text=ExtractFileDrive(name_Project);

}

void save_Info()
{
int i;
new_file.open(name_File.w_str());
for(i=0;i<4;i++)
 new_file<<(*text)[i].w_str()<<"\n";
new_file.close();

}


};
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 info_Project INFO("date_project.txt");
 INFO.save_Info();
}

niedziela, 2 czerwca 2019

TFileStream and read a text file (class C++ Builder)


class read_text_File : public TFileStream
{
private:
unsigned long length_file;
UnicodeString filename;
int size_bytes;
BY_HANDLE_FILE_INFORMATION info;
TFileStream *file;
wchar_t *buf;
public:
read_text_File(const System::UnicodeString name, TListBox *lBox,
unsigned short temp_value):
TFileStream(name,temp_value)
{
buf=new wchar_t[1024];
filename=name;
file=new TFileStream(filename,fmOpenRead);
file->Read(buf,1024);
lBox->SetTextBuf(buf);
if(GetFileInformationByHandle((HANDLE)Handle,&info))
{
length_file=info.nFileSizeLow;
}
delete buf;
file->Free();


}
unsigned int Size()
{
return length_file;
}

};
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 UnicodeString my_File="avaya.txt";
 read_text_File *OPEN=new read_text_File(my_File,ListBox1,1);
 Label1->Caption=OPEN->Size();
}