File: PROTOCOL\TProtocol.cpp

    1 //******************************************************************************
    2 //Datei     : TProtocol.cpp
    3 //Projekt   : XCTL
    4 //Subsystem : Protokollbuch
    5 //Autor     : Jens Klier <klier@informatik.hu-berlin.de>
    6 //            Institut fuer Informatik,
    7 //            Humboldt-Universitaet Berlin
    8 //Inhalt    : Implementation der Klasse TProtocol
    9 //******************************************************************************
   10 //!neu klier Protokollbuch
   11 #include "internls\evrythng.h"
   12 #include <ctype.h>
   13 #include <commdlg.h>
   14 
   15 // Bibliotheken zum Zugriff auf das Dateisystem (16/32bit)
   16 // damit koennen die Dateiattribute der Protokolldatei geaendert werden
   17 #include <winbase.h>
   18 
   19 #include "protocol\TProtocolLanguage.h"
   20 #include "protocol\TProtocol.h"
   21 
   22 
   23 
   24 //--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--
   25 
   26 #define Trennzeichen ";"
   27 
   28 extern TProtocolLanguage *ProtocolLanguage;
   29 TProtocol *Protocol;
   30 
   31 int CALLBACK EnumMetafileProc( HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpMFR, int nObj, LPARAM lpData )
   32 {
   33         char buf[MaxString], buf2[MaxString];
   34         int i;
   35 
   36         if ( (lpMFR->iType == 27)  || (lpMFR->iType == 54)  || (lpMFR->iType == 84)  ||
   37                  (lpMFR->iType == 531) || (lpMFR->iType == 532) || (lpMFR->iType == 1313))
   38         {
   39                 if (lpMFR->iType == 531)
   40                 {
   41                         Protocol->SetXY('L', lpMFR->dParm[1], lpMFR->dParm[0]);
   42                 }
   43                 if (lpMFR->iType == 54)
   44                 {
   45                         Protocol->SetXY('L', lpMFR->dParm[0], lpMFR->dParm[1]);
   46                 }
   47                 if (lpMFR->iType == 532)
   48                 {
   49                         Protocol->SetXY('M', lpMFR->dParm[1], lpMFR->dParm[0]);
   50                 }
   51                 if (lpMFR->iType == 27)
   52                 {
   53                         Protocol->SetXY('M', lpMFR->dParm[0], lpMFR->dParm[1]);
   54                 }
   55                 if (lpMFR->iType == 1313)
   56                 {
   57                         strcpy(buf, "");
   58                         for (i= 1; i <= ceil((float)(lpMFR->dParm[0] / 2)); i++)
   59                         {
   60                                 sprintf(buf2, "%c%c", lpMFR->dParm[i] % 256, lpMFR->dParm[i] / 256);
   61                                 strcat( buf, buf2 );
   62                         }
   63                         if ((lpMFR->dParm[0] % 2) != 0)
   64                         {
   65                                 sprintf(buf2, "%c", lpMFR->dParm[i] % 256);
   66                                 strcat( buf, buf2 );
   67                         }
   68                         Protocol->SetXY('T', lpMFR->dParm[lpMFR->nSize - 4], lpMFR->dParm[lpMFR->nSize - 5], buf);
   69                 }
   70                 if (lpMFR->iType == 84)
   71                 {
   72                         strcpy(buf, "");
   73                         for (i= 1; i <= ceil((float)(lpMFR->dParm[9] / 2)); i++)
   74                         {
   75                                 sprintf(buf2, "%c%c", lpMFR->dParm[i+16] % 65536, lpMFR->dParm[i+16] / 65536);
   76                                 strcat( buf, buf2 );
   77                         }
   78                         if ((lpMFR->dParm[9] % 2) != 0)
   79                         {
   80                                 sprintf(buf2, "%c", lpMFR->dParm[i+16] % 65536);
   81                                 strcat( buf, buf2 );
   82                         }
   83                         Protocol->SetXY('T', lpMFR->dParm[0], lpMFR->dParm[1], buf);
   84                 }
   85   }
   86 
   87         return 1;
   88 }
   89 
   90 TProtocol::TProtocol( int Feldelemente ) : Felder(Feldelemente),
   91                 currentINDEX(NULL), lastINDEX(NULL), firstINDEX(NULL),
   92                 currentPOINT(NULL), firstPOINT(NULL), lastPOINT(NULL),
   93                 bActiv(FALSE), iProtocolOnStatus(Off), Parameter(0)
   94 {
   95         Parameter= new TParameter[Feldelemente];
   96         if (!Parameter)
   97         {
   98                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(1), ProtocolLanguage->GetFailure(), MBFAILURE);
   99                 _FREELIST(Parameter);
  100         }
  101 
  102         SetAllParameterReadWrite();
  103 
  104         strcpy(CurrentLfdNr, "");
  105         strcpy(CopyLfdNr, "");
  106 };
  107 
  108 TProtocol::~TProtocol()
  109 {
  110         freeINDEX();
  111         freePic();
  112         _FREELIST(Parameter);
  113 };
  114 
  115 // Initialisiert das Protokollbuch
  116 BOOL TProtocol::Initialize()
  117 {
  118         if (bActiv)
  119                 return TRUE;
  120         if ( Parameter == NULL )
  121         {
  122                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  123                                    ProtocolLanguage->GetFailure(), MBFAILURE);
  124                 return FALSE;
  125         }
  126         Protocol= this;
  127         if ( GetPrivateProfileInt("Protocol", "Debug", 0, GetCFile()) == 1)
  128                 bDebug= TRUE;
  129         else
  130                 bDebug= FALSE;
  131         if ( GetPrivateProfileInt(szProtocolSection, "NoSortNew", 0, GetCFile()) == 1)
  132                 bNoSortNew= TRUE;
  133         else
  134                 bNoSortNew= FALSE;
  135         if ( GetPrivateProfileInt(szProtocolSection, "SortDate", 0, GetCFile()) == 1)
  136                 bSortDate= TRUE;
  137         else
  138                 bSortDate= FALSE;
  139         bActiv= ReloadProtocol();
  140         return bActiv;
  141 };
  142 
  143 // Initialisiert das Protokollbuch neu
  144 BOOL TProtocol::ReloadProtocol()
  145 {
  146         FILE *out;
  147         OFSTRUCT of;
  148         OPENFILENAME ofn;
  149         char buf[MaxLine], buf2[_MAX_PATH];
  150 
  151         freeINDEX();
  152         freePic();
  153         ClearProtocolString();
  154         // Test ob die Protokolldatei existiert
  155         while (OpenFile(GetPFile(), &of, OF_EXIST) == HFILE_ERROR)
  156         {
  157                 sprintf(buf, ProtocolLanguage->GetMessage(2), GetPFile());
  158                 if (MessageBox(GetFocus(), buf, ProtocolLanguage->GetQuestion(), MBASK) == IDNO)
  159                 {
  160                         sprintf(buf, "%s%s", GetPName(), ".CSV");
  161                         // Initialize the structure
  162                         memset( &ofn, '\0', sizeof( OPENFILENAME ) );
  163                         ofn.lpstrTitle= ProtocolLanguage->GetMessage(17);
  164                         ofn.lStructSize= sizeof( OPENFILENAME );
  165                         ofn.hwndOwner= GetFocus();
  166                         ofn.lpstrFilter= "csv-Dateien (*.CSV)\0*.CSV\0\0";
  167                         ofn.nFilterIndex= 1;
  168                         ofn.lpstrFile= buf;
  169                         ofn.nMaxFile= _MAX_PATH;
  170                         ofn.lpstrInitialDir= GetPDirectory();
  171                         ofn.Flags= OFN_HIDEREADONLY;
  172                         if ( !GetOpenFileName(&ofn) )
  173                         {
  174                                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  175                                                    ProtocolLanguage->GetFailure(), MBFAILURE);
  176                                 return FALSE;
  177                         }
  178                         SetPFile(buf);
  179                 }
  180                 else
  181                 {
  182                         if ( (out= fopen(GetPFile(), "wt")) == NULL )
  183                         {
  184                                 sprintf(buf, ProtocolLanguage->GetMessage(3), GetPDirectory());
  185                                 MessageBox(GetFocus(), buf, ProtocolLanguage->GetFailure(), MBFAILURE);
  186                                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  187                                                    ProtocolLanguage->GetFailure(), MBFAILURE);
  188                                 return FALSE;
  189                         }
  190                         strcpy(buf, "");
  191                         for (int i= 0; i < Felder; i++)
  192                         {
  193                                 if (i > 0)
  194                                         strcat(buf, Trennzeichen);
  195                                 strcat(buf, GetParameterNamen(i, buf2));
  196                         }
  197                         strcat(buf, "\n");
  198                         fputs(buf, out);
  199                         fclose(out);
  200 
  201                         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  202                 }
  203         }
  204         if ( !LoadProtocolFileIndex() )
  205         {
  206                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  207                                    ProtocolLanguage->GetFailure(), MBFAILURE);
  208                 return FALSE;
  209         }
  210         sprintf(buf, "%s%s", GetPName(), ".BK5");
  211         strcpy(buf, GetProtocolBackupFile(buf));
  212         strcpy(buf2, buf);
  213 
  214         // Schreibschutzattribut der Protokolldatei wird gesetzt
  215         SetFileAttributes(buf, FILE_ATTRIBUTE_ARCHIVE);
  216 
  217         remove(buf);
  218 
  219         for (int i= 4; i > 0; i--)
  220         {
  221                 buf2[strlen(buf2) - 1]--;
  222                 rename(buf2, buf);
  223                 buf[strlen(buf) - 1]--;
  224         }
  225         if (!BackupFile( GetPFile(), buf2))
  226         {
  227                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  228                                    ProtocolLanguage->GetFailure(), MBFAILURE);
  229                 return FALSE;
  230         }
  231         if (!bNoSortNew)
  232                 if (!SortProtocolFile())
  233                 {
  234                         MessageBox(GetFocus(), ProtocolLanguage->GetMessage(4),
  235                                            ProtocolLanguage->GetFailure(), MBFAILURE);
  236                         return FALSE;
  237                 }
  238         return TRUE;
  239 };
  240 
  241 // Erzeugt eine Backupkopie einer Datei
  242 BOOL TProtocol::BackupFile( LPCSTR File, LPCSTR BFile )
  243 {
  244         FILE *in, *out;
  245         char buf[MaxLine];
  246 
  247         // Schreibschutzattribut der Protokolldatei wird entfernt
  248         SetFileAttributes(BFile, FILE_ATTRIBUTE_ARCHIVE);
  249 
  250         if ( (in= fopen(File, "rt")) == NULL)
  251                 return FALSE;
  252         if ( (out= fopen(BFile, "wt")) == NULL )
  253                 return FALSE;
  254         while (!feof(in))
  255         {
  256                 if ( fgets( buf, MaxLine, in ) != NULL )
  257                         fputs(buf, out);
  258         }
  259         fclose(in);
  260         fclose(out);
  261 
  262         // Schreibschutzattribut der Protokolldatei wird gesetzt
  263         SetFileAttributes(BFile, FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  264 
  265         return TRUE;
  266 };
  267 
  268 // Erzeugt aus der Protokolldatei einen Index im Speicher
  269 // Die Lfd.-Nr. und die Position des Datensatzes innerhalb der Datei werden
  270 // als Index im Speicher abgelegt.
  271 BOOL TProtocol::LoadProtocolFileIndex( LPCSTR lLfdNr )
  272 {
  273         freeINDEX(lLfdNr);
  274         return AppendProtocolFileIndex();
  275 };
  276 
  277 BOOL TProtocol::AppendProtocolFileIndex( void )
  278 {
  279         FILE *in;
  280         int nr= 0;
  281         char buf[MaxLine], buf2[MaxLfdNr];
  282         long FilePos;
  283 
  284         bIsSortLfdNr= bIsSortDate= TRUE;
  285         currentINDEX= lastINDEX;
  286         if ( (in= fopen(GetPFile(), "rt")) == NULL )
  287                 return FALSE;
  288         if (currentINDEX != NULL)
  289         {
  290                 if ( fseek(in, currentINDEX->Pos, SEEK_SET) != 0 )
  291                 {
  292                         fclose(in);
  293                         return FALSE;
  294                 }
  295                 if ( fgets( buf, MaxLine, in ) == NULL )
  296                 {
  297                         fclose(in);
  298                         return FALSE;
  299                 }
  300                 nr= currentINDEX->Nr;
  301         }
  302         while (!feof(in))
  303         {
  304                 FilePos= ftell(in);
  305                 if ( fgets(buf, MaxLine, in ) != NULL )
  306                 {
  307                         if ( strncmp(buf, "Lfd", 3) != 0 )
  308                         {
  309                                 if ( !StrSplit(buf) )
  310                                 {
  311                                         fclose(in);
  312                                         return FALSE;
  313                                 }
  314                                 lastINDEX= currentINDEX;
  315 
  316                                 currentINDEX= new TFileIndex;
  317                                 if(!currentINDEX)
  318                                 {
  319                                         fclose(in);
  320                                         MessageBox(GetFocus(), ProtocolLanguage->GetMessage(5), ProtocolLanguage->GetFailure(), MBFAILURE);
  321                                         return FALSE;
  322                                 }
  323 
  324                                 if (nr == 0)
  325                                         firstINDEX= lastINDEX= currentINDEX;
  326                                 currentINDEX->Nr= nr + 1;
  327                                 currentINDEX->Pos= FilePos;
  328                                 strcpy(currentINDEX->LfdNr, GetParameterLfdNr(buf2));
  329                                 MakeDateString(GetParameterDate(buf2), currentINDEX->Date);
  330                                 currentINDEX->Status= atoi(GetProtocolValue(Felder - 1, buf2));
  331                                 lastINDEX->next= currentINDEX;
  332                                 currentINDEX->last= lastINDEX;
  333                                 currentINDEX->next= NULL;
  334                                 if (nr++ == 0)
  335                                         firstINDEX->last= NULL;
  336                                 else
  337                                 {
  338                                         if (strcmp(currentINDEX->LfdNr, lastINDEX->LfdNr) < 0)
  339                                                 bIsSortLfdNr= FALSE;
  340                                         if (strcmp(currentINDEX->Date, lastINDEX->Date) < 0)
  341                                                 bIsSortDate= FALSE;
  342                                 }
  343                         }
  344                 }
  345         }
  346         fclose(in);
  347         lastINDEX= currentINDEX;
  348         currentINDEX= NULL;
  349         ReloadProtocolString(GetLfdNr(buf));
  350         return TRUE;
  351 };
  352 
  353 // Setzt den aktuellen Index auf NULL
  354 void TProtocol::ResetIndex()
  355 {
  356         currentINDEX= NULL;
  357 };
  358 
  359 // Loescht den Index und gibt den Speicher frei
  360 void TProtocol::freeINDEX( LPCSTR lLfdNr )
  361 {
  362         TFileIndex *tmpINDEX;
  363 
  364         if (firstINDEX == NULL)
  365                 return;
  366         if (!SearchProtocolString(lLfdNr))
  367         {
  368                 currentINDEX= firstINDEX;
  369                 firstINDEX= tmpINDEX= NULL;
  370         }
  371         else
  372         {
  373                 tmpINDEX= currentINDEX->last;
  374                 if (tmpINDEX != NULL)
  375                         tmpINDEX->next= NULL;
  376         }
  377         for (currentINDEX= currentINDEX->next; currentINDEX != NULL; currentINDEX= currentINDEX->next)
  378                 _FREEOBJ(currentINDEX->last);
  379         _FREEOBJ(lastINDEX);
  380         currentINDEX= lastINDEX= tmpINDEX;
  381 };
  382 
  383 // Zerlegt den eingelesenen Protokollstring und traegt die Parameter in das
  384 // Parameterfeld ein (Hochkomma am Anfang und am Ende eines Wertes werden entfernt)
  385 BOOL TProtocol::StrSplit(LPCSTR sLine)
  386 {
  387         char buf[MaxLine], buf2[MaxString], buf3[MaxString];
  388         char *token, *sepp, fieldsepp[]= Trennzeichen;
  389         int i, j, k, nfield, seppc;
  390 
  391         strcpy(buf, sLine);
  392         // Zeilenende entfernen
  393         if ( buf[strlen(buf) - 1] == '\r' || buf[strlen(buf) - 1] == '\n')
  394                 buf[strlen(buf) - 1]= '\0';
  395         if ( buf[strlen(buf) - 1] == '\r' )
  396                 buf[strlen(buf) - 1]= '\0';
  397         ClearProtocolString();  // Parameterfeld löschen
  398         token= buf;
  399         nfield= 0;
  400         do
  401         {
  402                 if (*token == '"')
  403                 {
  404                         token++;  // erstes " überspringen
  405                         for (i= j= 0; token[j] != '\0'; i++, j++)
  406                         {
  407                                 if (token[j] == '"' && token[++j] != '"')
  408                                 {
  409                                         k= strcspn(token + j, fieldsepp);
  410                                         memmove(token + i, token + j, k);
  411                                         i += k;
  412                                         j += k;
  413                                         break;
  414                                 }
  415                                 token[i]= token[j];
  416                         }
  417                         token[i]= '\0';
  418                         sepp= token + j;
  419                 }
  420                 else
  421                         sepp= token + strcspn(token, fieldsepp);
  422                 seppc= sepp[0];
  423                 sepp[0]= '\0';  //Feld 0-terminiert
  424                 if ( !SetProtocolValue(nfield++, token) )
  425                 {
  426                         sprintf(buf, ProtocolLanguage->GetMessage(15),
  427                                         GetParameterNamen(nfield - 1, buf2), token);
  428                         MessageBox(GetFocus(), buf, ProtocolLanguage->GetFailure(), MBFAILURE);
  429                         return FALSE;
  430                 }
  431                 token= sepp + 1;
  432         } while (seppc == fieldsepp[0] && nfield < Felder);
  433         GetParameterLfdNr(buf2);
  434         GetParameterDate(buf3);
  435         if (!TestLfdNr(buf2) || !TestDate(buf3))
  436         {
  437                 sprintf(buf, ProtocolLanguage->GetMessage(16), buf2, buf3);
  438                 MessageBox(GetFocus(), buf, ProtocolLanguage->GetFailure(), MBFAILURE);
  439                 return FALSE;
  440         }
  441         return TRUE;
  442 };
  443 
  444 // Erzeugt aus dem Parameterfeld einen Protokollstring
  445 // Enthaelt ein Parameter das Trennzeichen wird er in Hochkomma eingeschlossen
  446 LPSTR TProtocol::StrMerge(LPSTR sLine)
  447 {
  448         char buf[MaxLine];
  449         char fieldsepp[]= Trennzeichen;
  450         int i, j, k;
  451 
  452         strcpy(sLine, "");
  453         for (i= 0; i < Felder; i++)
  454         {
  455                 for (j= k= 0; j < strlen(Parameter[i].Wert); j++)
  456                 {
  457                         buf[k]= Parameter[i].Wert[j];
  458                         if (buf[k++] == '"')
  459                                 buf[k++]= '"';
  460                 }
  461                 buf[k]= '\0';
  462                 if (strstr(buf, fieldsepp) != NULL)
  463                 {
  464                         strcat(sLine, "\"");
  465                         strcat(sLine, buf);
  466                         strcat(sLine, "\"");
  467                 }
  468                 else
  469                         strcat(sLine, buf);
  470                 if (i != Felder - 1)
  471                         strcat(sLine, fieldsepp);
  472         }
  473         strcat(sLine, "\n");
  474         return sLine;
  475 };
  476 
  477 // Erzeugt aus einem Datum einen speziellen String fuer die Suche in der Protokolldatei
  478 // 20.4.1999 -->  19990420
  479 LPSTR TProtocol::MakeDateString( LPCSTR Date, LPSTR DateString )
  480 {
  481         char *token;
  482         char buf[MaxString];
  483 
  484         if (strlen(Date) != 0)
  485         {
  486                 strcpy(buf, Date);
  487                 token= strtok(strrev(buf), ".");
  488                 if (strlen(token) == 2)
  489                         strcpy(DateString, "20");
  490                 else
  491                         strcpy(DateString, "");
  492                 strcat(DateString, strrev(token));
  493                 token= strtok(NULL, ".");
  494                 if (strlen(token) == 1)
  495                         strcat(DateString, "0");
  496                 strcat(DateString, strrev(token));
  497                 token= strtok(NULL, ".");
  498                 if (strlen(token) == 1)
  499                         strcat(DateString, "0");
  500                 strcat(DateString, strrev(token));
  501         }
  502         else
  503                 strcpy(DateString, "");
  504         return DateString;
  505 };
  506 
  507 // Prueft eine Zeichenkette ob sie ein gueltiges Datum enthaelt
  508 BOOL TProtocol::TestDate( LPCSTR Date )
  509 {
  510         char buf[MaxString / 4];
  511         char *token;
  512         int i;
  513 
  514         if ( (strlen(Date) < 6) || (strlen(Date) > 10) )
  515                 return FALSE;
  516         strcpy(buf, Date);
  517         if ( (token= strtok(buf, ".")) == NULL )
  518                 return FALSE;
  519         if ( strlen(token) != 1 && strlen(token) != 2 )
  520                 return FALSE;
  521         for (i= 0; i < strlen(token); i++)
  522                 if ( !isdigit(token[i]) )
  523                         return FALSE;
  524         if ( (token= strtok(NULL, ".")) == NULL )
  525                 return FALSE;
  526         if ( strlen(token) != 1 && strlen(token) != 2 )
  527                 return FALSE;
  528         for (i= 0; i < strlen(token); i++)
  529                 if ( !isdigit(token[i]) )
  530                         return FALSE;
  531         if ( (token= strtok(NULL, ".")) == NULL )
  532                 return FALSE;
  533         if ( strlen(token) != 2 && strlen(token) != 4 )
  534                 return FALSE;
  535         for (i= 0; i < strlen(token); i++)
  536                 if ( !isdigit(token[i]) )
  537                         return FALSE;
  538         if ( strtok(NULL, ".") != NULL )
  539                 return FALSE;
  540         return TRUE;
  541 };
  542 
  543 // ist TRUE wenn der aktuelle Protokollstring der Erste in der Protokolldatei ist
  544 BOOL TProtocol::IsFirstProtocolString( )
  545 {
  546         if (currentINDEX != NULL)
  547                 if (currentINDEX->last == NULL)
  548                         return TRUE;
  549         return FALSE;
  550 };
  551 
  552 // ist TRUE wenn der aktuelle Protokollstring der Erste in der Protokolldatei ist
  553 BOOL TProtocol::IsLastProtocolString( )
  554 {
  555         if (currentINDEX != NULL)
  556                 if (currentINDEX->next == NULL)
  557                         return TRUE;
  558         return FALSE;
  559 };
  560 
  561 // ist TRUE wenn der aktuelle Protokollstring der Erste in der Protokolldatei ist
  562 BOOL TProtocol::IsNewProtocolString( )
  563 {
  564         if (currentINDEX == NULL)
  565                 return TRUE;
  566         return FALSE;
  567 };
  568 
  569 // ist TRUE wenn die Protokolldatei leer ist
  570 BOOL TProtocol::IsEmptyProtocolFile( )
  571 {
  572         if (firstINDEX == NULL)
  573                 return TRUE;
  574         return FALSE;
  575 };
  576 
  577 BOOL TProtocol::InitializeSearchProtocolString( LPCSTR lLfdNr )
  578 {
  579         iSearch= 0;
  580         if ( strlen(lLfdNr) > 0 )
  581                 return SearchProtocolString(lLfdNr);
  582         currentINDEX= firstINDEX;
  583         return TRUE;
  584 };
  585 
  586 // sucht einen Protokollstring anhand der Lfd.-Nr. im Protokollindex
  587 // wird die Lfd.-Nr. gefunden liefert die Methode TRUE zurueck
  588 BOOL TProtocol::SearchProtocolString( LPCSTR lLfdNr )
  589 {
  590         TFileIndex *curINDEX;
  591 
  592         if ( strlen(lLfdNr) == 0 )
  593                 return FALSE;
  594         curINDEX= firstINDEX;
  595         while ( curINDEX != NULL )
  596         {
  597                 if ( strlen(lLfdNr) > 6)
  598                 {
  599                         if ( strcmp(curINDEX->LfdNr, lLfdNr) == 0 )
  600                         {
  601                                 currentINDEX= curINDEX;
  602                                 return TRUE;
  603                         }
  604                 }
  605                 else
  606                 {
  607                         if ( strncmp(curINDEX->LfdNr, lLfdNr, strlen(lLfdNr)) == 0 )
  608                         {
  609                                 currentINDEX= curINDEX;
  610                                 return TRUE;
  611                         }
  612                 }
  613                 curINDEX= curINDEX->next;
  614         }
  615         return FALSE;
  616 };
  617 
  618 // speichert einen Protokollstring in der Protokolldatei
  619 BOOL TProtocol::ImportProtocolFile( )
  620 {
  621         FILE *in, *out, *log;
  622         char buf[MaxLine], buf2[MaxString], ImportFile[_MAX_PATH];
  623         char ImportLfdNr[MaxString], tmpLfdNr[MaxString]= "";
  624         OPENFILENAME ofn;
  625         OFSTRUCT of;
  626         int iCount= 0, iErrorLfdNr= 0, iErrorMaxLen= 0, iErrorDelete= 0;
  627         int iCountSkizze= 0, iErrorSkizze= 0, iErrorExistSkizze= 0;
  628         //Hilfsvariablen für die Bestandteile einen Dateinamens
  629         char Drive[_MAX_DRIVE], Dir[_MAX_DIR], Name[_MAX_FNAME], Ext[_MAX_EXT], Path[_MAX_PATH];
  630 
  631         strcpy(ImportFile, "");
  632         // Initialize the structure
  633         memset( &ofn, '\0', sizeof( OPENFILENAME ) );
  634         ofn.lpstrTitle= ProtocolLanguage->GetMessage(17);
  635         ofn.lStructSize= sizeof( OPENFILENAME );
  636         ofn.hwndOwner= GetFocus();
  637         ofn.lpstrFilter= "csv-Dateien (*.CSV)\0*.CSV\0Backup-Dateien (*.BK*)\0*.BK*\0Alle Dateien (*.*)\0*.*\0\0";
  638         ofn.nFilterIndex= 1;
  639         ofn.lpstrFile= ImportFile;
  640         ofn.nMaxFile= _MAX_PATH;
  641         ofn.lpstrInitialDir= GetPDirectory();
  642         ofn.Flags= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  643         if ( !GetOpenFileName( &ofn ) )
  644                 return FALSE;
  645         if ( OpenFile(ImportFile, &of, OF_EXIST) == HFILE_ERROR)
  646                 return FALSE;
  647         if ( (in= fopen(ImportFile, "rt")) == NULL )
  648                 return FALSE;
  649         _splitpath( ImportFile, Drive, Dir, Name, Ext );
  650         sprintf( Path, "%s%s", Drive, Dir );
  651         BackupFile(GetPFile(), GetProtocolFile(GetPName(), ".tmp"));
  652 
  653         // Schreibschutzattribut der mProtokolldatei wird geloescht
  654         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE);
  655         SetFileAttributes(GetProtocolFile(GetPName(), ".tmp"), FILE_ATTRIBUTE_ARCHIVE);
  656 
  657         if ( (out= fopen(GetPFile(), "at")) == NULL )
  658                 return FALSE;
  659         if ( (log= fopen(GetProtocolFile("import.log"), "wt")) == NULL )
  660                 return FALSE;
  661         if (currentINDEX != NULL)
  662                 strcpy(tmpLfdNr, currentINDEX->LfdNr);
  663         else
  664                 strcpy(tmpLfdNr, "");
  665         while (!feof(in))
  666         {
  667                 if ( fgets( buf, MaxLine, in ) != NULL )
  668                 {
  669                         if ( strncmp(buf, "Lfd", 3) != 0 )
  670                         {
  671                                 if (StrSplit(buf))
  672                                 {
  673                                         GetParameterLfdNr(ImportLfdNr);
  674                                         if ( strcmp(GetProtocolValue(Felder - 1, buf2), "2") != 0 )
  675                                         {
  676                                                 if ( !SearchProtocolString(ImportLfdNr) )
  677                                                 {
  678                                                         fputs(buf, out);
  679                                                         fprintf(log, "%s --> %d. LfdNr importiert", ImportLfdNr, ++iCount);
  680                                                 }
  681                                                 else
  682                                                         fprintf(log, "%s --> %d. LfdNr existiert bereits", ImportLfdNr, ++iErrorLfdNr);
  683                                                 if (OpenFile(GetSkizzeFile(ImportLfdNr), &of, OF_EXIST) == HFILE_ERROR)
  684                                                 {
  685                                                         sprintf( ImportFile, "%s%s.wmf", Path, ImportLfdNr);
  686                                                         if (OpenFile(ImportFile, &of, OF_EXIST) == HFILE_ERROR)
  687                                                         {
  688                                                                 sprintf( ImportFile, "%sskizzen\\%s.wmf", Path, ImportLfdNr);
  689                                                                 if (OpenFile(ImportFile, &of, OF_EXIST) != HFILE_ERROR)
  690                                                                 {
  691                                                                         CopyEnhMetaFile(GetEnhMetaFile(ImportFile), GetSkizzeFile(ImportLfdNr));
  692 
  693                                                                         fprintf(log, " --> %d. Skizze importiert\n", ++iCountSkizze);
  694                                                                 }
  695                                                                 else
  696                                                                         fprintf(log, " --> %d. Skizze existiert nicht\n", ++iErrorSkizze);
  697                                                         }
  698                                                         else
  699                                                         {
  700                                                                 CopyEnhMetaFile(GetEnhMetaFile(ImportFile), GetSkizzeFile(ImportLfdNr));
  701 
  702                                                                 fprintf(log, " --> %d. Skizze importiert\n", ++iCountSkizze);
  703                                                         }
  704                                                 }
  705                                                 else
  706                                                         fprintf(log, " --> %d. Skizze existiert bereits\n", ++iErrorExistSkizze);
  707                                         }
  708                                         else
  709                                                 fprintf(log, "%s --> %d. LfdNr ist als gelöscht markiert\n", ImportLfdNr, ++iErrorDelete);
  710                                 }
  711                                 else
  712                                         fprintf(log, "!!!!!!!!!! --> %d. Messung enthält fehlerhafte Daten\n", ++iErrorMaxLen);
  713                         }
  714                 }
  715         }
  716         fclose(in);
  717         fclose(out);
  718         fprintf(log, "\n\n%s%d\n%s%d\n\n%s%d\n%s%d\n%s%d\n\n%s%d\n%s%d", 
  719                         ProtocolLanguage->GetMessage(8), iCount, 
  720                         ProtocolLanguage->GetMessage(9), iCountSkizze, 
  721                         ProtocolLanguage->GetMessage(10), iErrorLfdNr, 
  722                         ProtocolLanguage->GetMessage(11), iErrorMaxLen, 
  723                         ProtocolLanguage->GetMessage(12), iErrorDelete, 
  724                         ProtocolLanguage->GetMessage(13), iErrorExistSkizze, 
  725                         ProtocolLanguage->GetMessage(14), iErrorSkizze);
  726         fclose(log);
  727 
  728         if ( !LoadProtocolFileIndex() )
  729         {
  730                 remove
  731                         (GetPFile());
  732                 rename(GetProtocolFile(GetPName(), ".tmp"), GetPFile());
  733                 LoadProtocolFileIndex();
  734                 if (strlen(tmpLfdNr) > 0)
  735                         LoadProtocolString(tmpLfdNr);
  736                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(7), ProtocolLanguage->GetFailure(), MBFAILURE);
  737                 return FALSE;
  738         }
  739         remove
  740                 (GetProtocolFile(GetPName(), ".tmp"));
  741 
  742         // Schreibschutzattribut der Protokolldatei wird gesetzt
  743         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  744 
  745         SortProtocolFile();
  746 
  747         ReloadProtocolString(tmpLfdNr);
  748         sprintf(buf, "%s%d\n%s%d\n\n%s%d\n%s%d\n%s%d\n\n%s%d\n%s%d", 
  749                         ProtocolLanguage->GetMessage(8), iCount, 
  750                         ProtocolLanguage->GetMessage(9), iCountSkizze, 
  751                         ProtocolLanguage->GetMessage(10), iErrorLfdNr, 
  752                         ProtocolLanguage->GetMessage(11), iErrorMaxLen, 
  753                         ProtocolLanguage->GetMessage(12), iErrorDelete, 
  754                         ProtocolLanguage->GetMessage(13), iErrorExistSkizze, 
  755                         ProtocolLanguage->GetMessage(14), iErrorSkizze);
  756         MessageBox(GetFocus(), buf, ProtocolLanguage->GetMessage(), MBINFO);
  757         return TRUE;
  758 };
  759 
  760 // speichert einen Protokollstring in der Protokolldatei
  761 BOOL TProtocol::SaveProtocolString( )
  762 {
  763         FILE *in, *out;
  764         char buf[MaxLine], buf2[MaxLine];
  765         long FilePos;
  766 
  767         GetParameterLfdNr(buf);
  768         GetParameterDate(buf2);
  769         if (!TestLfdNr(buf) || !TestDate(buf2))
  770         {
  771                 sprintf(buf, ProtocolLanguage->GetMessage(16), buf, buf2);
  772                 MessageBox(GetFocus(), buf, ProtocolLanguage->GetFailure(), MBFAILURE);
  773                 return FALSE;
  774         }
  775         if ( strlen(GetLfdNr(buf)) <= 0 )
  776                 return FALSE;
  777 
  778         // Schreibschutzattribut der Protokolldatei wird geloescht
  779         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE);
  780         SetFileAttributes(GetProtocolFile(GetPName(), ".tmp"), FILE_ATTRIBUTE_ARCHIVE);
  781 
  782         if ( !SearchProtocolString(buf) )
  783         {
  784                 if ( bNoSortNew || ((lastINDEX == NULL) || (strcmp(buf, lastINDEX->LfdNr) >= 0)) )
  785                 {
  786                         if ( (out= fopen(GetPFile(), "at")) == NULL )
  787                                 return FALSE;
  788                         StrMerge(buf);
  789                         fputs(buf, out);
  790                         fclose(out);
  791 
  792                         // Schreibschutzattribut der Protokolldatei wird gesetzt
  793                         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  794 
  795                         AppendProtocolFileIndex();
  796                 }
  797                 else
  798                 {
  799                         currentINDEX= lastINDEX;
  800                         while ((currentINDEX != NULL) && (strcmp(currentINDEX->LfdNr, buf) > 0))
  801                                 currentINDEX= currentINDEX->last;
  802                         if (currentINDEX == NULL)
  803                                 currentINDEX= firstINDEX;
  804                         else
  805                                 currentINDEX= currentINDEX->next;
  806                         if ( (in= fopen(GetPFile(), "rt")) == NULL )
  807                                 return FALSE;
  808                         if ( (out= fopen(GetProtocolFile(GetPName(), ".tmp"), "wt")) == NULL )
  809                                 return FALSE;
  810                         StrMerge(buf);
  811                         while (!feof(in))
  812                         {
  813                                 FilePos= ftell(in);
  814                                 if ( fgets( buf2, MaxLine, in ) != NULL )
  815                                 {
  816                                         if (FilePos == currentINDEX->Pos)
  817                                                 fputs(buf, out);
  818                                         fputs(buf2, out);
  819                                 }
  820                         }
  821                         fclose(out);
  822                         fclose(in);
  823                         remove
  824                                 (GetPFile());
  825                         rename(GetProtocolFile(GetPName(), ".tmp"), GetPFile());
  826 
  827                         // Schreibschutzattribut der Protokolldatei wird gesetzt
  828                         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  829 
  830                         LoadProtocolFileIndex(currentINDEX->LfdNr);
  831                 }
  832         }
  833         else
  834         {
  835 
  836                 if (currentINDEX != NULL)
  837                         currentINDEX->Status= atoi(GetProtocolValue(Felder - 1, buf));
  838 
  839                 if ( (in= fopen(GetPFile(), "r+t")) == NULL )
  840                         return FALSE;
  841                 StrMerge(buf);
  842                 FilePos= ftell(in);
  843                 if ( fseek(in, currentINDEX->Pos, SEEK_SET) != 0 )
  844                 {
  845                         fclose(in);
  846                         return FALSE;
  847                 }
  848                 if ( fgets( buf2, MaxLine, in ) == NULL )
  849                 {
  850                         fclose(in);
  851                         return FALSE;
  852                 }
  853                 if ((strlen(buf) == strlen(buf2)) ||
  854                                 ((currentINDEX == lastINDEX) && (strlen(buf) > strlen(buf2))) )
  855                 {
  856                         if ( fseek(in, currentINDEX->Pos, SEEK_SET) != 0 )
  857                         {
  858                                 fclose(in);
  859                                 return FALSE;
  860                         }
  861                         fputs(buf, in);
  862                         fclose(in);
  863 
  864                         // Schreibschutzattribut der Protokolldatei wird gesetzt
  865                         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  866 
  867                         LoadProtocolFileIndex(currentINDEX->LfdNr);
  868                 }
  869                 else
  870                 {
  871                         if ( fseek(in, FilePos, SEEK_SET) != 0 )
  872                         {
  873                                 fclose(in);
  874                                 return FALSE;
  875                         }
  876                         if ( (out= fopen(GetProtocolFile(GetPName(), ".tmp"), "wt")) == NULL )
  877                                 return FALSE;
  878                         while (!feof(in))
  879                         {
  880                                 FilePos= ftell(in);
  881                                 if ( fgets( buf2, MaxLine, in ) != NULL )
  882                                 {
  883                                         if ( FilePos == currentINDEX->Pos )
  884                                                 fputs(buf, out);
  885                                         else
  886                                                 fputs(buf2, out);
  887                                 }
  888                         }
  889                         fclose(out);
  890                         fclose(in);
  891 
  892                         remove
  893                                 (GetPFile());
  894                         rename(GetProtocolFile(GetPName(), ".tmp"), GetPFile());
  895 
  896                         // Schreibschutzattribut der Protokolldatei wird gesetzt
  897                         SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
  898 
  899                         LoadProtocolFileIndex(currentINDEX->LfdNr);
  900                 }
  901         }
  902         if (!bNoSortNew)
  903                 return SortProtocolFile();
  904         return TRUE;
  905 };
  906 
  907 // loescht alle Felder des Parameterfeldes
  908 void TProtocol::ClearProtocolString( )
  909 {
  910         ClearProtocolString(0, Felder);
  911 };
  912 
  913 // loescht alle Felder von a bis b des Parameterfeldes
  914 void TProtocol::ClearProtocolString( int a, int b )
  915 {
  916         if (a < 0)
  917                 a= 0;
  918         if (b > Felder)
  919                 b= Felder;
  920         for (int i= a; i < b; i++)
  921         {
  922                 strcpy(Parameter[i].Wert, "");
  923                 SetParameterReadWrite(i);
  924         }
  925 };
  926 
  927 // loescht alle Felder des Parameterfeldes und die Skizze
  928 void TProtocol::NewProtocolString( )
  929 {
  930         char buf[MaxLfdNr];
  931 
  932         strcpy(CopyLfdNr, "");
  933         ClearProtocolString();
  934         SetParameterDate(GetSysDate(buf));
  935         SetProtocolValue(Felder - 1, "0");
  936         SaveLfdNr(GetNewLfdNr(buf));
  937         ResetIndex();
  938         freePic();
  939 };
  940 
  941 // loescht alle Felder des Parameterfeldes und die Skizze
  942 void TProtocol::CopyProtocolString( LPCSTR lLfdNr )
  943 {
  944         char buf[MaxLfdNr];
  945 
  946         if (SearchProtocolString(GetLfdNr(buf)))
  947                 strcpy(CopyLfdNr, buf);
  948         ClearOldParameter();
  949         SetParameterDate(GetSysDate(buf));
  950         SetProtocolValue(Felder - 1, "0");
  951         if (lLfdNr != NULL)
  952                 SaveLfdNr(lLfdNr);
  953         else
  954                 SaveLfdNr(GetNewLfdNr(buf));
  955         ResetIndex();
  956 };
  957 
  958 // laed einen Protokollstring aus der Protokolldatei und fuelt das Parameterfeld
  959 // mit den entspraechenden Parametern
  960 BOOL TProtocol::LoadProtocolString( long FilePos )
  961 {
  962         FILE *in;
  963         char buf[MaxLine], buf2[MaxLfdNr];
  964 
  965         if ((in= fopen(GetPFile(), "rt")) == NULL)
  966                 return FALSE;
  967         if ( fseek(in, FilePos, SEEK_SET) != 0 )
  968         {
  969                 fclose(in);
  970                 return FALSE;
  971         }
  972         if ( fgets( buf, MaxLine, in ) == NULL )
  973         {
  974                 fclose(in);
  975                 return FALSE;
  976         }
  977         fclose(in);
  978         StrSplit(buf);
  979         SetLfdNr(GetParameterLfdNr(buf2));
  980         return TRUE;
  981 };
  982 
  983 // laed einen Protokollstring aus der Protokolldatei und fuelt das Parameterfeld
  984 // mit den entspraechenden Parametern
  985 int TProtocol::LoadProtocolString( LPCSTR lLfdNr )
  986 {
  987         if ( !SearchProtocolString(lLfdNr) )
  988         {
  989                 if ( bDebug )
  990                         MessageBox(GetFocus(), "LoadProtocolString NewProtocolString!", "Debug Info", MBINFO);
  991                 NewProtocolString();
  992                 return 1;
  993         }
  994         if ( !LoadProtocolString( currentINDEX->Pos) )
  995                 return -1;
  996         return 0;
  997 };
  998 
  999 // laed einen Protokollstring aus der Protokolldatei und fuelt das Parameterfeld
 1000 // mit den entspraechenden Parametern
 1001 int TProtocol::ReloadProtocolString( LPCSTR lLfdNr )
 1002 {
 1003         if ( strlen(lLfdNr) <= 0 )
 1004         {
 1005                 NewProtocolString();
 1006                 return 2;
 1007         }
 1008         if ( !SearchProtocolString(lLfdNr) )
 1009         {
 1010                 if (strlen(CopyLfdNr) > 0)
 1011                 {
 1012                         if (SearchProtocolString(CopyLfdNr))
 1013                         {
 1014                                 LoadProtocolString(CopyLfdNr);
 1015                                 CopyProtocolString(lLfdNr);
 1016                         }
 1017                         else
 1018                                 NewProtocolString();
 1019                 }
 1020                 else
 1021                         NewProtocolString();
 1022                 Protocol->SetDefaultParameter();
 1023                 SaveLfdNr(lLfdNr);
 1024                 return 1;
 1025         }
 1026         if ( !LoadProtocolString( currentINDEX->Pos) )
 1027                 return -1;
 1028         LoadPicFile(lLfdNr);
 1029         return 0;
 1030 };
 1031 
 1032 // laed den naechsen Protokollstring aus der Protokolldatei und fuelt das
 1033 // Parameterfeld mit den entspraechenden Parametern
 1034 int TProtocol::LoadNextProtocolString( )
 1035 {
 1036         strcpy(CopyLfdNr, "");
 1037         if ( currentINDEX == NULL )
 1038                 currentINDEX= firstINDEX;
 1039         else
 1040                 currentINDEX= currentINDEX->next;
 1041         while (currentINDEX != NULL && IsProtocolDelete())
 1042                 currentINDEX= currentINDEX->next;
 1043         if ( currentINDEX == NULL )
 1044         {
 1045                 NewProtocolString();
 1046                 return 1;
 1047         }
 1048         if ( LoadProtocolString(currentINDEX->Pos) )
 1049                 return 0;
 1050         return -1;
 1051 };
 1052 
 1053 // laed den vorherigen Protokollstring aus der Protokolldatei und fuelt das
 1054 // Parameterfeld mit den entspraechenden Parametern
 1055 int TProtocol::LoadLastProtocolString( )
 1056 {
 1057         TFileIndex *curINDEX;
 1058 
 1059         strcpy(CopyLfdNr, "");
 1060         if ( currentINDEX == NULL )
 1061                 currentINDEX= curINDEX= lastINDEX;
 1062         else
 1063         {
 1064                 curINDEX= currentINDEX;
 1065                 currentINDEX= currentINDEX->last;
 1066         }
 1067         while (currentINDEX != NULL && IsProtocolDelete())
 1068                 currentINDEX= currentINDEX->last;
 1069         if (currentINDEX == NULL)
 1070                 currentINDEX= curINDEX;
 1071         if ( LoadProtocolString(currentINDEX->Pos) )
 1072         {
 1073                 if ( currentINDEX->last == NULL || (currentINDEX == curINDEX && currentINDEX != lastINDEX) )
 1074                         return 1;
 1075                 else
 1076                         return 0;
 1077         }
 1078         return -1;
 1079 };
 1080 
 1081 // macht aus dem Systemdatum einen String
 1082 LPSTR TProtocol::GetSysDate( LPSTR Value )
 1083 {
 1084         SYSTEMTIME d;
 1085 
 1086         GetLocalTime(&d);
 1087         sprintf(Value, "%d.%d.%d", d.wDay, d.wMonth, d.wYear);
 1088         return Value;
 1089 };
 1090 
 1091 // der Wert eines Protokollparameters wird in das Protokollparameterfeld eingetragen
 1092 BOOL TProtocol::SetProtocolValue( int Nr, LPCSTR Value )
 1093 {
 1094         char buf[MaxString], buf2[MaxString];
 1095 
 1096         if ( strlen(Value) > GetParameterMaxLen(Nr) )
 1097         {
 1098                 if (bDebug)
 1099                 {
 1100                         sprintf(buf, "%s: %s (max. %d Zeichen!)", GetParameterNamen(Nr, buf2), Value, GetParameterMaxLen(Nr));
 1101                         MessageBox(GetFocus(), buf, "Debug Info", MBINFO);
 1102                 }
 1103                 return FALSE;
 1104         }
 1105         strcpy(Parameter[Nr].Wert, Value);
 1106         return TRUE;
 1107 };
 1108 
 1109 // der Wert eines Protokollparameters wird aus dem Protokollparameterfeld ausgelesen
 1110 LPSTR TProtocol::GetProtocolValue( int Nr, LPSTR Value )
 1111 {
 1112         strcpy(Value, Parameter[Nr].Wert);
 1113         return Value;
 1114 };
 1115 
 1116 // die Variable für die aktuelle Lfd.Nr. wird gesetzt
 1117 void TProtocol::SetLfdNr( LPCSTR Value )
 1118 {
 1119         strcpy(CurrentLfdNr, Value);
 1120 };
 1121 
 1122 // die Variable für die aktuelle Lfd.Nr. wird ausgelesen
 1123 LPSTR TProtocol::GetLfdNr( LPSTR Value)
 1124 {
 1125         strcpy(Value, CurrentLfdNr);
 1126         return Value;
 1127 };
 1128 
 1129 // der dynamische Speicher für die Bildpunkte einer Skizze wird freigegeben
 1130 void TProtocol::freePic()
 1131 {
 1132         if (firstPOINT == NULL)
 1133                 return;
 1134         lastPOINT= currentPOINT= firstPOINT;
 1135         while (currentPOINT != NULL)
 1136         {
 1137                 lastPOINT= currentPOINT;
 1138                 currentPOINT= currentPOINT->next;
 1139                 _FREEOBJ(lastPOINT);
 1140         }
 1141         currentPOINT= lastPOINT= firstPOINT= NULL;
 1142 };
 1143 
 1144 // die Variable für den aktuellen Bildpunkt einer Skizze wird auf den ersten Bildpunkt gesetzt
 1145 void TProtocol::ResetXY()
 1146 {
 1147         currentPOINT= firstPOINT;
 1148         lastPOINT= NULL;
 1149 };
 1150 
 1151 // liefert einen Bildpunkt einer Skizze zurück
 1152 // ist der letzte Bildpunkt erreicht wird der Rückgabewert der Funktion FALSE
 1153 BOOL TProtocol::GetXY( char &c, int &x, int &y, LPSTR t )
 1154 {
 1155         if (currentPOINT == NULL)
 1156         {
 1157                 if (lastPOINT != NULL)
 1158                         currentPOINT= lastPOINT->next;
 1159                 else
 1160                         currentPOINT= firstPOINT;
 1161                 return FALSE;
 1162         }
 1163         c= currentPOINT->c;
 1164         x= currentPOINT->x;
 1165         y= currentPOINT->y;
 1166         strcpy(t, currentPOINT->t);
 1167         if (currentPOINT->next != NULL)
 1168                 lastPOINT= currentPOINT;
 1169         currentPOINT= currentPOINT->next;
 1170         return TRUE;
 1171 };
 1172 
 1173 // belegt neuen dynamischen Speicher für einen Bildpunkt einer Skizze und
 1174 // initialisiert ihn mit den Wert eines Bildpunktes
 1175 void TProtocol::SetXY( char c, int x, int y, LPCSTR t )
 1176 {
 1177         lastPOINT= currentPOINT;
 1178         currentPOINT= new TMFPoint;
 1179         if (!currentPOINT)
 1180         {
 1181                 MessageBox(GetFocus(), ProtocolLanguage->GetMessage(6), ProtocolLanguage->GetFailure(), MBFAILURE);
 1182                 currentPOINT= lastPOINT;
 1183                 return;
 1184         }
 1185 
 1186         if (firstPOINT == NULL)
 1187                 firstPOINT= currentPOINT;
 1188         currentPOINT->c= c;
 1189         currentPOINT->x= x;
 1190         currentPOINT->y= y;
 1191         strcpy(currentPOINT->t, t);
 1192         currentPOINT->next= NULL;
 1193         currentPOINT->last= lastPOINT;
 1194         if (lastPOINT != NULL)
 1195                 lastPOINT->next= currentPOINT;
 1196 };
 1197 
 1198 // loescht die letzten Bildpunkt einer Skizze
 1199 BOOL TProtocol::UndoXY()
 1200 {
 1201         TMFPoint *tmpPOINT;
 1202 
 1203         if (currentPOINT != NULL)
 1204         {
 1205                 do
 1206                 {
 1207                         if (currentPOINT->c == 'T')
 1208                         {
 1209                                 currentPOINT= currentPOINT->last;
 1210                                 break;
 1211                         }
 1212                         else
 1213                                 currentPOINT= currentPOINT->last;
 1214                 } while ( (currentPOINT != NULL) && (currentPOINT->c != 'M') && (currentPOINT->c != 'T') );
 1215                 if ( (currentPOINT != NULL) && (currentPOINT->c == 'M') )
 1216                         currentPOINT= currentPOINT->last;
 1217                 if (currentPOINT == NULL)
 1218                 {
 1219                         tmpPOINT= firstPOINT;
 1220                         firstPOINT= NULL;
 1221                         lastPOINT= NULL;
 1222                 }
 1223                 else
 1224                 {
 1225                         tmpPOINT= currentPOINT->next;
 1226                         currentPOINT->next= NULL;
 1227                 }
 1228                 while (tmpPOINT != NULL)
 1229                 {
 1230                         lastPOINT= tmpPOINT;
 1231                         tmpPOINT= tmpPOINT->next;
 1232                         _FREEOBJ(lastPOINT);
 1233                 }
 1234                 if (currentPOINT != NULL)
 1235                         lastPOINT= currentPOINT->last;
 1236         }
 1237         if (currentPOINT == NULL)
 1238                 return FALSE;
 1239         return TRUE;
 1240 };
 1241 
 1242 // liest alle Bildpunkte einer Skizze aus einer Datei in den dynamischen Speicher
 1243 // die Bildpunkte werden in einer verketteten Liste gespeichert
 1244 BOOL TProtocol::LoadPicFile( LPCSTR lLfdNr )
 1245 {
 1246         OFSTRUCT of;
 1247         HENHMETAFILE hMeta;
 1248 
 1249         freePic();
 1250         if (OpenFile(GetSkizzeFile(lLfdNr), &of, OF_EXIST) != HFILE_ERROR)
 1251         {
 1252                 hMeta= GetEnhMetaFile(GetSkizzeFile(lLfdNr));
 1253 
 1254                 if (hMeta != NULL)
 1255                 {
 1256                         EnumEnhMetaFile(0,hMeta,EnumMetafileProc,0,0);
 1257                         DeleteEnhMetaFile(hMeta);
 1258                         return TRUE;
 1259                 }
 1260         }
 1261         return FALSE;
 1262 };
 1263 
 1264 // speichert alle Bildpunkte einer Skizze in einer Datei ab
 1265 BOOL TProtocol::SavePicFile( )
 1266 {
 1267         OFSTRUCT of;
 1268         HENHMETAFILE hMeta;
 1269         HDC hdcMeta;
 1270         char lLfdNr[MaxLfdNr];
 1271 
 1272         if ( strlen(GetLfdNr(lLfdNr)) <= 0 )
 1273                 return FALSE;
 1274         if (firstPOINT == NULL)
 1275         {
 1276                 DeletePicFile( lLfdNr );
 1277                 return FALSE;
 1278         }
 1279         if (OpenFile(GetSkizzeFile(lLfdNr), &of, OF_EXIST) != HFILE_ERROR)
 1280         {
 1281                 hMeta= GetEnhMetaFile(GetSkizzeFile(lLfdNr));
 1282                 SetFileAttributes(GetSkizzeBackupFile(lLfdNr), FILE_ATTRIBUTE_ARCHIVE);
 1283 
 1284                 if (hMeta != NULL)
 1285                 {
 1286                         CopyEnhMetaFile(hMeta, GetSkizzeBackupFile(lLfdNr));
 1287                 }
 1288 
 1289                 // Schreibschutzattribut der Protokolldatei wird gesetzt
 1290                 SetFileAttributes(GetSkizzeBackupFile(lLfdNr), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
 1291         }
 1292 
 1293         // Schreibschutzattribut der Protokolldatei wird entfernt
 1294         SetFileAttributes(GetSkizzeFile(lLfdNr), FILE_ATTRIBUTE_ARCHIVE);
 1295 
 1296         DeleteEnhMetaFile(hMeta);
 1297         remove(GetSkizzeFile(lLfdNr));
 1298 
 1299         //if ( (hdcMeta= CreateMetaFile(GetSkizzeFile(lLfdNr))) == NULL )
 1300         if((hdcMeta= CreateEnhMetaFile(NULL,GetSkizzeFile(lLfdNr),NULL,NULL)) == NULL)
 1301                 return FALSE;
 1302 
 1303         currentPOINT= lastPOINT= firstPOINT;
 1304         while (currentPOINT != NULL)
 1305         {
 1306 
 1307                 if (hdcMeta != NULL)
 1308                 {
 1309                         switch (currentPOINT->c)
 1310                         {
 1311                                 case 'M':
 1312                                         MoveToEx(hdcMeta, currentPOINT->x, currentPOINT->y, NULL);
 1313                                         break;
 1314 
 1315                                 case 'L':
 1316                                         LineTo(hdcMeta, currentPOINT->x, currentPOINT->y);
 1317                                         break;
 1318 
 1319                                 case 'T':
 1320                                         TextOut(hdcMeta, currentPOINT->x, currentPOINT->y, currentPOINT->t, strlen(currentPOINT->t));
 1321                                         break;
 1322                         }
 1323                 }
 1324 
 1325                 if (currentPOINT->next != NULL)
 1326                         lastPOINT= currentPOINT;
 1327                 currentPOINT= currentPOINT->next;
 1328         }
 1329         currentPOINT= lastPOINT->next;
 1330 
 1331         CloseEnhMetaFile(hdcMeta);
 1332 
 1333         // Schreibschutzattribut der Protokolldatei wird gesetzt
 1334         SetFileAttributes(GetSkizzeFile(lLfdNr), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
 1335 
 1336         return TRUE;
 1337 };
 1338 
 1339 // löscht eine Bilddatei und gibt den Speicher für die Bildpunkte frei
 1340 void TProtocol::DeletePicFile( LPCSTR lLfdNr )
 1341 {
 1342         freePic();
 1343 
 1344         // Schreibschutzattribut der Protokolldatei wird gesetzt
 1345         SetFileAttributes(GetSkizzeFile(lLfdNr), FILE_ATTRIBUTE_ARCHIVE);
 1346 
 1347         remove(GetSkizzeFile(lLfdNr));
 1348 };
 1349 
 1350 // erzeugt aus einer Lfd.Nr. einen Namen für eine Bilddatei (PATH + Dateiname)
 1351 LPSTR TProtocol::GetSkizzeFile( LPCSTR Value )
 1352 {
 1353         return GetProtocolFile( "SKIZZEN\\", Value, ".WMF" );
 1354 };
 1355 
 1356 // erzeugt aus einer Lfd.Nr. einen Namen für eine Bildbackupdatei (PATH + Dateiname)
 1357 LPSTR TProtocol::GetSkizzeBackupFile( LPCSTR Value )
 1358 {
 1359         return GetProtocolBackupFile( "SKIZZEN\\", Value, ".BAK" );
 1360 };
 1361 
 1362 // erzeugt einen Protokolldateinamen (PATH + Dateiname) aus mehreren Namensbestandteilen
 1363 LPSTR TProtocol::GetProtocolFile( LPCSTR Value1, LPCSTR Value2, LPCSTR Value3 )
 1364 {
 1365         sprintf(szProtocolFile, "%s%s%s%s", GetPDirectory(), Value1, Value2, Value3);
 1366         return szProtocolFile;
 1367 };
 1368 
 1369 // erzeugt einen Protokollbackupdateinamen (PATH + Dateiname) aus mehreren Namensbestandteilen
 1370 LPSTR TProtocol::GetProtocolBackupFile( LPCSTR Value1, LPCSTR Value2, LPCSTR Value3 )
 1371 {
 1372         sprintf(szProtocolFile, "%s%s%s%s", GetPBackupDirectory(), Value1, Value2, Value3);
 1373         return szProtocolFile;
 1374 };
 1375 
 1376 // speichert den aktuellen ProtokollDialog in einer Variable
 1377 void TProtocol::SetCurrentDlg( int idDlg )
 1378 {
 1379         CurrentDlg= idDlg;
 1380 };
 1381 
 1382 // gibt den aktuellen Protokolldialog aus einer Variable zurück
 1383 int TProtocol::GetCurrentDlg( )
 1384 {
 1385         return CurrentDlg;
 1386 };
 1387 
 1388 // liefert den Namen eines Protokollparamerters zurück
 1389 LPSTR TProtocol::GetParameterNamen( int Nr, LPSTR Value )
 1390 {
 1391         strcpy(Value, Parameter[Nr].Name);
 1392         return Value;
 1393 };
 1394 
 1395 // liefert die maximale Länge eines Protokollparamerters zurück
 1396 int TProtocol::GetParameterMaxLen( int Nr )
 1397 {
 1398         return Parameter[Nr].MaxLen;
 1399 };
 1400 
 1401 // liefert den Status eines Protokollparamerters
 1402 BOOL TProtocol::IsParameterReadOnly( int Nr )
 1403 {
 1404         if (Parameter[Nr].Status == ReadOnly)
 1405                 return TRUE;
 1406         return FALSE;
 1407 };
 1408 
 1409 // setzt den Status eines Protokollparamerters
 1410 void TProtocol::SetParameterReadOnly( int Nr )
 1411 {
 1412         Parameter[Nr].Status= ReadOnly;
 1413 };
 1414 
 1415 // setzt den Status eines Protokollparamerters
 1416 void TProtocol::SetParameterReadWrite( int Nr )
 1417 {
 1418         Parameter[Nr].Status= ReadWrite;
 1419 };
 1420 
 1421 // liefert den Status des aktuellen Protokollstrings
 1422 BOOL TProtocol::IsProtocolReadOnly( void )
 1423 {
 1424         if (currentINDEX != NULL)
 1425                 if (currentINDEX->Status > ReadWrite)
 1426                         return TRUE;
 1427         return FALSE;
 1428 };
 1429 
 1430 // setzt den Status des aktuellen Protokollstrings readonly
 1431 void TProtocol::SetProtocolReadOnly( void )
 1432 {
 1433         char buf[MaxString];
 1434 
 1435         SetProtocolValue(Felder - 1, itoa(ReadOnly, buf , 10));
 1436         SaveProtocolString();
 1437 };
 1438 
 1439 // liefert den Status des aktuellen Protokollstrings
 1440 BOOL TProtocol::IsProtocolDelete( void )
 1441 {
 1442         if (currentINDEX != NULL)
 1443                 if (currentINDEX->Status == Deleted)
 1444                         return TRUE;
 1445         return FALSE;
 1446 };
 1447 
 1448 // setzt den Status des aktuellen Protokollstrings delete
 1449 void TProtocol::SetProtocolDelete( void )
 1450 {
 1451         char buf[MaxString];
 1452 
 1453         if (!IsProtocolDelete())
 1454         {
 1455                 SetProtocolValue(Felder - 1, itoa(Deleted, buf , 10));
 1456                 SaveProtocolString();
 1457         }
 1458 };
 1459 
 1460 // ist TRUE wenn eine Messung aufgezeichnet wird
 1461 void TProtocol::SetProtocolOn( BOOL bStatus )
 1462 {
 1463         if (bStatus && Initialize())
 1464         {
 1465                 if (iProtocolOnStatus == Off)
 1466                 {
 1467                         iProtocolOnStatus= On;
 1468                         ClearProtocolString();
 1469                         if ( !((IsLastProtocolString() && !IsProtocolReadOnly()) ||
 1470                                         IsNewProtocolString()) )
 1471                         {
 1472                                 NewProtocolString();
 1473                         }
 1474                 }
 1475         }
 1476         else
 1477                 iProtocolOnStatus= Off;
 1478 };
 1479 
 1480 // liefert den Status ob eine Messung aufgezeichnet wird
 1481 BOOL TProtocol::IsProtocolOn( )
 1482 {
 1483         if (iProtocolOnStatus == Off)
 1484                 return FALSE;
 1485         else
 1486                 return TRUE;
 1487 };
 1488 
 1489 // ändert den Status eine Messung die aufgezeichnet wird
 1490 void TProtocol::ChangeProtocolOnStatus( void )
 1491 {
 1492         if (IsProtocolOn())
 1493                 iProtocolOnStatus++;
 1494 };
 1495 
 1496 // liefert den Status eine Messung die aufgezeichnet wird
 1497 BOOL TProtocol::IsProtocolOnBegin( )
 1498 {
 1499         if (iProtocolOnStatus == Begin)
 1500                 return TRUE;
 1501         else
 1502                 return FALSE;
 1503 };
 1504 
 1505 void TProtocol::SetAllParameterReadWrite( void )
 1506 {
 1507         for (int i= 0; i < Felder; i++)
 1508                 SetParameterReadWrite(i);
 1509 };
 1510 
 1511 void TProtocol::SetAllParameterReadOnly( void )
 1512 {
 1513         for (int i= 0; i < Felder; i++)
 1514                 SetParameterReadOnly(i);
 1515 };
 1516 
 1517 LPCSTR TProtocol::GetProtocolSection( void )
 1518 {
 1519         return szProtocolSection;
 1520 };
 1521 
 1522 BOOL TProtocol::IsProtocolSortDate( void )
 1523 {
 1524         return bSortDate;
 1525 };
 1526 
 1527 void TProtocol::SetProtocolSortDate( BOOL bSet )
 1528 {
 1529         if (bSortDate != bSet)
 1530         {
 1531                 bSortDate= bSet;
 1532                 SortProtocolFile();
 1533                 if (bSortDate)
 1534                         WritePrivateProfileString(szProtocolSection, "SortDate", "1", GetCFile());
 1535                 else
 1536                         WritePrivateProfileString(szProtocolSection, "SortDate", "0", GetCFile());
 1537         }
 1538 };
 1539 
 1540 BOOL TProtocol::IsProtocolNoSortNew( void )
 1541 {
 1542         return bNoSortNew;
 1543 };
 1544 
 1545 void TProtocol::SetProtocolNoSortNew( BOOL bSet )
 1546 {
 1547         bNoSortNew= bSet;
 1548         if (bNoSortNew)
 1549                 WritePrivateProfileString(szProtocolSection, "NoSortNew", "1", GetCFile());
 1550         else
 1551                 WritePrivateProfileString(szProtocolSection, "NoSortNew", "0", GetCFile());
 1552 };
 1553 
 1554 // Sortiert den Protokolldateiindex
 1555 void TProtocol::QuickSortFileIndex(TFileIndex *von, TFileIndex *bis, int laenge)
 1556 {
 1557         TFileIndex *links, *rechts, zwischenspeicher;
 1558         char pivotelement[MaxLfdNr];
 1559         int linc= 0;
 1560         int rinc= 0;
 1561 
 1562         links= von;
 1563         rechts= bis;
 1564         if (bSortDate)
 1565                 strcpy(pivotelement, bis->Date);
 1566         else
 1567                 strcpy(pivotelement, bis->LfdNr);
 1568         do
 1569         {
 1570                 if (bSortDate)
 1571                 {
 1572                         while (strcmp(links->Date, pivotelement) < 0)
 1573                         {
 1574                                 links= links->next;
 1575                                 linc++;
 1576                         }
 1577                         while (strcmp(pivotelement, rechts->Date) < 0)
 1578                         {
 1579                                 rechts= rechts->last;
 1580                                 rinc++;
 1581                         }
 1582                 }
 1583                 else
 1584                 {
 1585                         while (strcmp(links->LfdNr, pivotelement) < 0)
 1586                         {
 1587                                 links= links->next;
 1588                                 linc++;
 1589                         }
 1590                         while (strcmp(pivotelement, rechts->LfdNr) < 0)
 1591                         {
 1592                                 rechts= rechts->last;
 1593                                 rinc++;
 1594                         }
 1595                 }
 1596                 if (linc + rinc < laenge)
 1597                 {
 1598                         strcpy(zwischenspeicher.LfdNr, links->LfdNr);
 1599                         strcpy(zwischenspeicher.Date, links->Date);
 1600                         zwischenspeicher.Pos= links->Pos;
 1601 
 1602                         strcpy(links->LfdNr, rechts->LfdNr);
 1603                         strcpy(links->Date, rechts->Date);
 1604                         links->Pos= rechts->Pos;
 1605 
 1606                         strcpy(rechts->LfdNr, zwischenspeicher.LfdNr);
 1607                         strcpy(rechts->Date, zwischenspeicher.Date);
 1608                         rechts->Pos= zwischenspeicher.Pos;
 1609 
 1610                         links= links->next;
 1611                         linc++;
 1612                         rechts= rechts->last;
 1613                         rinc++;
 1614                 }
 1615         }
 1616         while (linc + rinc < laenge);
 1617 
 1618         if (rinc < laenge - 1)
 1619                 QuickSortFileIndex(von, rechts, laenge - rinc);
 1620         if (linc < laenge - 1)
 1621                 QuickSortFileIndex(links, bis, laenge - linc);
 1622 }
 1623 
 1624 // Sortiert die Protokolldatei, wenn sie nicht sortiert ist (bIsSort),
 1625 // liefert TRUE wenn die Datei sortiert ist oder werden konnte
 1626 // bIsSort wird von LoadProtocolFileIndex() oder AppendProtocolFileIndex() gesetzt
 1627 BOOL TProtocol::SortProtocolFile( void )
 1628 {
 1629         FILE *in, *out;
 1630         char buf[MaxLine];
 1631 
 1632         if ( (!bIsSortLfdNr && !bSortDate) || (!bIsSortDate && bSortDate))
 1633         {
 1634                 QuickSortFileIndex( firstINDEX, lastINDEX, lastINDEX->Nr);
 1635                 if ( (in= fopen(GetPFile(), "rt")) == NULL )
 1636                         return FALSE;
 1637                 if ( (out= fopen(GetProtocolFile(GetPName(), ".tmp"), "wt")) == NULL )
 1638                         return FALSE;
 1639                 if ( fgets( buf, MaxLine, in ) == NULL )
 1640                 {
 1641                         fclose(in);
 1642                         fclose(out);
 1643                         return FALSE;
 1644                 }
 1645                 if ( strncmp(buf, "Lfd", 3) == 0 )
 1646                         fputs(buf, out);
 1647                 currentINDEX= firstINDEX;
 1648                 while (currentINDEX != NULL)
 1649                 {
 1650                         if ( fseek(in, currentINDEX->Pos, SEEK_SET) != 0 )
 1651                         {
 1652                                 fclose(in);
 1653                                 fclose(out);
 1654                                 return FALSE;
 1655                         }
 1656                         if ( fgets( buf, MaxLine, in ) == NULL )
 1657                         {
 1658                                 fclose(in);
 1659                                 fclose(out);
 1660                                 return FALSE;
 1661                         }
 1662                         fputs(buf, out);
 1663                         currentINDEX= currentINDEX->next;
 1664                 }
 1665                 fclose(out);
 1666                 fclose(in);
 1667 
 1668                 // Schreibschutzattribut der Protokolldatei wird entfernt
 1669                 SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE);
 1670 
 1671                 remove(GetPFile());
 1672                 rename(GetProtocolFile(GetPName(), ".tmp"), GetPFile());
 1673 
 1674                 // Schreibschutzattribut der Protokolldatei wird gesetzt
 1675                 SetFileAttributes(GetPFile(), FILE_ATTRIBUTE_ARCHIVE + FILE_ATTRIBUTE_READONLY);
 1676 
 1677                 return LoadProtocolFileIndex();
 1678         }
 1679         return TRUE;
 1680 };
 1681 
 1682 
 1683 // LastLine
 1684 
 1685 
 1686