niedziela, 29 września 2019

Saving printer data (class C++ Builder)

#include <Printers.hpp>
#include <System.hpp>
#include <Classes.hpp>
#include <fstream.h>
#include <Vcl.h>

class info_Printer
{
private:
ofstream save_File;
TStrings* date_Printer;
UnicodeString file_Name;
DEVMODE struct_Info;
wchar_t *printer_Name;
wchar_t *form_Name;
unsigned short p_Size;
short paper_Width;
short paper_Length;
unsigned short driver_Version;
    public:
info_Printer(UnicodeString name)
{
int temp;
file_Name=name;
date_Printer=new TStringList();
struct_Info=*((DEVMODE*)temp);
wcscpy(printer_Name,struct_Info.dmDeviceName);
date_Printer->Add(printer_Name);
wcscpy(form_Name,struct_Info.dmFormName);
date_Printer->Add(form_Name);
p_Size=struct_Info.dmSize;
date_Printer->Add(IntToStr(p_Size));
paper_Width=struct_Info.dmPaperWidth;
date_Printer->Add(IntToStr(paper_Width));
paper_Length=struct_Info.dmPaperLength;
date_Printer->Add(IntToStr(paper_Length));
driver_Version=struct_Info.dmDriverVersion;
date_Printer->Add(IntToStr(driver_Version));
}
void Save()
{
  int i,temp_Size;
  temp_Size=date_Printer->Count;
  save_File.open(file_Name.w_str());
  for(i=0;i<temp_Size;i++)
   save_File<<(*date_Printer)[i].w_str()<<"\n";
  save_File.close();

}

};



void __fastcall TForm1::Button1Click(TObject *Sender)
{
 UnicodeString test_File;
 test_File=Edit1->Text;
 info_Printer my_Device(test_File);
 my_Device.Save();
}

czwartek, 26 września 2019

Canvas, LineTo - C++ Builder



void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Image1->Width=475;
 Image1->Height=475;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int size_X,size_Y,centr_X,centr_Y;
 int centr_4,centr_8;
 size_X=size_Y=Image1->Width;
 centr_X=centr_Y=size_X/2;
 centr_4=centr_X/2;
 centr_8=centr_4/2;
 Image1->Canvas->MoveTo(0,0);
 Image1->Canvas->LineTo(size_X,size_Y);
 Image1->Canvas->MoveTo(size_X,0);
 Image1->Canvas->LineTo(0,size_Y);
 Image1->Canvas->MoveTo(0,centr_Y);
 Image1->Canvas->LineTo(size_X,centr_Y);
 Image1->Canvas->MoveTo(centr_X,0);
 Image1->Canvas->LineTo(centr_X,size_Y);
 Image1->Canvas->MoveTo(0,centr_Y);
 Image1->Canvas->LineTo(centr_X,0);
 Image1->Canvas->LineTo(size_X,centr_Y);
 Image1->Canvas->LineTo(centr_X,size_Y);
 Image1->Canvas->LineTo(0,centr_Y);
 Image1->Canvas->MoveTo(0,size_Y);
 Image1->Canvas->LineTo(size_X,centr_Y);
 Image1->Canvas->MoveTo(size_X,size_Y);
 Image1->Canvas->LineTo(0,centr_Y);
 Image1->Canvas->MoveTo(size_X,0);
 Image1->Canvas->LineTo(0,centr_Y);
 Image1->Canvas->MoveTo(0,0);
 Image1->Canvas->LineTo(size_X,centr_Y);
 Image1->Canvas->MoveTo(centr_X,size_Y);
 Image1->Canvas->LineTo(0,0);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(centr_X,centr_Y-centr_4);
 Image1->Canvas->LineTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(centr_X,centr_Y+centr_4);
 Image1->Canvas->LineTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(centr_X,0);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(size_X,0);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(size_X,size_Y);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(centr_X,size_Y);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(0,size_Y);
 Image1->Canvas->MoveTo(centr_X+centr_4,centr_Y);
 Image1->Canvas->LineTo(0,0);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(0,0);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(centr_X,0);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(size_X,0);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(size_X,size_Y);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->MoveTo(centr_X-centr_4,centr_Y);
 Image1->Canvas->LineTo(0,size_Y);
 Image1->Canvas->MoveTo(centr_X,centr_Y-centr_4);
 Image1->Canvas->LineTo(0,size_Y);
 Image1->Canvas->MoveTo(centr_X,centr_Y-centr_4);
 Image1->Canvas->LineTo(size_X,size_Y);
 Image1->Canvas->MoveTo(centr_X,centr_Y+centr_4);
 Image1->Canvas->LineTo(0,0);
 Image1->Canvas->MoveTo(centr_X,centr_Y+centr_4);
 Image1->Canvas->LineTo(size_X,0);
 Image1->Canvas->MoveTo(centr_X-centr_8,centr_Y-centr_8);
 Image1->Canvas->LineTo(centr_X+centr_8,centr_Y-centr_8);
 Image1->Canvas->LineTo(centr_X+centr_8,centr_Y+centr_8);
 Image1->Canvas->LineTo(centr_X-centr_8,centr_Y+centr_8);
 Image1->Canvas->LineTo(centr_X-centr_8,centr_Y-centr_8);
 Image1->Canvas->MoveTo(0,centr_4);
 Image1->Canvas->LineTo(centr_4,centr_4);
 Image1->Canvas->LineTo(centr_4,0);
 Image1->Canvas->LineTo(0,centr_4);
 Image1->Canvas->MoveTo(size_X,centr_4);
 Image1->Canvas->LineTo(size_X-centr_4,centr_4);
 Image1->Canvas->LineTo(size_X-centr_4,0);
 Image1->Canvas->LineTo(size_X,centr_4);
 Image1->Canvas->MoveTo(size_X,size_Y-centr_4);
 Image1->Canvas->LineTo(size_X-centr_4,size_Y-centr_4);
 Image1->Canvas->LineTo(size_X-centr_4,size_Y);
 Image1->Canvas->LineTo(size_X,size_Y-centr_4);
 Image1->Canvas->MoveTo(0,size_Y-centr_4);
 Image1->Canvas->LineTo(centr_4,size_Y-centr_4);
 Image1->Canvas->LineTo(centr_4,size_Y);
 Image1->Canvas->LineTo(0,size_Y-centr_4);




}

