Annotation of pmsdk/samples/newcard/cfopen.c, revision 1.1

1.1     ! root        1: #include "cardfile.h"
        !             2: 
        !             3: /*********************************************************************/
        !             4: /*  Windows/PM Cardfile Shared Code                                  */
        !             5: /*                                                                   */
        !             6: /*  (c) Copyright Microsoft Corp. 1987,1988 - All Rights Reserved    */
        !             7: /*********************************************************************/
        !             8: 
        !             9: /*********************************************************************/
        !            10: /*    The following shared code was developed from the original      */
        !            11: /* Cardfile application.  This code can be compiled to run under     */
        !            12: /* either the Windows or the PM manager environment.  All            */
        !            13: /* functionality associated with bitmaps or printing has been        */
        !            14: /* deleted.  Some comments refering to these functions may still be  */
        !            15: /* present in the code and should be disregarded. jw.                */
        !            16: /*********************************************************************/
        !            17: 
        !            18: 
        !            19: /*********************************************************************/
        !            20: /* DoMerge -                                                         */
        !            21: /*    Merge cardfile on disk to existing cardfile in memory.         */
        !            22: /*                                                                   */
        !            23: /* Same in Windows and PM.                                           */
        !            24: /*********************************************************************/
        !            25: 
        !            26: void DoMerge()
        !            27:     {
        !            28:     char *pchBuf;
        !            29:     OFSTRUCT ofStruct;
        !            30: 
        !            31:     if(!fReadOnly && (pchBuf = (char *)PutUpDB(DTMERGE)))
        !            32:         {
        !            33:         /* check the name that the user specifies */
        !            34:         if (OpenFile((LPSTR)pchBuf, (LPOFSTRUCT)&ofStruct, OF_PARSE))
        !            35:             {
        !            36:             /* not a valid file name */
        !            37:             CardfileOkError(IDS_EINVALIDFILE);
        !            38:             LocalFree((HANDLE)pchBuf);
        !            39:             return;
        !            40:             }
        !            41: 
        !            42:         /* save current data */
        !            43:         if (CardPhone == IDM_PHONEBOOK || SaveCurrentCard(iFirstCard))
        !            44:             {
        !            45:             /* merge the files */
        !            46:             if(MergeCardFile(pchBuf))
        !            47:                 {
        !            48:                 /* successful, reset information */
        !            49:                 iTopCard = iFirstCard = 0;
        !            50:                 SetScrRangeAndPos();
        !            51:                 if (CardPhone == IDM_CARDFILE)
        !            52:                     SetCurCard(iFirstCard);
        !            53:                 InvalidateRect(hCardfileWnd, (LPRECT)NULL, TRUE);
        !            54:                 }
        !            55:             }
        !            56:         LocalFree((HANDLE)pchBuf);
        !            57:         }
        !            58:     }
        !            59: 
        !            60: 
        !            61: /*********************************************************************/
        !            62: /* DoOpen -                                                          */
        !            63: /*    Open specified cardfile, checking file name for validity.      */
        !            64: /*                                                                   */
        !            65: /* Same in Windows and PM.                                           */
        !            66: /*********************************************************************/
        !            67: 
        !            68: BOOL FAR DoOpen(pchBuf)
        !            69: char *pchBuf;
        !            70:     {
        !            71:     int result = FALSE;
        !            72:     OFSTRUCT ofStruct;
        !            73: 
        !            74:     /* check the filename the user specified */
        !            75:     if (OpenFile((LPSTR)pchBuf, (LPOFSTRUCT)&ofStruct, OF_PARSE))
        !            76:         {
        !            77:         /* not a valid filename */
        !            78:         CardfileOkError(IDS_EINVALIDFILE);
        !            79:         LocalFree((HANDLE)pchBuf);
        !            80:         return(0);
        !            81:         }
        !            82: 
        !            83:     /* read the file */
        !            84:     if(ReadCardFile(pchBuf))
        !            85:         {
        !            86:         /* successful, update caption and tempfile */
        !            87:         SetCaption();
        !            88:         Fdelete(TmpFile);
        !            89:         MakeTmpFile();
        !            90: 
        !            91:         /* reset scroll bars */
        !            92:         iTopCard = iFirstCard = 0;
        !            93:         SetScrRangeAndPos();
        !            94: 
        !            95:         /* get card's data */
        !            96:         if (CardPhone == IDM_CARDFILE);
        !            97:             SetCurCard(iFirstCard);
        !            98: 
        !            99:         CurCardHead.flags = 0;
        !           100:         InvalidateRect(hCardfileWnd, (LPRECT)NULL, TRUE);
        !           101:         result = TRUE;
        !           102:         }
        !           103:     return(result);
        !           104:     }
        !           105: 
        !           106: 
        !           107: /*********************************************************************/
        !           108: /* MaybeSaveFile -                                                   */
        !           109: /*    Prompt user to see if changes should be saved or if current    */
        !           110: /* operation should be cancelled.                                    */
        !           111: /*                                                                   */
        !           112: /* Same in Windows and PM.                                           */
        !           113: /*********************************************************************/
        !           114: 
        !           115: BOOL MaybeSaveFile()
        !           116:     {
        !           117:     char *pchFile;
        !           118:     int result;
        !           119:     char *pch;
        !           120:     OFSTRUCT ofStruct;
        !           121: 
        !           122: /* put up a message box that says "Do you wish to save your edits?" */
        !           123: /* if so, save 'em */
        !           124: /* if returns FALSE, means it couldn't save, and whatever is happening */
        !           125: /* should not continue */
        !           126: 
        !           127:     /* If file needs to be saved */
        !           128:     if (fFileDirty || (CurCardHead.flags & FDIRTY) || 
        !           129:            SendMessage(hCardWnd, EM_GETMODIFY, 0, 0L))
        !           130:         {
        !           131:         if (CurIFile[0])
        !           132:             {
        !           133:             /* get to the end of the file */
        !           134:             for (pch = CurIFile ; *pch; ++pch)
        !           135:                 ;
        !           136:             /* scan backwards to beginning of filename */
        !           137:             while (pch > CurIFile && *pch != '\\')
        !           138:                 pch = (PSTR)AnsiPrev(CurIFile, pch);
        !           139:             if (*pch == '\\')
        !           140:                 pch++;
        !           141: 
        !           142:             AnsiUpper((LPSTR)pch);
        !           143:             }
        !           144:         else
        !           145:             pch = rgchUntitled;
        !           146: 
        !           147:         /* put up the message, merging in filename */
        !           148:         result = MyMessageBox(IDS_OKTOSAVE, pch, 
        !           149:                               MB_YESNOCANCEL | MB_ICONQUESTION);
        !           150: 
        !           151:         /* if user wants to save */
        !           152:         if (result == IDYES)
        !           153:             {
        !           154:             /* save the current information */
        !           155:             if (SaveCurrentCard(iFirstCard))
        !           156:                 {
        !           157:                 /* if unnamed, need to get a filename */
        !           158:                 if (!CurIFile[0])
        !           159:                     {
        !           160:                     if (GetNewFileName(&ofStruct))
        !           161:                         pchFile = ofStruct.szPathName;
        !           162:                     else
        !           163:                         {
        !           164:                         SetCurCard(iFirstCard);
        !           165:                         return(FALSE);      /* cancelled */
        !           166:                         }
        !           167:                     }
        !           168:                 else
        !           169:                     pchFile = CurIFile;
        !           170: 
        !           171:                 /* save file, if can't save don't continue */
        !           172:                 if (!WriteCardFile(pchFile))
        !           173:                     return(FALSE);
        !           174:                 }
        !           175:             else
        !           176:                 return(FALSE);
        !           177:             }
        !           178:         else if (result == IDCANCEL)
        !           179:             return(FALSE);
        !           180:         }
        !           181: 
        !           182:     return(TRUE);
        !           183:     }
        !           184: 
        !           185: 
        !           186: /*********************************************************************/
        !           187: /* GetNewFileName -
        !           188: /*    Asks user for a filename.
        !           189: /*
        !           190: /* Same in Windows and PM.
        !           191: /*********************************************************************/
        !           192: 
        !           193: BOOL GetNewFileName(pOFStruct)
        !           194: POFSTRUCT pOFStruct;
        !           195:     {
        !           196:     PSTR pchBuf;
        !           197:     BOOL fDone;
        !           198:     int fh;
        !           199: 
        !           200:     fDone = FALSE;
        !           201:     while (!fDone)
        !           202:         {
        !           203:         if(pchBuf = PutUpDB(DTSAVE))
        !           204:             {
        !           205:             /* check for valid filename */
        !           206:             if (OpenFile((LPSTR)pchBuf, (LPOFSTRUCT)pOFStruct, OF_PARSE))
        !           207:                 {
        !           208:                 CardfileOkError(IDS_EINVALIDFILE);
        !           209:                 }
        !           210: 
        !           211:             /* check to see if file already exists */
        !           212:             else if ((fh = MyOpen((LPSTR)pchBuf, READ)) > -1)
        !           213:                 {
        !           214:                 /* it does, close it and as if user wants to overwrite */
        !           215:                 MyClose(fh);
        !           216:                 AnsiUpper((LPSTR)pchBuf);
        !           217:                 if (MyMessageBox(IDS_EFILEEXISTS,
        !           218:                                  pchBuf,
        !           219:                                  MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)
        !           220:                         == IDYES)
        !           221:                     {
        !           222:                     /* does, done */
        !           223:                     fDone = TRUE;
        !           224:                     }
        !           225:                 }
        !           226:             else
        !           227:                 /* file does not exist, done */
        !           228:                 fDone = TRUE;
        !           229: 
        !           230:             /* free up the response buffer */
        !           231:             LocalFree((HANDLE)pchBuf);
        !           232:             }
        !           233:         else
        !           234:             /* cancelled save as dialog box */
        !           235:             return(FALSE);
        !           236:         }
        !           237:     return(TRUE);
        !           238:     }
        !           239: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.