Solved

Delphi - C++ Translation

Posted on 2001-08-07
38
708 Views
Last Modified: 2012-08-13
is there someone that can translate me delphi-syntax?
i started beginning with translation but i have to less knowledge of delphi and/or pascal.
would be happy if anyone could help me!!!

that's the delphi code:


unit ProdCode;

interface

uses
  SysUtils;

(******************************************************************************)

function Week(Date : TDateTime) : integer;
{ Diese Funktion liefert die Nummer der Kalenderwoche zuruck.
(c) 1997 Rainer Reusch & Toolbox
  Date: Datum der zu ermittelnden Woche
  Funktionsergebnis: Kalenderwoche (1..53)
                     Bei einem Fehler 0 }

function YearCode(ADate: TDateTime): Char;
{ Umwandlung des Datum in Jahrescode nach DIN IEC 62}

function PCode(ADate: TDateTime): String;
{ Umwandlung des Datum in Produktionscode im Format XYYZ
      X: Jahresziffer nach DIN IEC 62
     YY: Kalenderwoche
      Z: Wochentag: 1=Montag..7=Sonntag}

(******************************************************************************)
implementation
(******************************************************************************)

function Week(Date : TDateTime) : INTEGER;
var
  Y, M, D,           { Jahr, Monat, Tag }
  FDay : WORD;       { Wochentag des 1. Januar im Jahr }
  JanF : TDateTime;  { 1. Januar des Jahres }
  Days : INTEGER;    { Anzahl Tage seit dem 1. Januar des Jahres }
begin
  try
    DecodeDate(Date, Y, M, D);
    JanF := EncodeDate(Y, 1, 1);
    FDay := DayOfWeek(JanF);
    Days := Trunc(Int(Date)-JanF)+7-DayOfWeek(Date-1);
    Inc(Days, 7 * Ord(FDay IN [2..5]));
    Result := Days DIV 7;
    if Result = 0 then begin
      if (DayOfWeek(EncodeDate(Y-1,  1,  1)) > 5) or
         (DayOfWeek(EncodeDate(Y-1, 12, 31)) < 5) then
        Result := 52
      else
        Result := 53;
    end else if Result = 53 then begin
      if (FDay > 5) or
         (DayOfWeek(EncodeDate(Y, 12, 31)) < 5) then
        Result := 1;
    end;
  except
    Result := 0;
  end;
end;

function YearCode(ADate: TDateTime): Char;
var Year, Month, Day: Word;
    ModYear: Byte;
begin
  DecodeDate(ADate,Year,Month,Day);
  if Year <> 0 then begin
    ModYear:= (Year+10) mod 20;
    case ModYear of
      0: Result:='A';
      1: Result:='B';
      2: Result:='C';
      3: Result:='D';
      4: Result:='E';
      5: Result:='F';
      6: Result:='H';
      7: Result:='J';
      8: Result:='K';
      9: Result:='L';
     10: Result:='M';
     11: Result:='N';
     12: Result:='P';
     13: Result:='R';
     14: Result:='S';
     15: Result:='T';
     16: Result:='U';
     17: Result:='V';
     18: Result:='W';
     19: Result:='X';
    else Result:='z';
    end;
  end else Result:='z';
end;

function PCode(ADate: TDateTime): String;
var
  YStr: Char;
  DStr,
  WStr: String[2];
  W: Byte;
  D: Byte;
begin
  Result:='';

  YStr:=YearCode(ADate);

  W:=Week(ADate);
  WStr:=IntToStr(W);
  while Length(WStr) < 2 do WStr:='0'+WStr;

  D:=DayOfWeek(ADate)-1;
  if D <= 0 then D:=7; {= Sonntag}
  DStr:=IntToStr(D);

  if (YStr <> 'z') and (W > 0)
    then Result:=YStr+WStr+DStr;
end;

(******************************************************************************)

end.
0
Comment
Question by:reggler
  • 15
  • 14
  • 4
  • +4
38 Comments
 

Author Comment

by:reggler
ID: 6358817
i want to use the code in c++builder after translation
0
 
LVL 22

Expert Comment

by:nietod
ID: 6358993
This should be pretty close.  You will probably have to fix a compiler error or two.



int Week(TDateTime Date);
char YearCode(TDateTime ADate);
string PCode(TDateTime ADate);
             