wtorek, 17 września 2019

strcpy, strcat (class Text)


#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>

using namespace std;

class TEXT
{
private:
char *base;
char temp_base[1024];
char *sum_Base;
char *reverse;
char *min_Text;
char *minus_Left;
char *minus_Right;

int width_T;

public:
TEXT(char *text)
{
base=text;
strcpy(temp_base,text);
width_T=strlen(base);
min_Text=new char[width_T-2];
minus_Left=new char[width_T-1];
minus_Right=new char[width_T-1];
reverse=new char[width_T];
sum_Base=new char[width_T*2];
}
char *write_min_Text()
{
int i;
for(i=1;i<width_T-1;i++)
min_Text[i-1]=base[i];
return min_Text;
}
char *write_minus_Left()
{
int i;
for(i=1;i<width_T;i++)
minus_Left[i-1]=base[i];
return minus_Left;
}
char *write_minus_Right()
{
int i;
for(i=0;i<width_T-1;i++)
  minus_Right[i]=base[i];
return minus_Right;
}
char *plus_Left(char *add)
{
char *result=add;
strcat(result,base);

return result;
}
char *plus_Right(char *add)
{

char *result2=temp_base;

strcat(result2,add);
return result2;

}
char *Reverse()
{

int i,j;
for(i=0,j=width_T-1;i<width_T;i++,j--)
reverse[i]=base[j];
return reverse;
}

char *write_Sum()
{
char *temp1=temp_base;
char *temp2=Reverse();
strcat(temp1,temp2);
return temp1;
}
~TEXT()

{
delete min_Text;
delete minus_Left;
delete minus_Right;
delete reverse;
delete sum_Base;
}
};

 int _tmain(int argc, _TCHAR* argv[])
{
TEXT test("Welcome to New York");
cout<<test.write_min_Text()<<endl;
cout<<test.write_minus_Left()<<endl;
cout<<test.write_minus_Right()<<endl;
cout<<test.plus_Left("Today ")<<endl;
cout<<test.plus_Right(" Yesterday")<<endl;
cout<<test.Reverse()<<endl;
cout<<test.write_Sum()<<endl;

getch();
return 0;
}

