Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Delphi - C++ Translation

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
reggler
Asked:
reggler
  • 15
  • 14
  • 4
  • +4
1 Solution
 
regglerAuthor Commented:
i want to use the code in c++builder after translation
0
 
nietodCommented:
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
 
nietodCommented:
The line

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

becomes

if (2<= FDay && Fday <= 5)
  Days += 7;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RideOnCommented:
the compiler doesn't know the function Trunc.
don't know what it does make....
0
 
RideOnCommented:
and something else nietod, function declaration:

string PCode(TDateTime ADate);
             
string? that should be char.
0
 
regglerAuthor Commented:
hi

thanx for your help rideon & nietod.
erm, nietod, rideon is right, the compiler doesn't know Trunc.
0
 
regglerAuthor Commented:
hm, the help in c++builder can find the function but the compiler doesn't....
0
 
nietodCommented:
>> 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
 
regglerAuthor Commented:
Days = Trunc(int(Date)-JanF)+7-DayOfWeek(Date-1);
0
 
RideOnCommented:
oh yes, should be AnsiString. Sorry for that mistake reggler....my fault...
0
 
regglerAuthor Commented:
n.p.
0
 
nietodCommented:
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
 
regglerAuthor Commented:
the function Ord is unknown too
Days += 7 * Ord(FDay IN [2..5]);


i open the question now for other experts...
0
 
nietodCommented:
>> 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
 
regglerAuthor Commented:
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
 
nietodCommented:
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
 
regglerAuthor Commented:
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
 
nietodCommented:
Try

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

or

AnsiString Tmp;
  Tmp = YStr;

Result = Tmp+WStr+DStr;
0
 
jkrCommented:
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
 
regglerAuthor Commented:
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
 
nietodCommented:
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
 
RideOnCommented:
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
 
nietodCommented:
It can't be blank.  It is an integer.  It has to have a value.  What is the value?  Is it zero?  
0
 
regglerAuthor Commented:
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
 
nietodCommented:
You said that the line that has the exceptions is

JanF = EncodeDate(Y, 1, 1);

What is y's value?
0
 
regglerAuthor Commented:
1242426
0
 
nietodCommented:
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
 
karouriCommented:
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
 
karouriCommented:
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
 
nietodCommented:
>> 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
 
AxterCommented:
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
 
scrapdogCommented:
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
 
regglerAuthor Commented:
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
 
regglerAuthor Commented:
aaah Karouri
already got it (*.hpp) :o)
0
 
regglerAuthor Commented:
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
 
nietodCommented:
>> 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
 
regglerAuthor Commented:
>>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
 
nietodCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 15
  • 14
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now