int Week(TDateTime Date)
{
   int Result;
   int Y, M, D,           // Jahr, Monat, Tag
   int FDay;       // Wochentag des 1. Januar im Jahr
   TDateTime JanF;  // 1. Januar des Jahres }
   int Days;     // Anzahl Tage seit dem 1. Januar des Jahres }

   try
   {
      DecodeDate(Date, Y, M, D);
      JanF = EncodeDate(Y, 1, 1);
      FDay = DayOfWeek(JanF);
      Days = Trunc(int(Date)-JanF)+7-DayOfWeek(Date-1);
      // alright this line got me.
      // do you know what "in" does?
      Days += 7 * Ord(FDay IN [2..5]);

      Result = Days / 7;
      if (Result == 0)
      {
         if (DayOfWeek(EncodeDate(Y-1,  1,  1)) > 5) ||
                      (DayOfWeek(EncodeDate(Y-1, 12, 31)) < 5))
             Result = 52
         else
             Result = 53;
       }
       else if (Result = 53)
       {
          if ((FDay > 5) || (DayOfWeek(EncodeDate(Y, 12, 31)) < 5))
             Result = 1;
        }
     }
     catch (...)
     {
        Result = 0;
     }
     return result;
}

char YearCode(TDateTime ADate)
{
   int Year, Month, Day;
   int Result;
   char ModYear;


   DecodeDate(ADate,Year,Month,Day);
   if (Year <> 0)
   {
     ModYear = (Year+10) % 20;
     switch (ModYear)
     {
       0: Result ='A'; break;
       1: Result ='B'; break;
       // you can change the rest the same way.
                   2: Result:='C';
                   3: Result:='D';
                   4: Result:='E';
                   5: Result:='F';
                   6: Result:='H';
                   7: Result:='J';
                   8: Result:='K';
                   9: Result:='L';
                  10: Result:='M';
                  11: Result:='N';
                  12: Result:='P';
                  13: Result:='R';
                  14: Result:='S';
                  15: Result:='T';
                  16: Result:='U';
                  17: Result:='V';
                  18: Result:='W';
                  19: Result:='X';
       default:  
          Result='z';
     }
   }
   else
     Result:='z';
   return Result;
}