poniedziałek, 16 września 2019

Map, munmap, lseek (example gcc)


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#define max 100
#define FILE1 0x100
#define FILE2 0x200
#define FILE3 0x300
#define FILE4 0x400
#define FILE5 0x500
#define FILE6 0x600
int is_P(int a,  int b,  int c)
{
int delta;
delta=(b*b)-(4*a*c);
if(delta>=0)
return 1;
else
return -1;
}
int plus_Minus()
{
int x,y;
x=rand()%2;
if(x==0)
y=-1;
else
y=1;
return y;

}

int main(int argc, char **argv)
{
char file_Name[20];
void *mem1,*mem2,*mem3,*mem4,*mem5,*mem6;
int a,b,c,delta,e,file;
double x1,x2;
srand(time(NULL));
do
{
a=(rand()%max+1)*plus_Minus();
b=(rand()%max+1)*plus_Minus();
c=(rand()%max+1)*plus_Minus();

}while(is_P(a,b,c));
delta=(b*b)-(4*a*c);
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
strcpy(file_Name,"math_values");


file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE1+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem1=mmap(0,FILE1,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem1,"%d\n",a); munmap(mem1,FILE1);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE2+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem2=mmap(0,FILE2,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem2,"%d\n",b); munmap(mem2,FILE2);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE3+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem3=mmap(0,FILE3,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem3,"%d\n",c); munmap(mem3,FILE3);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE4+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem4=mmap(0,FILE4,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem4,"%d\n",delta); munmap(mem4,FILE4);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE5+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem5=mmap(0,FILE5,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem5,"%f\n",x1); munmap(mem5,FILE5);

file=open(file_Name,O_RDWR,S_IRUSR | S_IWUSR);
lseek(file,FILE6+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
mem6=mmap(0,FILE6,PROT_WRITE,MAP_SHARED,file,0);
close(file);
sprintf((char*) mem6,"%f\n",x2); munmap(mem6,FILE6);









return 0;
}

Read a file with fork and exec (g++ Linux)

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

class open_FILE
{
private:

char *n_File;
char run_Cat[4];
pid_t child_P;
public:
open_FILE(char *name)
{


n_File=name;

strcpy(run_Cat,"cat");
}
int child_Value()
{

      char *name_File[]={"cat",n_File,NULL};
 child_P=fork();

if(child_P!=0)
return child_P;
else
execvp(run_Cat,name_File);
}
void read_F()
{
child_Value();
}
};

int main(int argc, char **argv)
{
open_FILE OPEN("/proc/cpuinfo");
OPEN.read_F();
return 0;
}

niedziela, 15 września 2019

Info OS Version (C++ Linux)


#include <iostream>
#include <string>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>


using namespace std;

class os_Info
{
private:
struct utsname os_Version;
struct sysinfo sys_Values;
char  machine[40];
char  sysname[40];
char  version[40];
char  release[40];
double free_Memory;
double total_Memory;
int file;
public:
os_Info()
{
uname(&os_Version);
sysinfo(&sys_Values);
strcpy(machine,os_Version.machine);
strcpy(sysname,os_Version.sysname);
strcpy(version,os_Version.version);
strcpy(release,os_Version.release);
free_Memory=sys_Values.freeram/(1024.*1024.);
total_Memory=sys_Values.totalram/(1024.*1024.);
}
void write_to_File(char *file_Name)
{
size_t l_machine,l_version,l_sysname,l_release;

l_machine=strlen(machine);
l_version=strlen(version);
l_sysname=strlen(sysname);
l_release=strlen(release);
file=open(file_Name,O_WRONLY | O_CREAT | O_APPEND, 0666);
write(file,sysname,l_sysname);
write(file,version,l_version);
write(file,machine,l_machine);
write(file,release,l_release);
write(file,&total_Memory,sizeof(double));
write(file,&free_Memory,sizeof(double));
close(file);
}


};

int main(int argc, char **argv)
{
os_Info OS;
OS.write_to_File("date.txt");


return 0;
}

czwartek, 12 września 2019

Reverse text file (Visual C++)



private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

String^ all_Text="";
String^ my_test_File="C:\\Windows\\setupact.log";
String^ text_File;
StreamReader^ main_File=gcnew StreamReader(my_test_File,System::Text::Encoding::Default);
while(text_File=main_File->ReadLine())
{
all_Text+=text_File;
textBox1->AppendText(text_File+System::Environment::NewLine);
}
main_File->Close();
 
int width_Text=all_Text->Length;
array<System::Char>^ temp_Char1=gcnew array<System::Char>(width_Text);
array<System::Char>^ temp_Char2=gcnew array<System::Char>(width_Text);
int i,j;
for(i=0;i<width_Text;i++)
{
temp_Char1[i]=(Char)all_Text[i];

}
for(i=0,j=width_Text-1;i<width_Text;i++,j--)
{
temp_Char2[i]=temp_Char1[j];
}
String^ reverse_Text=gcnew String(temp_Char2);
textBox2->AppendText(reverse_Text);
 
 


}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
label1->Text="C:\\Windows\\setupact.log";
label2->Text="Reverse text";
}
};
}


