📄 print.c
字号:
#include <stdio.h>#include "catalog.h"#include "utility.h"#define MAX(a,b) ((a) > (b) ? (a) : (b))#define MIN(a,b) ((a) < (b) ? (a) : (b))//// Compute widths of columns in attribute array.//const Status UT_computeWidth(const int attrCnt, const AttrDesc attrs[], int *&attrWidth){ attrWidth = new int [attrCnt]; if (!attrWidth) return INSUFMEM; for(int i = 0; i < attrCnt; i++) { int namelen = strlen(attrs[i].attrName); switch(attrs[i].attrType) { case INTEGER: case FLOAT: attrWidth[i] = MIN(MAX(namelen, 5), 7); break; case STRING: attrWidth[i] = MIN(MAX(namelen, attrs[i].attrLen), 20); break; } } return OK;}//// Prints values of attributes stored in buffer pointed to// by recPtr. The desired width of columns is in attrWidth.//void UT_printRec(const int attrCnt, const AttrDesc attrs[], int *attrWidth, const Record & rec){ for(int i = 0; i < attrCnt; i++) { char *attr = (char *)rec.data + attrs[i].attrOffset; switch(attrs[i].attrType) { case INTEGER: int tempi; memcpy(&tempi, attr, sizeof(int)); printf("%-*d ", attrWidth[i], tempi); break; case FLOAT: float tempf; memcpy(&tempf, attr, sizeof(float)); printf("%-*.2f ", attrWidth[i], tempf); break; default: printf("%-*.*s ", attrWidth[i], attrWidth[i], attr); break; } } printf("\n");}//// Prints the contents of the specified relation.//// Returns:// OK on success// an error code otherwise//const Status UT_Print(string relation){ Status status; RelDesc rd; AttrDesc *attrs; AttrDesc ads ; int attrCnt; Record rec ; RID rid ; HeapFileScan *hfs ; int *attrWidth ; string myrel1 ; if (relation.empty()) relation = RELCATNAME ; if ((status = relCat->getInfo( relation, rd)) != OK) return status ; myrel1.append(rd.relName) ; hfs = new HeapFileScan( myrel1, status) ; if (status != OK) return status ; if ((status = attrCat->getRelInfo( myrel1, attrCnt, attrs)) != OK){ delete hfs ; return status ; } if ((status = UT_computeWidth( attrCnt, attrs, attrWidth)) != OK){ delete hfs ; delete [] attrWidth ; return status ; } int recNum = 0 ; cout<<"\n" ; for(int w = 0; w < attrCnt; w++) printf("%-*s ", attrWidth[w], attrs[w].attrName) ; cout<<"\n" ; for(int p = 0; p < attrCnt; p++){ for (int j = 0; j < attrWidth[p]; j++) cout<<"-" ; cout<<" " ; } cout<<"\n" ; hfs->startScan( 0, 0, STRING, NULL, EQ ) ; while(hfs->scanNext(rid) != FILEEOF){ hfs->getRecord(rec) ; UT_printRec( attrCnt, attrs, attrWidth, rec) ; recNum++ ; } cout<<"\n "<<recNum<<" record (s) selected!\n" ; delete [] attrs ; delete hfs ; delete [] attrWidth ; return OK ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -