From: acevest Date: Tue, 23 Jun 2015 09:39:17 +0000 (+0800) Subject: ... X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=4cf337e4061067d9230147d847e73367049d04b6;p=acecode.git ... --- diff --git a/learn/swift/.DS_Store b/.DS_Store similarity index 82% rename from learn/swift/.DS_Store rename to .DS_Store index 0af6509..2c3105d 100644 Binary files a/learn/swift/.DS_Store and b/.DS_Store differ diff --git a/learn/.DS_Store b/learn/.DS_Store new file mode 100644 index 0000000..8cc4f85 Binary files /dev/null and b/learn/.DS_Store differ diff --git a/learn/AppleSwift/.DS_Store b/learn/AppleSwift/.DS_Store new file mode 100644 index 0000000..4bc2632 Binary files /dev/null and b/learn/AppleSwift/.DS_Store differ diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj b/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj new file mode 100644 index 0000000..79dc39c --- /dev/null +++ b/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj @@ -0,0 +1,243 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 5063B2A61B390AAF009C5821 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5063B2A51B390AAF009C5821 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 5063B2A01B390AAF009C5821 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 5063B2A21B390AAF009C5821 /* AppleSwift */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = AppleSwift; sourceTree = BUILT_PRODUCTS_DIR; }; + 5063B2A51B390AAF009C5821 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5063B29F1B390AAF009C5821 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5063B2991B390AAF009C5821 = { + isa = PBXGroup; + children = ( + 5063B2A41B390AAF009C5821 /* AppleSwift */, + 5063B2A31B390AAF009C5821 /* Products */, + ); + sourceTree = ""; + }; + 5063B2A31B390AAF009C5821 /* Products */ = { + isa = PBXGroup; + children = ( + 5063B2A21B390AAF009C5821 /* AppleSwift */, + ); + name = Products; + sourceTree = ""; + }; + 5063B2A41B390AAF009C5821 /* AppleSwift */ = { + isa = PBXGroup; + children = ( + 5063B2A51B390AAF009C5821 /* main.swift */, + ); + path = AppleSwift; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 5063B2A11B390AAF009C5821 /* AppleSwift */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5063B2A91B390AAF009C5821 /* Build configuration list for PBXNativeTarget "AppleSwift" */; + buildPhases = ( + 5063B29E1B390AAF009C5821 /* Sources */, + 5063B29F1B390AAF009C5821 /* Frameworks */, + 5063B2A01B390AAF009C5821 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = AppleSwift; + productName = AppleSwift; + productReference = 5063B2A21B390AAF009C5821 /* AppleSwift */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5063B29A1B390AAF009C5821 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0630; + ORGANIZATIONNAME = Ace; + TargetAttributes = { + 5063B2A11B390AAF009C5821 = { + CreatedOnToolsVersion = 6.3.2; + }; + }; + }; + buildConfigurationList = 5063B29D1B390AAF009C5821 /* Build configuration list for PBXProject "AppleSwift" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 5063B2991B390AAF009C5821; + productRefGroup = 5063B2A31B390AAF009C5821 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5063B2A11B390AAF009C5821 /* AppleSwift */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 5063B29E1B390AAF009C5821 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5063B2A61B390AAF009C5821 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5063B2A71B390AAF009C5821 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 5063B2A81B390AAF009C5821 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 5063B2AA1B390AAF009C5821 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 5063B2AB1B390AAF009C5821 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5063B29D1B390AAF009C5821 /* Build configuration list for PBXProject "AppleSwift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5063B2A71B390AAF009C5821 /* Debug */, + 5063B2A81B390AAF009C5821 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5063B2A91B390AAF009C5821 /* Build configuration list for PBXNativeTarget "AppleSwift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5063B2AA1B390AAF009C5821 /* Debug */, + 5063B2AB1B390AAF009C5821 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 5063B29A1B390AAF009C5821 /* Project object */; +} diff --git a/learn/swift/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/contents.xcworkspacedata similarity index 70% rename from learn/swift/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata rename to learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 919434a..5e84e35 100644 --- a/learn/swift/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata +++ b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:AppleSwift.xcodeproj"> diff --git a/learn/swift/MyPlayground.playground/playground.xcworkspace/xcshareddata/MyPlayground.xccheckout b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcshareddata/AppleSwift.xccheckout similarity index 87% rename from learn/swift/MyPlayground.playground/playground.xcworkspace/xcshareddata/MyPlayground.xccheckout rename to learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcshareddata/AppleSwift.xccheckout index 51108a5..e196f85 100644 --- a/learn/swift/MyPlayground.playground/playground.xcworkspace/xcshareddata/MyPlayground.xccheckout +++ b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcshareddata/AppleSwift.xccheckout @@ -5,20 +5,20 @@ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier - 1330DDA2-1B99-4E60-9F3A-B7DE1D7ECB22 + C6698B6B-E176-4266-9A99-7EA7B71954A4 IDESourceControlProjectName - MyPlayground + project IDESourceControlProjectOriginsDictionary BA634633803B1A00DDD2BCDEF5C645E5844F56E6 https://github.com/acevest/acecode.git IDESourceControlProjectPath - learn/swift/MyPlayground.playground + learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace IDESourceControlProjectRelativeInstallPathDictionary BA634633803B1A00DDD2BCDEF5C645E5844F56E6 - ../../.. + ../../../.. IDESourceControlProjectURL https://github.com/acevest/acecode.git diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..596ab3f Binary files /dev/null and b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/AppleSwift.xcscheme b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/AppleSwift.xcscheme new file mode 100644 index 0000000..8bc289c --- /dev/null +++ b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/AppleSwift.xcscheme @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/xcschememanagement.plist b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..15e61c0 --- /dev/null +++ b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + AppleSwift.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 5063B2A11B390AAF009C5821 + + primary + + + + + diff --git a/learn/AppleSwift/AppleSwift/main.swift b/learn/AppleSwift/AppleSwift/main.swift new file mode 100644 index 0000000..da38bb1 --- /dev/null +++ b/learn/AppleSwift/AppleSwift/main.swift @@ -0,0 +1,240 @@ +// +// main.swift +// AppleSwift +// +// Created by Ace on 15/6/23. +// Copyright (c) 2015年 Ace. All rights reserved. +// + +import Foundation + + +let Constant = 0x1234 // let 声明常量 +var Variable = 0xAFEC // var 声明变量 +var VarInt = 8421 +var VarFloat = 3.1415926 +var VarDouble:Double = 2.71828182846 //如果初始类型信息不足,可以在变量后加":类型" +var VarF:Float = 0.1314 +var str:String = "Hello, World!" + + +let ss = str + " " + String(Variable) // 类型必需显示转换 + + +// 可以通过在客串中加\()的方式把值转换成字符串 +let Int2Str = "translate int in string with \(VarInt) \(ss)" +println(Int2Str) + + +// 数组 +var arrayList = ["apple", "microsoft", "xx", "google", "tencent", "twitter"] +arrayList[2] = "amazon" +println(arrayList) + +// 字典 +var dict = [ + "apple" : "USA", + "tencnet" : "CN" +] +println(dict) +println(dict["apple"]) +println(dict["apple"]!) + +// 创建空数组和空字典 +var EmptyArray = [String]() +var EmptyDict = [String:String]() +// or +var EmptArrayWithNoType = [] +var EmptDictWithNoType = [:] + +for cp in arrayList { + println(cp) +} + + + +var OptionalVar:String? = "hehe" +//OptionalVar = nil +if let name = OptionalVar { + println("Hello \(OptionalVar)") +} +else +{ + println("sssssss") +} + + +let word = "ff" + +switch word { +case "dd" : + println("unknown") +case "ff" : + println("fallthrough") + fallthrough // 会走到下一个case +case "red apple", "blue apple" : + println("red") +default: // 必须在最后。。。 + println("default"); +} + + +// 闭区间 +for i in 0...3 { + print(i) +} +println("") +// 开区间 +for i in 0..<3 { + print(i) +} + + +for i in 0..=[] +for i in 1...5 { + IntArray.append(i) +} +IntArray += [0, -1, -2, -3, -4] +IntArray[0] = 1024 +IntArray[1..<4]=[999, 888, 777] +IntArray.insert(1, atIndex: 0) +IntArray.removeAtIndex(5) +IntArray.removeLast() +println(IntArray) +for (index, value) in enumerate(IntArray) { + println("Item \(index+1): \(value)") +} + + +var StringArray = [String]() +StringArray.append("dd") +StringArray = [] + +var DoubleArray = [Double](count:3, repeatedValue:9.9) +for i in 1...5 { + DoubleArray.append(Double(i)*1.1) +} +println(DoubleArray) + + +// Set +var SetA=Set() +var SetB:Set=[] +var SetC: Set = ["a", "b", "b"] + +SetA.insert(1) +SetA.insert(2) +SetA.insert(1) +if SetA.contains(1) { + println(SetA) +} + +for genre in sorted(SetA) { + println("\(genre)") +} + +let oddSet:Set = [1, 3, 5, 7, 9] +let evenSet:Set = [0, 2, 4, 6, 8] +println(sorted(oddSet.intersect(evenSet))) // 交 +println(sorted(oddSet.union(evenSet))) // 并 +println(oddSet.hashValue) + + +// Dict +var airports: [String:String] = ["TYO" : "Tokyo", "DUB":"Dublin"] +airports["LHR"] = "London" +airports["APL"] = "Apple Internation" +if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB") { + println("The old value for DIB was \(oldValue)") +} + +for (k, v) in airports { + println("\(k): \(v)") +} + +for k in airports.keys { + println("Airport Code: \(k)") +} + +for v in airports.values { + println("Airport Name: \(v)") +} + +println(airports) +airports["APL"] = nil // Delete +println(airports) + +if let removedValue = airports.removeValueForKey("DUB") { + println("The removed airport's name is \(removedValue)") +} else { + println("The airports dictionary does not contain a value for DUB") +} + +// create an empty dict +var emptyDict = Dictionary() +emptyDict[16] = "sixteen" +emptyDict = [:] + + + + + + + diff --git a/learn/swift/MyPlayground.playground/contents.xcplayground b/learn/swift/MyPlayground.playground/contents.xcplayground deleted file mode 100644 index 8e39341..0000000 --- a/learn/swift/MyPlayground.playground/contents.xcplayground +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/learn/swift/MyPlayground.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/swift/MyPlayground.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 0624362..0000000 Binary files a/learn/swift/MyPlayground.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/learn/swift/MyPlayground.playground/section-1.swift b/learn/swift/MyPlayground.playground/section-1.swift deleted file mode 100644 index bb6b9ec..0000000 --- a/learn/swift/MyPlayground.playground/section-1.swift +++ /dev/null @@ -1,68 +0,0 @@ -// Playground - noun: a place where people can play - -import UIKit - -var str = "Hello, playground" -println(str) - -let Constant = 0x1234 // let 声明常量 -var Variable = 0xAFEC // var 声明变量 -var VarInt = 8421 -var VarFloat = 3.1415926 -var VarDouble:Double = 2.71828182846 //如果初始类型信息不足,可以在变量后加":类型" -var VarF:Float = 0.1314 - - -let s = str + String(Variable) // 类型必需显示转换 - - -// 可以通过在客串中加\()的方式把值转换成字符串 -let Int2Str = "translate int in string with \(VarInt) \(str)" -println(Int2Str) - - -// 数组 -var arrayList = ["apple", "microsoft", "xx", "google", "tencent", "twitter"] -arrayList[2] = "amazon" -println(arrayList) - -// 字典 -var dict = [ - "apple" : "USA", - "tencnet" : "CN" -] -println(dict) - -// 创建空数组和空字典 -var EmptyArray = [String]() -var EmptyDict = [String:String]() -// or -var EmptArrayWithNoType = [] -var EmptDictWithNoType = [:] - -for cp in arrayList { - println(cp) -} - - - -var OptionalVar: String? = "hehe" -OptionalVar == nil -if let name = OptionalVar { - println("Hello \(OptionalVar)") -} -else -{ - println("sssssss") -} - - -let word = "red apple" - -//switch word { -//case "dd" : -//} - - - - diff --git a/learn/swift/MyPlayground.playground/timeline.xctimeline b/learn/swift/MyPlayground.playground/timeline.xctimeline deleted file mode 100644 index bf468af..0000000 --- a/learn/swift/MyPlayground.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/lib/RingBuffer.cpp b/lib/RingBuffer.cpp new file mode 100644 index 0000000..ac000d0 --- /dev/null +++ b/lib/RingBuffer.cpp @@ -0,0 +1,184 @@ +/* + * ------------------------------------------------------------------------ + * File Name: RingBuffer.cpp + * Thu Jun 18 16:13:49 2015 + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include + +class RingBuffer +{ +public: + RingBuffer(): buffer_(NULL), in_(0), out_(0), size_(0) {} + ~RingBuffer() {} + +public: + // Init - 初始化缓冲区 + // @buffer: 已经申请的用作缓冲区的内存指针 + // @size: 缓冲区的大小, 必须是2的整数次幂 + // + // 通过@buffer传入的内存, 可以是通过Alloc申请的, 也可以是外部自行分配的 + // 后者需要自行释放内存, 不适合通过Free释放 + int Init(unsigned char *buffer, unsigned int size) + { + if (!is_power_of_two(size)) + return -1; + + buffer_ = buffer; + size_ = size; + in_ = out_ = 0; + + return 0; + } + + // Alloc - 在堆上申请指定大小的缓冲区内存并初始化 + // @size: 缓冲区大小, 会向上调整到2的整数次幂 + // + // 通过Alloc申请的内存需要通过Free进行释放, 即Alloc和Free要配对使用 + int Alloc(unsigned int size) + { + unsigned char *buffer; + int ret; + + if (!is_power_of_two(size)) + size = roundup_power_of_two(size); + + buffer = new unsigned char[size]; + if (!buffer) + return -1; + + ret = Init(buffer, size); + if (ret != 0) + delete[] buffer; + + return 0; + } + + // Put - 向缓冲区拷贝指定长度的数据 + // @buffer: 拷贝到缓冲区的数据 + // @len: 数据长度 + // + // 从@buffer向缓冲区拷贝至多@len字节的数据, 具体拷贝长度依赖缓冲区的 + // 剩余空间大小, 实际拷贝长度通过返回值返回 + unsigned int Put(const unsigned char *buffer, unsigned int len) + { + unsigned int l; + + len = min(len, size_ - Len()); + + // 从buffer_的in_位置开始写入数据, 直到buffer_的结尾位置 + l = min(len, size_ - (in_ & (size_ - 1))); + memcpy(buffer_ + (in_ & (size_ - 1)), buffer, l); + + // 把剩余数据从buffer_的起始位置开始写入, 直到完成len字节写入 + memcpy(buffer_, buffer + l, len - l); + + in_ += len; + + return len; + } + + // Get - 从缓冲区中取出指定长度的数据 + // @buffer: 取出数据的拷贝内存 + // @len: 拷贝内存的长度 + // + // 向@buffer拷贝至多@len字节的数据, 实际拷贝长度通过返回值返回 + unsigned int Get(unsigned char *buffer, unsigned int len) + { + unsigned int l; + + len = min(len, Len()); + + // 从buffer_的out_位置开始读出数据, 直到buffer_的结束位置 + l = min(len, size_ - (out_ & (size_ - 1))); + memcpy(buffer, buffer_ + (out_ & (size_ - 1)), l); + + // 然后从buffer_的起始位置开始读出, 直到完成len字节读出 + memcpy(buffer + l, buffer_, len - l); + + out_ += len; + + return len; + } + + // Len - 返回缓冲区的数据长度 + unsigned int Len() + { + return in_ - out_; + } + + // Reset - 重置缓冲区, 即清空缓冲区数据 + void Reset() + { + in_ = out_ = 0; + } + + // Free - 释放缓冲区内存 + // 释放通过Alloc申请的缓冲区内存, 和Alloc配对使用 + void Free() + { + if (buffer_) + { + delete[] buffer_; + buffer_ = NULL; + } + } + +private: + template + T min(T a, T b) + { + return a > b ? b : a; + } + + bool is_power_of_two(unsigned int n) + { + return (n != 0 && ((n & (n - 1)) == 0)); + } + + unsigned int roundup_power_of_two(unsigned int n) + { + unsigned int fls = __builtin_clz(n); + return 1 << (sizeof(n) * 8 - fls); + } + +private: + unsigned char *buffer_; // 缓冲区指针 + unsigned int in_; // 写指针 + unsigned int out_; // 读指针 + unsigned int size_; // 缓冲区大小 +}; + +int main() +{ + RingBuffer rb; + rb.Alloc(7); + + unsigned char str[128] = {0}; + int n; + + str[0] = 'a'; + n = rb.Put(str, 1); + + str[1] = 'b'; + n = rb.Put(str, 2); + + str[2] = 'c'; + n = rb.Put(str, 3); + + str[3] = 'd'; + n = rb.Put(str, 4); + + str[4] = 'e'; + n = rb.Put(str, 5); + + n = rb.Get(str, 2); + n = rb.Get(str, 1); + + rb.Free(); + + return 0; +} diff --git a/tools/comm/countc.c b/tools/comm/countc.c index e71f771..1b978fc 100644 --- a/tools/comm/countc.c +++ b/tools/comm/countc.c @@ -91,6 +91,7 @@ void count_file(char *path) && !valid_type(path,"py") && !valid_type(path,"go") && !valid_type(path,"php") + && !valid_type(path,"swift") ) { #if 0