piątek, 6 września 2019

Continuation of the previous example (Visual C++)



#pragma endregion
System::Boolean is_First(int value)
{
int i,sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;


}
int *tab1, *tab2,  *tab3;

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
int h_many1,h_many2,h_many3,i,j;
h_many1=h_many2=h_many3=0;
for(i=0;i<1001;i++)
if(is_First(i))
++h_many1;
for(i=1000;i<2001;i++)
if(is_First(i))
++h_many2;
for(i=2000;i<3001;i++)
if(is_First(i))
++h_many3;
label7->Text=h_many1.ToString();
label8->Text=h_many2.ToString();
label9->Text=h_many3.ToString();
tab1=new int[h_many1];
tab2=new int[h_many2];
tab3=new int[h_many3];
i=0;
j=1;
do
{
if(is_First(j))
{
tab1[i]=j;
++i;
}
++j;
}while(i<h_many1);
i=0;
j=1000;
do
{
if(is_First(j))
{
tab2[i]=j;
++i;
}
++j;
}while(i<h_many2);
i=0;
j=2000;
do
{
if(is_First(j))
{
tab3[i]=j;
++i;
}
++j;
}while(i<h_many3);
Graphics^ a1=panel1->CreateGraphics();
Graphics^ a2=panel2->CreateGraphics();
Graphics^ a3=panel3->CreateGraphics();
Pen^ p1=gcnew Pen(System::Drawing::Color::Yellow);
Pen^ p2=gcnew Pen(System::Drawing::Color::Red);
Pen^ p3=gcnew Pen(System::Drawing::Color::Green);

p1->Width=2;
p2->Width=2;
p3->Width=2;
int height=panel1->Height;
             int h1,h2,h3;
h1=h2=h3=height;
for(i=1;i<h_many1;i++)
{
a1->DrawLine(p1,tab1[i-1],h1,tab1[i],h1);
h1-=1;

}
for(i=1;i<h_many2;i++)
{
a2->DrawLine(p2,-1000+tab2[i-1],h2,-1000+tab2[i],h2);
h2-=1;
for(i=1;i<h_many3;i++)
{
a3->DrawLine(p3,tab3[i-1]-2000,h3,tab3[i]-2000,h3);
h3-=1;
}

delete tab1;
delete tab2;
delete tab3;

}

A simple chart (Visual C++)



#pragma endregion
System::Boolean is_First(int value)
{
int i,sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Graphics^ gr=panel1->CreateGraphics();
Pen^ pn=gcnew Pen(System::Drawing::Color::Blue);
int height=panel1->Height;
int *tab;
int how_many=0;
int i,j;
int width=panel1->Width;
pn->Width=2;
int x0=0,y0=0,x1,y1=0;
for(i=0;i<width;i++)
{
                  if(is_First(i))
 {
++how_many;
 }
}
tab=new int[how_many];
i=0;
j=2;
do
{
if(is_First(j))
{
tab[i]=j;
++i;
}
++j;
}while(i<how_many);
for(i=0;i<how_many-1;i++)
{
                   gr->DrawLine(pn,tab[i],height,tab[i+1],height);
  height-=2;
}

delete tab;

label1->Text=panel1->Width.ToString();
label2->Text="0";



}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
};
}