示例#1
0
BOOL TranslateFile (LPTSTR psz)
{
   BOOL rc = TRUE;

   // First open the file and read it into memory.
   //
   HANDLE hFile;
   if ((hFile = CreateFile (psz, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
      {
      printf ("failed to open %s; error %lu\n", psz, GetLastError());
      return FALSE;
      }

   DWORD cbSource;
   if ((cbSource = GetFileSize (hFile, NULL)) != 0)
      {
      LPTSTR abSource = (LPTSTR)malloc(cbSource + 4);
	  memset(abSource, 0x00, cbSource + 4);

      DWORD dwRead;
      if (!ReadFile (hFile, abSource, cbSource, &dwRead, NULL) || cbSource != dwRead)
         {
         printf ("failed to read %s; error %lu\n", psz, GetLastError());
         rc = FALSE;
         }
      else
         {
         DWORD cbTarget = cbSource * 4;
         LPSTR abTarget = (LPSTR)malloc(cbTarget);
         memset (abTarget, 0x00, cbTarget);

         BOOL fDefault = FALSE;
         WideCharToMultiByte (g::CodePage, 0, (LPCWSTR)abSource, -1, abTarget, cbTarget, TEXT(" "), &fDefault);

         rc = FormatFile (psz, abTarget);

         free(abTarget);
         }

      free(abSource);
      }

   CloseHandle (hFile);
   return rc;
}
bool ClassGenerateDialog::GenerateClass(Table* pTab, const wxString& path)
{
    wxString hFileName = wxT("");
    wxString cFileName = wxT("");

    wxArrayString arrFileNames = wxStringTokenize( m_mapTemplateFiles[ m_choiceTemplates->GetStringSelection() ],
                                 wxT(";"),
                                 wxTOKEN_RET_EMPTY );

    if (pTab->IsView()) {
        hFileName = arrFileNames[2];
        cFileName = arrFileNames[3];

    } else {
        hFileName = arrFileNames[0];
        cFileName = arrFileNames[1];
    }

    wxTextFile htmpFile(m_mgr->GetInstallDirectory() + wxT("/templates/databaselayer/") + hFileName);
    wxTextFile ctmpFile(m_mgr->GetInstallDirectory() + wxT("/templates/databaselayer/") + cFileName);

    if (!htmpFile.Open() || !ctmpFile.Open()) return false;

    classTableName = pTab->GetName();
    classItemName = m_txPrefix->GetValue() + pTab->GetName() + m_txPostfix->GetValue();
    classItemDef = wxT("__") + classItemName.Upper() + wxT("_H__");
    classColName = m_txPrefix->GetValue() + pTab->GetName() + wxT("Collection")+ m_txPostfix->GetValue();
    classUtilName = m_txPrefix->GetValue() + pTab->GetName() + wxT("Utils")+ m_txPostfix->GetValue();

    wxString hFile;
    wxFileName fnHeaderFileName(path + wxT("/") + classItemName + wxT(".h"));
    
    wxString cFile;
    wxFileName fnCppFileName(path + wxT("/") + classItemName + wxT(".cpp"));
    
    bool suc = GenerateFile(pTab,htmpFile, hFile, classItemName, classItemDef,classColName,classTableName, classUtilName);
    suc &= GenerateFile(pTab, ctmpFile, cFile, classItemName, classItemDef,classColName,classTableName, classUtilName);

    htmpFile.Close();
    ctmpFile.Close();

    // format output files
    FormatFile( hFile );
    FormatFile( cFile );

    ::WriteFileWithBackup(fnCppFileName.GetFullPath(), cFile, false);
    ::WriteFileWithBackup(fnHeaderFileName.GetFullPath(), hFile, false);

    // add files to the workspace
    wxArrayString arrString;
    arrString.Add( path + wxT("/") + classItemName + wxT(".h"));
    arrString.Add( path + wxT("/") + classItemName + wxT(".cpp"));

    m_mgr->AddFilesToVirtualFolder( m_txVirtualDir->GetValue(), arrString );

    // retag workspace
    wxCommandEvent evt( wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace") );
    EventNotifier::Get()->TopFrame()->GetEventHandler()->AddPendingEvent( evt );

    return suc;
}