string PCode(TDateTime ADate)
{
   char YStr;
   string DStr, WStr;
   char W;
   char D;
   string Result;

   YStr=YearCode(ADate);

   W=Week(ADate);
   WStr=IntToStr(W); // This needs to be written in C++.

   while (Length(WStr) < 2) WStr = "0" + WStr;

   D = DayOfWeek(ADate)-1;
   if (D <= 0) D = 7; {= Sonntag}
   DStr= IntToStr(D);

   if ((YStr != 'z') &&d (W > 0))
      Result = YStr+WStr+DStr;
   return Result;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359006
The line

 Inc(Days, 7 * Ord(FDay IN [2..5]));

becomes

if (2<= FDay && Fday <= 5)
  Days += 7;
0
 
LVL 1

Expert Comment

by:RideOn
ID: 6359011
the compiler doesn't know the function Trunc.
don't know what it does make....
0
 
LVL 1

Expert Comment

by:RideOn
ID: 6359014
and something else nietod, function declaration:

string PCode(TDateTime ADate);
             
string? that should be char.
0
 

Author Comment

by:reggler
ID: 6359026
hi

thanx for your help rideon & nietod.
erm, nietod, rideon is right, the compiler doesn't know Trunc.
0
 

Author Comment

by:reggler
ID: 6359034
hm, the help in c++builder can find the function but the compiler doesn't....
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359093
>> the compiler doesn't know the function Trunc.
Where is it?  I don't see it in the code.

>> string PCode(TDateTime ADate);
>>                          
>> string? that should be char.
No string.  either the STL stirng class called "string" or you coudl use the BCB string class called, I think, AnsiString.
0
 

Author Comment

by:reggler
ID: 6359105
Days = Trunc(int(Date)-JanF)+7-DayOfWeek(Date-1);
0
 
LVL 1

Expert Comment

by:RideOn
ID: 6359107
oh yes, should be AnsiString. Sorry for that mistake reggler....my fault...
0
 

Author Comment

by:reggler
ID: 6359124
n.p.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359130
If I remember Pascal correctly then what you want would be

Days = int((int)(Date) - (double)(JanF)) + 7 - DayOfWeek(Date-1);

That is more parenthesis than are needed, but I think that is clearer.
0
 

Author Comment

by:reggler
ID: 6359155
the function Ord is unknown too
Days += 7 * Ord(FDay IN [2..5]);


i open the question now for other experts...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359208
>> the function Ord is unknown too
>> Days += 7 * Ord(FDay IN [2..5]);
I already posted a fix for that.

if (2<= FDay && Fday <= 5)
   Days += 7;

>> i open the question now for other experts...
Why?  Does the code I posted not work?  let me know. I spent a lot of time writting it.  if you have a problem with it, tell me and I will fix it.
0
 

Author Comment

by:reggler
ID: 6359282
oh yes. sorry for that!
i replaced that false...
sorry ...
i'm still correcting syntax errors...i don't know if it works, can't compile yet.

thought could help if other experts can help too...

anyway, other unknown function:
while (Length(WStr) < 2) WStr = "0" + WStr;

Length is unknown...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359298
That is going to depend on the string class you use.  What string class will you use?

If you use STL string, then

while (WStr.length() < 2) WStr = "0" + WStr;

otherwise

while (WStr.Length() < 2) WStr = "0" + WStr;

Note that if you use Ansistring, you need to change the strings to be declared as AnsiString in the code.
0
 

Author Comment

by:reggler
ID: 6359322
i use AnsiString
i declared all strings as AnsiString.

the last error is in the second last line:
Result = YStr+WStr+DStr;

PCode.cpp(151): E2015 equivocation between '_fastcall System::operator +(int,const System::Variant &)' und '_fastcall System::operator +(int,const System::Currency &)'.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359426
Try

Result = AnsiString(&YStr,1)+WStr+DStr;

or

AnsiString Tmp;
  Tmp = YStr;

Result = Tmp+WStr+DStr;
0
 
LVL 86

Expert Comment

by:jkr
ID: 6359521
If you have access to a Linux machine, try 'p2c':

#include <p2c/p2c.h>


#define PRODCODE_G
#include "prodcode.h"


/******************************************************************************/
long Week(Date)
long Date;
{
  unsigned short Y, M, D;   /* Jahr, Monat, Tag */
  unsigned short FDay;   /* Wochentag des 1. Januar im Jahr */
  long JanF;   /* 1. Januar des Jahres */
  long Days;   /* Anzahl Tage seit dem 1. Januar des Jahres */
  long Result;

  TRY(try1);
    DecodeDate(Date, Y, M, D);
/* p2c: p.pas, line 38: Warning: Symbol 'DECODEDATE' is not defined [221] */
    JanF = EncodeDate(Y, 1, 1);
/* p2c: p.pas, line 39: Warning: Symbol 'ENCODEDATE' is not defined [221] */
    FDay = DayOfWeek(JanF);
/* p2c: p.pas, line 40: Warning: Symbol 'DAYOFWEEK' is not defined [221] */
    Days = (long)((double)(Date - JanF)) - DayOfWeek(Date - 1) + 7;
/* p2c: p.pas, line 41: Warning: Symbol 'DAYOFWEEK' is not defined [221] */
    Days += (FDay < 32 && ((1L << FDay) & 0x3c) != 0) * 7;
    Result = Days / 7;
/* p2c: p.pas, line 43: Warning: Symbol 'RESULT' is not defined [221] */
    if (Result == 0) {
      if ((DayOfWeek(EncodeDate(Y - 1, 1, 1)) > 5) |
       (DayOfWeek(EncodeDate(Y - 1, 12, 31)) < 5)) {
/* p2c: p.pas, line 45: Warning: Symbol 'ENCODEDATE' is not defined [221] */
/* p2c: p.pas, line 45: Warning: Symbol 'DAYOFWEEK' is not defined [221] */
/* p2c: p.pas, line 46: Warning: Symbol 'ENCODEDATE' is not defined [221] */
/* p2c: p.pas, line 46: Warning: Symbol 'DAYOFWEEK' is not defined [221] */
     Result = 52;
      } else {
     Result = 53;
      }
    } else if (Result == 53) {
      if ((FDay > 5) | (DayOfWeek(EncodeDate(Y, 12, 31)) < 5)) {
/* p2c: p.pas, line 52: Warning: Symbol 'ENCODEDATE' is not defined [221] */
/* p2c: p.pas, line 52: Warning: Symbol 'DAYOFWEEK' is not defined [221] */
     Result = 1;
      }
    }
    except();
/* p2c: p.pas, line 56: Warning: Symbol 'EXCEPT' is not defined [221] */
/* p2c: p.pas, line 56: Warning: Expected RECOVER, found 'Result' [227] */
  RECOVER(try1);
    ;
  ENDTRY(try1);
}
/* p2c: p.pas, line 60: Warning: Expected a '.', found FUNCTION [227] */
/* p2c: p.pas, line 60: Warning: Junk at end of input file ignored [277] */



/* End. */
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:reggler
ID: 6359545
okay, can compile it.

but there is an exception error when i try follow:

Label1->Caption = PCode(35065);

the exception is here:
JanF = EncodeDate(Y, 1, 1);


i don't have access to a linux machine at the moment jkr. i'll try later. thx anyway
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359710
should also add, along the lines of jkr's suggestion, that if I remember correctly, there are features in builder for compiling pascal code.

when you test the code what is the year set to when the exception occurs?
0
 
LVL 1

Expert Comment

by:RideOn
ID: 6359731
nothing, the label is blank.


>> should also add, along the lines of jkr's suggestion,
>> that if I remember correctly, there are features
>> in builder for compiling pascal code.

really? let me look for that....
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359870
It can't be blank.  It is an integer.  It has to have a value.  What is the value?  Is it zero?  
0
 

Author Comment

by:reggler
ID: 6359897
after the exception... (i set a breakpoint after my function call)

my function call is:

Label1->Caption = PCode(35065);

Label1->Caption is {Data:NULL}  (That means it's blank on the form)
PCode is : 00401A90
0
 
LVL 22

Expert Comment

by:nietod
ID: 6359929
You said that the line that has the exceptions is

JanF = EncodeDate(Y, 1, 1);

What is y's value?
0
 

Author Comment

by:reggler
ID: 6359970
1242426
0
 
LVL 22

Expert Comment

by:nietod
ID: 6360105
Year must be a 4 digit year, like 2001.  You are passing 124246.   this is not a problem with the code I converted.  This is a problem with the value you passed to it.

now work backarwads and try to see why that value is wrong...
0
 
LVL 3

Accepted Solution

by:
karouri earned 70 total points
ID: 6360415
sorry for this comment but why do you want to translate Delphi to C++ Builder?
The C++Builder has an internal Delphi compiler, so it can use Delphi code. In particular, to use the unit ProdCode in you BCB program, all you need is :
1. To add ProdCode.pas to your project
2. To use the Unit header ProdCode in your forms or C files. This can be done automatically by Include Unit Header in File Menu or manually by adding #include <ProdCode.hpp>

What happens is that the BCB compiler compiles the Pascal unit using dcc (delphi compiler) and makes .obj files and the abovementioned header, which contains the function prototypes, so you can use
function Week(Date : TDateTime) : integer; directly as
int Week(Date: TDateTime);
Note that TDateTime, AnsiString and many other types are direct counterparts of their Delphi equivalents
0
 
LVL 3

Expert Comment

by:karouri
ID: 6360444
In fact, most of the VCL code of the C++ Builder is written in Delphi. And there are many components in the internet written in Delphi to be used for both languages. I usually do write components in Delphi, though I usually write programs in BCB, they work together..
0
 
LVL 22

Expert Comment

by:nietod
ID: 6360657
>> The C++Builder has an internal Delphi
>> compiler, so it can use Delphi code.
I already suggested that.  But I didn't know the details for doing this.  Thanks.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6361635
reggle,
>>i open the question now for other experts...
If you want, you can still unlock this question.
Doing so will increase your chance of obtaining additional input from other experts.

There's no problem with this.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6362320
re:  Ord

'Ord' is for casting a variable to it's ordinal value (an integer).  This function can be completely removed in the C++ code (just substitute the function's argument by itself).
0
 

Author Comment

by:reggler
ID: 6362775
Thanx for all your Comments
erm Karouri, where can i get the *.hpp file?
I'll try your solution as well as nietods.

nietod, to enable answering my question from other experts i open the question again. i please you not to propose direct annother answer. i'll give you the points if your solution works first!
I'll look on the code and see where the error is.
The Pascal-Code worked! (without any exception ;o) ) so, there must be an error from translation....
0
 

