微软Office的源代码样式规范(上) —— 绝对机密文档!!!

Office Source Code Style Guide
Dave Parker, 6/30/95

Abstract
This document outlines a general style guide for C and C++ source code in Office Development.  The main purpose here is to list features of C++ which we will use and which we will avoid, along with the basic rationale for doing so.  There are also standards for basic coding issues for the sake of consistency within the code and robust constructs.  This is not a complete list of C/C++ language features with commentary.  Rather, it mentions only the issues we consider important.  Knowledge of C++ is assumed.
Contents
1. GENERAL GOALS 3
2. CLASSES 3
2.1 CLASS VS. STRUCT 4
2.2 PUBLIC, PRIVATE, AND PROTECTED MEMBERS 4
2.3 DATA MEMBERS 4
2.4 VIRTUAL FUNCTIONS 5
2.5 CONSTRUCTORS 5
2.6 DESTRUCTORS 6
2.7 NEW AND DELETE 7
2.8 OPERATORS 7
2.9 INHERITANCE 8
2.9.1 Inheritance of Interface vs. Implementation 8
2.9.2 Inheritance vs. Containment 10
2.9.3 Multiple Inheritance 11
3. OTHER C++ FEATURES 11
3.1 CONSTANTS AND ENUMERATIONS 12
3.2 REFERENCES 12
3.3 CONST PARAMETERS AND FUNCTIONS 13
3.4 DEFAULT ARGUMENTS 13
3.5 FUNCTION OVERLOADING 14
3.6 OPERATOR OVERLOADING 14
4. COMMON C/C++ ISSUES 14
4.1 #IFDEFS 14
4.2 GLOBAL VARIABLES 15
4.3 MACROS AND INLINE FUNCTIONS 16
4.4 OPTIMIZATION 16
4.5 WARNINGS 17
4.6 PRIVATE DATA AND FUNCTIONS 17
4.7 TYPEDEFS 17
4.8 BASIC DATA TYPES 17
4.9 POINTERS 18
4.10 SWITCH STATEMENTS 19
4.11 ASSERTS 19
4.12 ERRORS AND EXCEPTIONS 19
5. FORMATTING CONVENTIONS 20
5.1 NAMING CONVENTIONS 20
5.2 FUNCTION PROTOTYPES 21
5.3 VARIABLE DECLARATIONS 22
5.4 CLASS DECLARATIONS 22
5.5 COMMENTS 23
5.5.1 File Headers and Section Separators 23
5.5.2 Function Headers 24
5.5.3 In-Code Comments 25
5.5.4 Attention Markers 25
5.6 MISC. FORMATTING CONVENTIONS 26
5.7 SOURCE FILE ORGANIZATION 27
5.7.1 Public Interface Files 27
5.7.2 Private Interface Files 28
5.7.3 Implementation Files 28
5.7.4 Base Filenames 29
6. INTERFACES TO DLLS 29
6.1 C FUNCTIONS AND GLOBAL VARIABLES 29
6.2 COMMON C/C++ PUBLIC HEADER FILES 29
6.3 LIGHTWEIGHT COM OBJECTS AND ISIMPLEUNKNOWN 30
7. APPENDIX A: BASIC HUNGARIAN REFERENCE 33
7.1 MAKING HUNGARIAN NAMES 33
7.2 STANDARD BASE TAGS 33
7.3 STANDARD PREFIXES 34
7.4 STANDARD QUALIFIERS 35

 1. General Goals
C++ is a complex language that provides many ways to do things, and going 搘hole hog” on all of its features can lead to confusion, inefficiency, or maintenance problems.  All Office developers need to become experts on the features we will use, and avoid the others in order to form solid conventions within the group that we are all comfortable with.  Our use of C++ features will be fairly conservative.  We抎 much rather err on the side of just dealing with C, which we抮e all used to, then screwing up our app with a new concept that not all of us are used to.
Underlying the choice of all of the style decisions are a few basic goals, as listed below.  When in doubt about a particular issue, always think about the spirit of these goals.  Sometimes these goals will conflict, of course, and in these cases we try to either prioritize the tradeoffs or use experience (either our own or from other groups that have used C++ extensively).
1. Simplicity.  When in doubt, keep it simple.  Bugs are related mostly to complexity, not code.
2. Clarity.  The code should do what it looks like it抯 doing.  Other people need to be able to understand your code.
3. Efficiency.  Speed and size are important.  Using C++ does not imply big and slow.  There are plenty of perfectly reasonable ways to make things as fast or faster than the normal C way.  Speed and size often trade off, and most people probably err on the side of choosing speed too often.  Remember that 20% of the code is responsible for 80% of the time.  In most cases, we抮e more concerned about fitting comfortably in less RAM.
4. Appropriateness.  Use the language construct that is appropriate for the abstraction or operation you are trying to do.  Do not abuse the language.  Don抰 use a construct just because it happens to work.  Definitely don抰 use a strange construct to amaze and confuse your friends to try to show how smart you are.
5. Natural transition from C to C++.  We all used to be C programmers.  Others that look at our code are still C programmers (e.g. Word and Excel).  When possible, avoid C++ constructs where a C programmer抯 instinct causes a wrong assumption.
6. Catch Errors Early.  Having the compiler catch an error is ideal.  Having debug code (e.g. Asserts) catch it is the next best thing, etc.  Declare things in such as way as to give the compiler the best chance at catching errors.
7. Fast builds.  Total generality and modularity can cause lots of inter-dependencies between files, which can have a dramatic impact on build times.  This is a constant time sink for everyone.  It is often worth rearranging things a little to make incremental builds faster.
8. Consistency.  The whole point of having a style guide is that programmers are never totally autonomous, even when the group has strong code ownership.  Other people need to read and understand your code.  Everyone has to give a little to have a consistent style guide, but everyone gains it back when they read or debug other people抯 code.
2. Classes
C++ classes are a nice way to encapsulate code and data into a single unit, which provides a good paradigm for object-oriented implementations as well other features such as flexible access control, convenient and type-safe polymorphism, and the possibility of code reuse via inheritance.
At the most general, classes are an extension to the built-in typing of C which allows you to define your own types along with the operations on that type.  Taken to the extreme, every piece of data in a program could be an instance of a class.  However, we will not go nearly this far in Office.  We will use classes when there is a good reason to, such as the concept being implemented is inherently object-oriented or polymorphism is required.  It has been the experience of many people that programs that use classes for everything evolve into systems that are complex and inefficient.  Although this may not be the fault of any particular class, complex class hierarchies can lead to needless complexity, and overly abstracted concepts can easily lead to inefficiency.
In general, we will avoid allocating classes on the stack and passing classes by value, because this is where the use of constructors and destructors gets you into the most trouble.  Most classes should be allocated via new, freed by delete, and passed by pointer.  In addition, we will never declare a global variable which is an instance of a class that has a constructor, because this causes a bunch of C runtime stuff to get linked in and stuff to happen at boot time to construct the thing, which is a big performance hit.  Using only heap-allocated classes implies we抣l probably use classes only for relatively complex objects that you would normally have in the heap anyway, not simple things like basic data types.  Beyond this, it is a judgment call when to use a class.  Use one if there is a good reason, but not if a more straightforward solution is just as good.
Summary:
 Use classes to encapsulate the implementation of an object-oriented concept.
 Use classes to implement polymorphism.
 Avoid allocating class instances on the stack and passing them by value.  Use new and delete, and pass them by pointer.  This implies not using classes for simple data types.
 Never declare a global instance of a class that has a constructor.
 Not everything is as class.  Use them only when you gain something.
2.1 Class vs. Struct
In C++, a struct can also have member functions and operators and everything else that a class can have.  In fact, the only difference between a class and a struct is that all members default to public access in a struct but private access in a class.  However, we will not use this as the deciding point between using a class vs. a struct.  To match the normal intuition, we will use a class if and only if there are member functions included.
Summary:
 Use a class instead of a struct if and only if there are member functions.
2.2 Public, Private, and Protected members
As stated above, structs default to public access and classes default to private access.  However, we will depend on the default only in the case of structs (where we leave all the data implicitly public).  For a class, we will declare all members (both data and code) explicitly as public, protected, or private, and group them into sections in that order.  For example:
class Foo
 {
public:
 Foo();
 ~Foo();
 void Hey(int I);
 void Ack();
protected:
 int m_iValue;
private:
 int m_iStuff;
 void LocalHelperSub();
 };