Author Comment

by:reggler
ID: 6362786
aaah Karouri
already got it (*.hpp) :o)
0
 

Author Comment

by:reggler
ID: 6362821
Thanx for all your Help.
Special Thanx to nietod, too. Sorry but karouris solution was more time-save than working out the full translation...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6363403
>> Special Thanx to nietod,
You are wecome, but...

>>  Sorry but karouris solution was more
>> time-save than working out the full
>> translation...
But you never asked how to make this work in BCB.  You asked how to translate it to C++.   I spent the time translating it to C++.  Is spent the time helping you debug it.  

Furthermore when I learned this was for BCB, I suggested
"there are features in builder for compiling pascal code."

this is the answer you chose to use.
0
 

Author Comment

by:reggler
ID: 6363577
>>Furthermore when I learned this was for BCB, I suggested
>>"there are features in builder for compiling pascal code."

nietod, i wrote:
"i want to use the code in c++builder after translation "

sorry for that. i know that i asked how to translate it... but u know, i gave karouri the points because i use his solution now. i'm very happy that u wanted to help me... i would give u points too but i don't have some anymore... sorry 'bout that.

THANK YOU!
0
 
LVL 22

Expert Comment

by:nietod
ID: 6363606
Yes you did.  It wasn't in the original question.  I'm sorry I missed that.  

Its that just that I felt like you asked for soemthing and I gave it to you, then you decided to change the quesiton to something else and gave the points to someone who answered that question.  (Although I had actually propsoed that solution too.)

Well, nothing can be done about it now.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now