Summary:
 Declare all class members explicitly as public, protected, or private, in groups in that order.
2.3 Data Members
Data members should use the naming convention m_name where name is a normal Hungarian local variable name.  This makes member function implementations easier to read (no confusion about member vs. local data), and allows the use of the same Hungarian name for, e.g., parameters and members.  See the example below.
Data members should normally not be declared public because this usually defeats the purpose

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在线阅读 一、 功能所需工具 下载工具 OpenOffice http://zh.openoffice.org/new/zh_cn/downloads.html JodConverter http://dldx.csdn.net/fd.php?i=992314146801277&s=08dbee95a6e2dda1a95aa8cbf4df197b Swftools(pdf2swf) http://dldx.csdn.net/fd.php?i=389133735472350&s=2f7430ad3c00cca78ada8b4671a50b24 FlexPaper http://flexpaper.googlecode.com/files/FlexPaper_1.4.5_flash.zip 二、 搭建所需环境及实现 第一步:安装OpenOffice。从上述下载地址得到可执行安装文件,直接双击执行,安装过程较为人性化,只需选择下一步即可。此处注意下安装路径,文件转换之前需在Windows命令行窗口打开安装根目录,然后执行开启服务命令。 第二步:解压JodConverter。解压目录结构如下图: 打开lib文件夹, 将其中的jar包复制到Web工程的WebRoot/WEB-INF/lib下。 第三步:安装Swftools。从下载的压缩包中解压得到可执行安装文件,直接双击执行。该转换工具用来将pdf文件转换成swf文件。改工具既可以安装使用实现文件转换,也拷贝安装后Program Files下的Swftools文件夹放到工程中,以绿色软件方式来使用。转换命令将在FileConverterUtil.java中特别指明。 第四步:使用Flexpaper。Flexpaper就是一个播放swf文件的播放器。解压后目录如下: 其中Paper.swf、所有的txt文件、php文件夹和example文件夹都可以删掉。清理完之后,新建readFile.jsp(jsp页面代码在后面附加),然后将flexpaper文件夹拷贝到WebRoot下即可。 FileConverterUtil.java代码如下: package com.sdjt.util; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /** * <p>Title: </p> * <p>desc: 档案Action类 * <p>Copyright: Copyright(c)shundesoft 2011</p> * <p>company:济南舜德竟天软件有限公司</p> * @author 温中伟 * @date 2011-10-14 * @version 1.0 * @since */ public class FileConverterUtil{ /** * 实现文件格式转换 * @param sourceFilePath //源文件路径 * @param fullFileName //源文件名称 * @param converterFlag //源文件转换标志 * @throws Exception */ public String convertFile(String sourceFilePath, String fullFileName, String swfToolsPath, String converterFlag) throws Exception{ File sourceFile; //转换源文件 File pdfFile; //PDF媒介文件 File swfFile; //SWF目标文件 File createPath; //创建文件存放目录 Runtime rt; //转换命令执行类 String converFileName = ""; //转换之后的SWF文件名称 String middleFilePath = sourceFilePath.substring(0, sourceFilePath.length()-1); String filePath = (middleFilePath.substring(0, middleFilePath.lastIndexOf("\\"))).substring(0, (middleFilePath.substring(0, middleFilePath.lastIndexOf("\\"))).lastIndexOf("\\")); String fileName = PinYinUtil.getPinYinFirstOrAllLetter(fullFileName.substring(0, fullFileName.lastIndexOf(".")), false)[0]; String fileType = fullFileName.substring(fullFileName.lastIndexOf(".")+1); String folderName = middleFilePath.substring(middleFilePath.lastIndexOf("\\")+1); if(converterFlag.equals("1")){ converFileName = folderName+"/"+fileName+".swf"; }else{ if(fileType.equals("pdf")){ //PDF格式文件处理方式 rt = Runtime.getRuntime(); sourceFile = new File(sourceFilePath+fullFileName); //创建SWF文件存放目录 createPath = new File(filePath+"\\swfFiles\\"+folderName); if(!createPath.isDirectory()){ createPath.mkdir(); } swfFile = new File(filePath+"/swfFiles/"+folderName+"/"+fileName+".swf"); Process p = rt.exec(swfToolsPath+"/pdf2swf.exe " + sourceFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); //缓冲区读入内容清理 clearCache(p.getInputStream(), p.getErrorStream()); converFileName = folderName+"/"+fileName+".swf"; }else{ //非PDF格式文件处理方式 if(isLegal(fileType.toUpperCase())){ sourceFile = new File(sourceFilePath+fullFileName); pdfFile = new File(filePath+"/swfFiles/"+folderName+"/"+fileName+".pdf"); swfFile = new File(filePath+"/swfFiles/"+folderName+"/"+fileName+".swf"); //获取连接对象 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); //取得连接 connection.connect(); //创建文件格式转换对象 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); //实现文件格式转换 converter.convert(sourceFile, pdfFile); //生成已转换的PDF文件 pdfFile.createNewFile(); //释放连接 connection.disconnect(); rt = Runtime.getRuntime(); //执行PDF文件转换成SWF文件命令 Process p = rt.exec(swfToolsPath+"/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); //缓冲区读入内容清理 clearCache(p.getInputStream(), p.getErrorStream()); //删除中转PDF文件 if(pdfFile.exists()){ pdfFile.delete(); } converFileName = folderName+"/"+fileName+".swf"; } } } return converFileName; } /** * 清理缓冲区 * @param isi * @param ise */ public void clearCache(InputStream isi, InputStream ise){ try { final InputStream is1 = isi; //启用单独线程清空InputStream缓冲区 new Thread(new Runnable() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(is1)); try { while(br.readLine() != null) ; } catch (IOException e) { e.printStackTrace(); } } }).start(); //读入ErrorStream缓冲 BufferedReader br = new BufferedReader(new InputStreamReader(ise)); //保存缓冲输出结果 StringBuilder buf = new StringBuilder(); String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } //循环等待进程结束 while(line != null) buf.append(line); is1.close(); ise.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 判断所转换文件类型是否合法 * @param getFileType //文件格式 * @param fileLegalFlag //是否合法标志 false:非法 true:合法 */ public boolean isLegal(String getFileType){ boolean fileLegalFlag = false; if(getFileType.equals("TXT")){ fileLegalFlag = true; }else if(getFileType.equals("DOC")||getFileType.equals("DOCX")){ fileLegalFlag = true; }else if(getFileType.equals("PPT")||getFileType.equals("PPTX")){ fileLegalFlag = true; }else if(getFileType.equals("XLS")||getFileType.equals("XLSX")){ fileLegalFlag = true; } return fileLegalFlag; } } readFile.jsp页面代码如下: <%@ page language="java" import="java.lang.String" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String getFilePath = request.getParameter("recFileName"); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="en" xml:lang="en"> <head> <title>在线阅读</title> <style type="text/css" media="screen"> html, body { height:100%; } body { margin:0; padding:0; overflow:auto; } #flashContent { display:none; } </style> [removed][removed] </head> <body> <div <a id="viewerPlaceHolder" [removed] var fp = new FlexPaperViewer( 'FlexPaperViewer', 'viewerPlaceHolder', { config : { SwfFile : escape('../smsdocument/swfFiles/<%=getFilePath%>'), Scale : 0.6, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, PrintEnabled : false, FullScreenAsMaxWindow : false, ProgressiveLoading : true, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'zh_CN' }}); [removed] </div> </body> </html> Struts配置文件: OpenOffice服务启动命令: cd C:\Program Files\OpenOffice.org 3\program soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard 需注意的问题 转换TXT时内容中文乱码问题 反编译jodconverter-2.2.2.jar,反编译好的已经放在在线阅读文件夹下。Jodconverter-2.2.1.jar不出现TXT乱码问题,但是不支持office2007格式的文件转换。 Flexpaper不支持中文路径 中文名称的文件转换成了汉语拼音.swf 参考资料 http://topic.csdn.net/u/20110712/18/4daf5746-e64e-434d-aeb0-77b05f6c9903.html http://www.cnblogs.com/qinpeifeng107/archive/2011/08/29/2158879.html http://blog.csdn.net/liuyuhua0066/article/details/6603493 http://blog.csdn.net/lyq123333321/article/details/6546104 http://www.cnblogs.com/compass/articles/2046311.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

silver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值